Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

while adding a record in sub grid, On the Save and close , the column 'productName' of child form should be autopopulated in 'Products' column of parent form.

Posted on by Microsoft Employee

 Using OData I have to Accomplish this. What will be the Query. I am a new beginner.

I have two entities Order(Parent) and OrderProduct(child) and the relationship is 1:N. I have a column 'Products' in the Order Form. I have inserted OrderProduct as a Sub Grid in the Parent Form. In the Sub Grid, I have columns like'productId' and 'productName'. while adding a record in sub grid, On the Save and close , the column 'productName' of child form should be autopopulated in 'Products' column of parent form. 

Thanks in Advance.

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: while adding a record in sub grid, On the Save and close , the column 'productName' of child form should be autopopulated in 'Products' column of parent form.

    Hi Mahen,

    I have written update request on child entity 'orderproduct' and getting 'name' field value from the child entity, have to display the 'name' field value of child entity on the parent entity form field called 'product'. Please help me.

    function UpdateParentField() {

       var OrderId = Xrm.Page.data.entity.attributes.get("orderid").getValue()[0].id;

       var oDataEndpointUrl = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/orderproductSet?$filter=OrderId/Id eq guid'" + OrderId + "'";

       var results = GetDataUsingXMLHttpRequest(oDataEndpointUrl);

       if (results != null) {

           results = results.results;

           if (results != null && results.length > 0) {

               var name = Xrm.Page.data.entity.attributes.get("name").getValue();

               var setUservalue = new Array();

               setUservalue[0] = new Object();

               setUservalue[0].id = OrderId;

               setUservalue[0].entityType = 'order';

               setUservalue[0].name = name

               UpdateRecordUsingXMLHttpRequest(OrderId, setUservalue, 'order')

           }

       }

    }

    function UpdateRecordUsingXMLHttpRequest(id, object, type) {

       var id = id.replace('{', '').replace('}', '');

       var req = (window.XMLHttpRequest) ? new window.XMLHttpRequest : new ActiveXObject("MSXML2.XMLHTTP.3.0");

       req.open("POST", encodeURI(Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/" + type + "Set(guid'" + id + "')"), false);

       req.setRequestHeader("Accept", "application/json");

       req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

       req.setRequestHeader("X-HTTP-Method", "MERGE");

       req.onreadystatechange = function () {

           if (this.readyState == 4) {

            if (this.status == 200) {

                   var req = JSON.parse(req.responseText).d;

                   if (req.results.length > 0) {

                   }

               }

           }

       }

    }

  • Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: while adding a record in sub grid, On the Save and close , the column 'productName' of child form should be autopopulated in 'Products' column of parent form.

    In case you are looking for update sample you can refer my code sample here : himbap.com/blog

    And you can update lookup like below

    entityobject["lookupfieldname@odata.bind"] = "/lookupentityname(GUID of the record)" ;

    Again workflow and plugin will be best option :)

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: while adding a record in sub grid, On the Save and close , the column 'productName' of child form should be autopopulated in 'Products' column of parent form.

    Thank You Mahen.

  • Verified answer
    Preeti Sharma Profile Picture
    Preeti Sharma 2,678 on at
    RE: while adding a record in sub grid, On the Save and close , the column 'productName' of child form should be autopopulated in 'Products' column of parent form.

    Hi,

    You can retrieve child records using web api as  below.Here all child contacts of parent accounts are retrieved:

    function retireveAllChildContacts() {

       var parentAccountId = Xrm.Page.getAttribute('parentcustomerid').getValue();

       var req = new XMLHttpRequest();

       req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/contacts?$select=accountrolecode,firstname,fullname,gendercode,lastname,middlename,_ownerid_value&$filter=_accountid_value eq " + parentAccountId + "", false);

       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.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");

       req.setRequestHeader("Prefer", "odata.maxpagesize=10");

       req.onreadystatechange = function () {

           if (this.readyState === 4) {

               req.onreadystatechange = null;

               if (this.status === 200) {

                   var results = JSON.parse(this.response);

                   for (var i = 0; i < results.value.length; i++) {

                       var accountrolecode = results.value[i]["accountrolecode"];

                       var accountrolecode_formatted = results.value[i]["accountrolecode@OData.Community.Display.V1.FormattedValue"];

                       var firstname = results.value[i]["firstname"];

                       var fullname = results.value[i]["fullname"];

                       var gendercode = results.value[i]["gendercode"];

                       var gendercode_formatted = results.value[i]["gendercode@OData.Community.Display.V1.FormattedValue"];

                       var lastname = results.value[i]["lastname"];

                       var middlename = results.value[i]["middlename"];

                       var _ownerid_value = results.value[i]["_ownerid_value"];

                       var _ownerid_value_formatted = results.value[i]["_ownerid_value@OData.Community.Display.V1.FormattedValue"];

                   }

               }

               else {

                   alert(this.statusText);

               }

           }

       };

       req.send();

    }

    Hope this helps:)

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: while adding a record in sub grid, On the Save and close , the column 'productName' of child form should be autopopulated in 'Products' column of parent form.

    This code is not going to work, you need to write update request on child entity. As I said best option is to implement it with Workflow, you can also write plugin.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: while adding a record in sub grid, On the Save and close , the column 'productName' of child form should be autopopulated in 'Products' column of parent form.

    Hi Mahen,

    I have written this query to get the fields of Parent form.

    var oDataEndpointUrl = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/orderSet?$select = name &$filter=orderId eq guid'" + Xrm.Page.data.entity.getId() + "'";

    How should i get the fields of child Entity using a query.

    child entity name 'orderproduct'

    child field name 'productprice'

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: while adding a record in sub grid, On the Save and close , the column 'productName' of child form should be autopopulated in 'Products' column of parent form.

    Hi Rosy,

    You can write a update call, in case you only want to do it using JS,

    1. Download Rest Builder github.com/.../rest-client.

    2. Generate a Update request based on the order id,

    3. Use js code on the save of the order line item, you can get parent order id and pass to your js function.

    If you have already written some js and facing some issue share it have for checking

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: while adding a record in sub grid, On the Save and close , the column 'productName' of child form should be autopopulated in 'Products' column of parent form.

    Hi,

    Could you please help with the Javascript OData.

    I am trying out with Javascript OData.

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: while adding a record in sub grid, On the Save and close , the column 'productName' of child form should be autopopulated in 'Products' column of parent form.

    Hello,

    Not sure why you want to implement this as there could be multiple line item to order. Anyway you can simply setup a workflow on create of the order product and pick product from child and set it to parent record.

    Hope it will help.

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans