All,
I need help with writing a javascript that will count how many salesorderdetail record is associated with the salesorder and also get the salesorderdetail guid.
The salesorderid seen below is for the order only, how can i get all the salesorderdetail associated with thats sales order ? Then update the stock status if it is blank.
var StockStatus = {}; StockStatus .available = 1; var salesorderId = Xrm.Page.data.entity.getId(); var req = new XMLHttpRequest(); req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/salesorderdetails(salesorderId)", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 204 || this.status === 200) { } else { alert(this.statusText); } } }; req.send(JSON.stringify(StockStatus));
*This post is locked for comments
See the info below
var SubGrid = SubGrid ? SubGrid : {}; SubGrid.OrderFormLoad = function () { //// ADD ON LOAD EVENT WHEN FORM LOADS, THIS WILL ENSURE RECORD COUNT IS UPADTED WHEN THE SUBGRID REFRESHES Xrm.Page.getControl("WebResource_productlotgrid").addOnLoad(SubGrid.GetTotalRecordCount); }; SubGrid.GetTotalRecordCount = function () { var functionName = "GetTotalRecordCount"; try { //setting timeout beacuse subgid take some time to load after the form is loaded setTimeout(function () { ////validating to check if the sub grid is present on the form if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("WebResource_productlotgrid") != null && Xrm.Page.getControl("WebResource_productlotgrid") != undefined) { { /// GET RECORD COUNT var filteredRecordCount = Xrm.Page.getControl("WebResource_productlotgrid").getGrid().getTotalRecordCount(); //Xrm.Utility.alertDialog("Order product Record Count: " + filteredRecordCount, null); var allGridEntityIDs = []; var rows = Xrm.Page.getControl("WebResource_productlotgrid").getGrid().getRows(); rows.forEach(function (row, i) { var entityReference = row.getData().getEntity().getEntityReference(); var guid = entityReference.id; allGridEntityIDs.push(guid); }); //// ALL GUIDS WILL BE IN THE ARRAY allGridEntityIDs //// SET STOCK STATUS - ATTRIBUTE VALUE Xrm.Page.getAttribute("new_numberofitems").setValue("rows"); } } }, 5000); } catch (e) { Xrm.Utility.alertDialog(functionName + "Error: " + (e.message || e.description)); } };
It is definitely not deprecated. On the contrary, this method was introduced with CRM 2015 Update 1. I tested it and is working properly on CRM 2016 Update 1 online environment.
Refer the official article from msdn-
[View:https://msdn.microsoft.com/en-in/library/dn932126.aspx]
It didnt work - it says it's not supported.
I'm on 2016 Online - perhaps its deprecated.
Please tick Verify Answer if it resolved your query.
Open your form. Click Form Editor from the ribbon.
In form editor select your subgrid like below:
Click Change properties from ribbon, you will see a popup which has field - "Name". This is the unique name of the Subgrid control:
Please let me know if you need more info. Waiting for your update if you are able to resolve this issue.
1) CRM 2016 Online
2) Where can I find the name of the grid on the Order form?
Can you please confirm :
1. you are using CRM 2015 Update 1 or later
2. The unique name of your subgrid on the form is correct. Because the name - "salesorderdetailsGrid" is in my CRM order form. It most probably woud be different on your form.
Additionally can you send screenshot of the subgrid control in form editor.
[quote user="Somesh2207"]
Hi,
You can also using the new Subgrid events and functions from CRM 2015 Update 1. See code to retrieve subgrid data count and subgrid Ids below.
Additionally, you have to register both the functions on form on-load. Refer Screenshot below:
<Mark this as an answer if it solves your issue.>
JS Source Code:
var SubGrid = SubGrid ? SubGrid : {}; SubGrid.OrderFormLoad = function () { //// ADD ON LOAD EVENT WHEN FORM LOADS, THIS WILL ENSURE RECORD COUNT IS UPADTED WHEN THE SUBGRID REFRESHES Xrm.Page.getControl("salesorderdetailsGrid").addOnLoad(SubGrid.GetTotalRecordCount); }; SubGrid.GetTotalRecordCount = function () { var functionName = "GetTotalRecordCount"; try { //setting timeout beacuse subgid take some time to load after the form is loaded setTimeout(function () { ////validating to check if the sub grid is present on the form if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("salesorderdetailsGrid") != null && Xrm.Page.getControl("salesorderdetailsGrid") != undefined) { { /// GET RECORD COUNT var filteredRecordCount = Xrm.Page.getControl("salesorderdetailsGrid").getGrid().getTotalRecordCount(); //Xrm.Utility.alertDialog("Order product Record Count: " + filteredRecordCount, null); var allGridEntityIDs = []; var rows = Xrm.Page.getControl("salesorderdetailsGrid").getGrid().getRows(); rows.forEach(function (row, i) { var entityReference = row.getData().getEntity().getEntityReference(); var guid = entityReference.id; allGridEntityIDs.push(guid); }); //// ALL GUIDS WILL BE IN THE ARRAY allGridEntityIDs //// SET STOCK STATUS - ATTRIBUTE VALUE Xrm.Page.getAttribute("yourattributeName").setValue("yourvalue"); } } }, 5000); } catch (e) { Xrm.Utility.alertDialog(functionName + "Error: " + (e.message || e.description)); } };
[/quote]Got a script error - One of the scripts for this record caused an error. Object doesnt support property or method 'addOnLoad' at subgrid.orderformload('")
I keep getting resource not found for 'salesorderdetail'
function retrieveentityCollection(entity, options) {
var serverURL = Xrm.Page.context.getClientUrl();
var Query = entity + options;
var req = new XMLHttpRequest();
req.open("GET", serverURL + "/api/data/v8.0/" + Query, true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function() {
if (this.readyState == 4 /* complete */ ) {
req.onreadystatechange = null;
if (this.status == 200) {
var data = JSON.parse(this.response);
if(data['@odata.count']!=null)
Xrm.Page.getAttribute("new_numberofitems").setValue(data['@odata.count']);
} else {
var error = JSON.parse(this.response).error;
alert(error.message);
}
}
};
req.send();
}
function SalesOrderOnLoad() {
var Id = Xrm.Page.data.entity.getId().substring(1, 37); //remove braces
var entity = "salesorderdetail";
//note here we need to use _accounted_value instead of parentcustomerid
var columnSet = "?$select=salesorderdetailid&$filter=_salesorderdetailid_value eq " + Id + "&$count=true";
retrieveentityCollection(entity, columnSet);
}
Hi,
You can also using the new Subgrid events and functions from CRM 2015 Update 1. See code to retrieve subgrid data count and subgrid Ids below.
Additionally, you have to register both the functions on form on-load. Refer Screenshot below:
<Mark this as an answer if it solves your issue.>
JS Source Code:
var SubGrid = SubGrid ? SubGrid : {}; SubGrid.OrderFormLoad = function () { //// ADD ON LOAD EVENT WHEN FORM LOADS, THIS WILL ENSURE RECORD COUNT IS UPADTED WHEN THE SUBGRID REFRESHES Xrm.Page.getControl("salesorderdetailsGrid").addOnLoad(SubGrid.GetTotalRecordCount); }; SubGrid.GetTotalRecordCount = function () { var functionName = "GetTotalRecordCount"; try { //setting timeout beacuse subgid take some time to load after the form is loaded setTimeout(function () { ////validating to check if the sub grid is present on the form if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("salesorderdetailsGrid") != null && Xrm.Page.getControl("salesorderdetailsGrid") != undefined) { { /// GET RECORD COUNT var filteredRecordCount = Xrm.Page.getControl("salesorderdetailsGrid").getGrid().getTotalRecordCount(); //Xrm.Utility.alertDialog("Order product Record Count: " + filteredRecordCount, null); var allGridEntityIDs = []; var rows = Xrm.Page.getControl("salesorderdetailsGrid").getGrid().getRows(); rows.forEach(function (row, i) { var entityReference = row.getData().getEntity().getEntityReference(); var guid = entityReference.id; allGridEntityIDs.push(guid); }); //// ALL GUIDS WILL BE IN THE ARRAY allGridEntityIDs //// SET STOCK STATUS - ATTRIBUTE VALUE Xrm.Page.getAttribute("yourattributeName").setValue("yourvalue"); } } }, 5000); } catch (e) { Xrm.Utility.alertDialog(functionName + "Error: " + (e.message || e.description)); } };
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156