Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How to retrieve the guid for salesorderdetail? (Web API JavaScript Experts help!)

Posted on by Microsoft Employee

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to retrieve the guid for salesorderdetail? (Web API JavaScript Experts help!)

    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));
        }
    
    };


    18418.1.png

  • Suggested answer
    Somesh2207 Profile Picture
    Somesh2207 1,563 on at
    RE: How to retrieve the guid for salesorderdetail? (Web API JavaScript Experts help!)

    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]

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to retrieve the guid for salesorderdetail? (Web API JavaScript Experts help!)

    It didnt work - it says it's not supported.

    I'm on 2016 Online - perhaps its deprecated.

  • Somesh2207 Profile Picture
    Somesh2207 1,563 on at
    RE: How to retrieve the guid for salesorderdetail? (Web API JavaScript Experts help!)

    Please tick Verify Answer if it resolved your query.

  • Somesh2207 Profile Picture
    Somesh2207 1,563 on at
    RE: How to retrieve the guid for salesorderdetail? (Web API JavaScript Experts help!)

    Open your form. Click Form Editor from the ribbon.

    In form editor select your subgrid like below:

    3348.Capture2.PNG

    Click Change properties from ribbon, you will see a popup which has field - "Name". This is the unique name of the Subgrid control:

    3348.Capture2.PNG

    Please let me know if you need more info. Waiting for your update if you are able to resolve this issue.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to retrieve the guid for salesorderdetail? (Web API JavaScript Experts help!)

    1) CRM 2016 Online

    2) Where can I find the name of the grid on the Order form?

  • Somesh2207 Profile Picture
    Somesh2207 1,563 on at
    RE: How to retrieve the guid for salesorderdetail? (Web API JavaScript Experts help!)

    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.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to retrieve the guid for salesorderdetail? (Web API JavaScript Experts help!)

    [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.>

    formload_2D00_events.png

    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('")

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to retrieve the guid for salesorderdetail? (Web API JavaScript Experts help!)

    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);

    }

  • Suggested answer
    Somesh2207 Profile Picture
    Somesh2207 1,563 on at
    RE: How to retrieve the guid for salesorderdetail? (Web API JavaScript Experts help!)

    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.>

    formload_2D00_events.png

    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)); } };

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans