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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / how to get parent enti...
Power Apps
Answered

how to get parent entity id from inside a subgrid?

(0) ShareShare
ReportReport
Posted on by 167

I have an editable subgrid on a form in my model-driven app. I need to update the parent entity each time a change is made to a field on the subgrid. I attached some javascript to the onChange event of the subgrid. The event fires as expected, but I don't seem to be able to access the parent entity.

	function onLikelihoodChange (executionContext){
		var formContext = executionContext.getFormContext();
		var entityId = formContext.data.entity.getId().replace('{', '').replace('}', ''); //get child entity; want parent entity
		refreshRoleupField(executionContext, 'pm_preliminaryriskassessments', entityId, 'pm_answeredcount');
	 
	 console.info(entityId);//gives id of child entity
	}

 

Can I access the parent entity from the executionContext of the subgrid? Or do I need create some kind of listener function for the main form that "hears" the subgrid change?

 

What's the best way to do this?

 

I have the same question (0)
  • Ram Prakash Duraisamy Profile Picture
    5,593 Super User 2025 Season 2 on at

    Hello @JenniferK 

     

    Steps to follow :

     

    1. Make sure your parent entity field available in the View or in Form.

    2. Make sure it contains data.

    3. If both not working Make a retrieve call using ODATA and get the data (parent record)

    4. Update it.

     

    Let me know if you need clarity, share me some snapshot, where is your parent record available and where is subgrid available, so that we will share the code for the same.

     

    Could you please tel me which field you are trying to update. Is it a lookup field?

    Because in your screenshot am not seeing any field getting mapped

     

    Please mark as Answer if it is helpful and provide Kudos


    Subscribe : https://www.youtube.com/channel/UCnGNN3hdlKBOr6PXotskNLA
    Blog : https://microsoftcrmtechie.blogspot.com

  • JenniferK Profile Picture
    167 on at

    Hi @rampprakash ,

    I'm trying to force the recalculation of a rollup field on the parent entity. The line of code in my function that is supposed to update the parent record is this one:

    refreshRoleupField(executionContext, 'pm_preliminaryriskassessments', entityId, 'pm_answeredcount'); 

    The parent entity type is pm_preliminaryriskassessment and the rollup field to recalculate is pm_answeredcount. The problem is that the entityId that is being passed belongs to the subgrid selected record (i.e. the child entity).  I could do an oData query to get the parent entity, but that seems inefficient since the script is called from a form that should already have that data.

     

    Here is what my form looks like:

    JenniferK_0-1643920699178.png

    You can see that the parent entity exists in the main form. The subgrid has an onChange event attached to the Likelihood column.

  • Ram Prakash Duraisamy Profile Picture
    5,593 Super User 2025 Season 2 on at

    Hello @JenniferK 

     

    You can simply use 

    parent.formContext.data.entity.getId().replace('{', '').replace('}', '');

     

    Please mark as Answer if it is helpful and provide Kudos


    Subscribe : https://www.youtube.com/channel/UCnGNN3hdlKBOr6PXotskNLA
    Blog : https://microsoftcrmtechie.blogspot.com

     

  • JenniferK Profile Picture
    167 on at

    @rampprakash

    var formContext = executionContext.getFormContext();
    console.log(parent.formContext); //undefined

    The subgrid on which the onChange event is called is in a form section. I think that means that its parent will be a section and not a form. At any rate, `parent.formContext.data.entity...` does not exist.

  • Ram Prakash Duraisamy Profile Picture
    5,593 Super User 2025 Season 2 on at

    Hello @JenniferK 

     

    As you said there is a Relationship available, can you please open the record and share me the LOOKUP field.

     

    If the Lookup field is available add that in the view and use the same.

     

     

    Please mark as Answer if it is helpful and provide Kudos


    Subscribe : https://www.youtube.com/channel/UCnGNN3hdlKBOr6PXotskNLA
    Blog : https://microsoftcrmtechie.blogspot.com

  • Verified answer
    JenniferK Profile Picture
    167 on at

    I don't wish to include the parent record on the subgrid since it's already on the main form. While it would make scripting easy, it would be bad UI.

     

    I have discovered a different way to get the id of the parent record:

    var entityId = Xrm.Utility.getPageContext().input.entityId.replace('{', '').replace('}', '');

     

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 721 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 320 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard