How can I know when the one to many relationship the associated entity data is saved, and I can make change for parent entity?
- Hi All:
I have two entity entity1 and entity2, and there is one to many relationship between entity1(one)->entity2(many). There is a field on entity1 called total, and the field call subtotal on entity2, and the value of field "total" is the sum of value of subtotal from all related entity2. I had the entity2 associated view in the Iframe of Entity1 form. What I wanted to do is after user add new entry of entity2 or edit the subtotal from entity2, and close the entity2 form, the value of total in entity1 form automatically changed according to the new data of subtotal. The problem for this I had is I could find a way to trace that the entity2 is saved to database. I tried onbeforeunload of entity2, it didnot work, seem to me the data have not saved yet at that time.
I appreciate for all the helps
All Replies
- My recommendation: Plugin. Establish a plugin to fire on the Update of entity2, and have it sum the values from all entity2 related to its parent entity1 and push the total into parent entity1. This is the simplest and easiest method to achieve this, and guarantees that the total will be pushed back into entity1 even if the data of entity2 is changed from some other view (such as an Advanced Find) that isn't necessarily encapsulated by entity1.
Dave Berry- Proposed As Answer byMSCRM Blogger Thursday, November 05, 2009 12:28 PM
- You could update the "parent" entity total field in jscript at the OnSave of the "child" entity form. You would need two soap calls, one to multipleRetrieve all the children and one to update the parent.total once you've summed up what you got in the retrieval.
Check out the SDK for the soap/jscript versions of CrmService.RetrieveMultiple and .Update for examples.
The parent's form will not necessarily update to the current database value just because the child closes, however. OnSave() did not work. OnSave() event of the "child" entity form is fired before the data is saved to database, so CrmService.RetrieveMultiple still get the old data of "subtotal".
I will try Plugin and see if that work.
ThanksOk, I found a simple way to do this:
decalre a varible var oldSubtotal = crmForm.all.subtotal.DataValue==null?0:crmForm.all.subtotal.DataValue;
on cfmForm.onload event of child form (entity2)
On the OnSave() Event of child form (entity2)
OnSave()
{
var parentWin = window.opener.Parent;
if(parentWin)
{
var parentForm = parentWin.document.all.crmForm;
if(parentForm)
{
var oldTotal = parentForm.all.total.DataValue==null?0:parentForm.all.total.DataValue;
parentForm.all.total.DataValue = oldTotal-oldSubTotal + crmForm.all.subtotal.DataValue==null?0: crmForm.all.subtotal.DataValue;
}
}
}- Once again, I'm going to suggest that your approach leaves room for error when it comes to updating prices on the parent entity if the child entity was opened any other way than from within the parent. Coding yourself in this kind of corner can have unintended consequences later on (which may present themselves when attempting to be efficient within standard platform operations such as Bulk Editing, or Workflow-initiated changes). However, I applaud you for discovering a solution that will work for you, and hope the best of it.
Dave Berry

