web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to get fields from child entity and set it to the parent entity fields

(0) ShareShare
ReportReport
Posted on by 74

Hello,

I have two entities called booking and payment list and there is 1:N relation between them.

Now i want to get latest record from payment list entity which is child entity of booking.

Payment list entity has multiple records and i want only the last record(who has payment date) and that data should be set to the parent entity (booking) fields.

I want to achieve this using javascript.

please help me in this.

Thank You.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Rawish Kumar Profile Picture
    13,758 on at

    Hi Suraj,

    I would recommend you download the rest builder and take full advantage of this amazing tool. You will have to web api request to fetch child record and order by the date and pick the first record from the object.  https://github.com/jlattimer/CRMRESTBuilder/releases    get the managed solution and import to your enviornment. You will be able to generate the code.  I am in below exmaples fetching all contacts from an account. _ -click on generate code.

    48185.2.png

    Here is the code you will get ,  you can call this function on on load of the parent record or onchange of any field.
    function GetChildRecords()

    {

    var id =Xrm.Page.data.entity.getId();

    var newid = id.slice(1, -1);

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/contacts?$select=createdon,fullname&$filter=_accountid_value eq "+newid +"&$orderby=createdon desc", 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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    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 createdon = results.value[i]["createdon"];
    var fullname = results.value[i]["fullname"];

    //you can take the first result here as we have set order by  and set the values to the require fields. 

    //exmaple  Xrm.Page.getAttribute(“fieldname”).setValue(fullname );


    }
    } else {
    Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send();

    }

  • Suraj Jadhav Profile Picture
    74 on at

    Hello Rawish ,

    I follow your instruction but i am not able to filter query based on option set value

    And the query builder filter gives me filter like filter=chargetype eq eq , even when i have pass option set value in rest builder.And the chargetype field is from child entity.

  • Verified answer
    Rawish Kumar Profile Picture
    13,758 on at

    Hi Suraj,

    It is okay. You can go from customizations -- entity --> fields --> open the field and get the value of the optionset set.

    and put it in the code manually like filter=chargetype eq 1 or filter=chargetype eq 56000001

    it will be okay.

  • Suraj Jadhav Profile Picture
    74 on at

    Hello Rawish,

    Thank you it's work but it's not sorting descending by date

    i have done it using rest builder please find below code line

    &$orderby=psm_duedate desc", true);

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    Hi Suraj,

    How did you know its not sorting. Did you debug the script  to see what record you are getting on [0] position?

  • Suraj Jadhav Profile Picture
    74 on at

    Hello Rawish ,

    I have total 8 records and i am sorting data on Due date

    1 record has due date 23-04-2016

    2 record 30-08-2017

    3 record  14-09-2016

    4th record 13-11-2016

    5th  30-11-2016

    6th 30-12-2016

    7th 16-12-2016

    8th 09-02-2019

    and i want 8th record first

    i am getting all dates as 2019-02-16

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    this works for me :

    04783.2.png

    rest builder - script , first result:

    04783.2.png

  • Suraj Jadhav Profile Picture
    74 on at

    My date field is custom field not created on

    and i just debug the cord and  date field taking same value as 2019-02-16

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    Hi Suraj,

    It is working fine for me even for custom date.

    please try to clear the cache and try in inprivate.

  • Community Member Profile Picture
    on at

    .. why JS and not just Rollup using max of date?
    Another solution would be on Change of the date to write it to the parent field using Workflow...

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans