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 :
Customer experience | Sales, Customer Insights,...
Answered

I want to populate lookup value in child entity subgrid which has also this lookup field

(0) ShareShare
ReportReport
Posted on by 60

I have a lookup field on parent entity which should be populated in all existing or newly created record in child  entity which present as a subgrid in parent entity  form. So,

basically populate all subgrid record with the lookup value (field) present on the main form

Can I get the sample javascript code to do it?

Thanks in Advance.

I have the same question (0)
  • Verified answer
    Kosenurm Profile Picture
    85 on at

    I'm not sure this is the best code, but it should work as I do similar in my model driven app. Xrm.Page is deprecated so I think this should be updated.

    function populateLookupChildRecords() {
    var parentRecordId = Xrm.Page.data.entity.getId();
    var lookupValue = Xrm.Page.getAttribute("new_lookupfield").getValue();

    // Get the child entity records
    var childRecords = Xrm.Page.getControl("childentity_subgridname").getGrid().getRows().get();

    for (var i = 0; i < childRecords.length; i++) {
    var childRecordId = childRecords[i].getData().getEntity().getId();

    // Set the lookup value on the child record
    var entity = {};
    entity.new_lookupfield = { Id: parentRecordId, LogicalName: "parententity_logicalname" };

    Xrm.WebApi.updateRecord("childentity_logicalname", childRecordId, entity)
    .then(function success(result) {
    console.log("Lookup value set on child record with ID: " + childRecordId);
    },
    function (error) {
    console.log(error.message);
    });
    }
    }

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans