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?
Solved! Go to Solution.
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('}', '');
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
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:
You can see that the parent entity exists in the main form. The subgrid has an onChange event attached to the Likelihood column.
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
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.
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
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('}', '');
User | Count |
---|---|
19 | |
9 | |
9 | |
5 | |
5 |
User | Count |
---|---|
32 | |
30 | |
18 | |
18 | |
6 |