locked
CRM 2011 - Set Lookup Value on Child Entity Created from Parent Entity RRS feed

  • Question

  • Hi Forum,

    I am trying to create parent as well as child entity at the same time from parent entity 'On Save' using Odata/Javascript. ( I am aware of the fact the this can be achieved by Plugin but we want to do this using odata/Javascript)

    To explain this by dummy example,below is my execution flow.

    1. I have 2 custom entities 'Property' and 'Tenanat' ( one to many. 'Property' as parent and 'Tenant' as child). Also Child entity is not available to end user for data entry.

    2. On Property entity I have textbox to get name of the Tenant.

    3. On Tenant entity, I have text box to get Tenant name and lookup control for the Property entity.

    4. When user create new Property with Tenant name into it, it should create Property as well as Tenant record.

    5. On creation of the Tenant record from above step, it should copy name from Property entity and populate lookup control with Property entity record value to establish the relationship.

    I have created the odata/Javascript function for Onsave event of Property entity. This creates Property and Tenant entity fine but it doens't populate lookup value which is understandable

    because I haven't include this look up in my code. The reason for it is, I don't have parent entity ID at the time of saving record becuase it's a pre save process and thus I can't set it on child entity lookup.

    So, what I have done is, I created second function but this one is on child entity(tenant). This fucntion fires on save (not manualy but from the above step 4) and find the matching parent record and should populate this lookup control on the tenant entity. ( ideas is to relate this child record with parent once parentID got generated in above step 4).

    When I debug this second function I  see the Id of the matching parent record but it doens't set the lookup field.

    Now one thing to notice is , child entity form will never be open or close by end user. child record is created at the same time parent is created. this function only assoicate child with parent

    by setting up lookup value which was not possible in step 4 as Parent ID was not available during save process.

    In short, both parent and child record get created but lookup on child doesn't get populated once child record is created from parent.

    My Question is, does onsave event of child record fires in background when you create it from parent entity onsave??

    Belwo is by code

    // below function is registered for onsave of Tenant entity(child)
    
    function GetProperty() {
    
        var name = "";
    	
    	// this field to compare with parent entity field.  
    	//this field is available because child record is already created while parent was created.
        name = Xrm.Page.getAttribute("new_parentname").getValue(); 
        if (name != null) 
    	{
             RetrieveProperty(name);
        }
    }
    
    // below is oDate function
    function RetrieveProperty(_name) 
    {
        var entity = "new_parent";  // parent entity
        var select = "?$select=new_parentname";  // field to compare 
        var oDataSelect;
    
        oDataSelect = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/" + entity + "Set?$filter=new_parentname eq  '" + _name + "'" + "";
      
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: oDataSelect,
            beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
            success: function (data, textStatus, XmlHttpRequest) {
                
                ProcessProperty(data.d);
            },
            error: function (xmlHttpRequest, textStatus, errorThrown) {
                alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
            }
        });
    }
    
    
    function ProcessProperty(PropertyEntity)  // Getting Parent entity.
    {
        if (PropertyEntity != null)  
    	{
    	
    	// all values came OK during debug.
        var  Pid = PropertyEntity.results[0].new_propertyid;   // Getting parent record id.
        var  pname = PropertyEntity.results[0].new_propertyname;  // getting name of the property
    	
    	// new_propertyid = lookupid , new_property = entity type
    	SetLookupValue("new_propertyid",Pid,pname,"new_property");  // calling this function to set lookup value in Tenant entity record.
    
        }
    }
    
    function SetLookupValue(fieldName, id,name,entityType)  
    { 
        if (fieldName != null) 
       { 
            var lookupValue = new Array(); 
            lookupValue[0] = new Object(); 
            lookupValue[0].id = id; 
            lookupValue[0].name = name; 
            lookupValue[0].typename = entityType; 
    		
    		// below line is NOT setting lookup field event though I can see
           //lookup is generated fine during debug.
            Xrm.Page.getAttribute(fieldName).setValue(lookupValue); 
        } 
    } 

    Any ideas ?

    Thanks for your time.

    Cheers

    Thursday, June 21, 2012 2:05 AM

Answers

  • It's true that workflow from parent entity cannot access to the child records.

    But you can create a child record from the workflow in the parent entity.

    In the following example, the workflow will create a Contact (child) record when the Account (parent) record is created. Then, in the properties for the Contact record, set the newly created Account as a value of Parent Customer (to link the parent and child records) [lookup control for the Property in your case], and the value of Primary Contact from the Account to the Last Name of the Contact [Tenant name in your case]. I hope you get the logic.

    • Marked as answer by H.Desai Friday, June 22, 2012 4:52 AM
    Thursday, June 21, 2012 5:41 AM
  • Hi Desai,

    To access your child entity from parent entity, you can use the Distribute Workflow Activity.

    http://crm2011distributewf.codeplex.com/

    You need to create child workflow only for your child entity first.

    After that you can use Distribute Workflow Activity to access your child workflow (ie Tenant)



    Regards, Kyaw Kyaw Tun

    • Marked as answer by H.Desai Friday, June 22, 2012 4:53 AM
    Thursday, June 21, 2012 5:42 AM

All replies

  • My Question is, does onsave event of child record fires in background when you create it from parent entity onsave??

    The answer is "No". OnSave JavaScript of the child record only fires when the user trigger the save event on the child form.

    It won't trigger when the child record is saved by a web-service call, plugin, workflow or any server side events (obviously since it is a client script)

    There is a workaround solution for your scenario but that one is not going to be perfect.

    • Instead of creating a child record at OnSave event of the Parent record, add a new boolean flag "Child Record Created" on the parent entity.
    • OnLoad of the parent entity Update Form (Xrm.Page.ui.getFormType() == 2), check the flag and if it is false, create a child record (now you have the parent record ID) and update the flag to true.

    But the problem with this solution is if the user click "Save and Close", the child record won't be created until that parent record is opened again.

    If you don't want to go for Plugin development, you can achieve this with a workflow which will trigger on Create of the parent record and in the workflow steps, Create a child entity and populate the lookup with the parent record.

    • Edited by Linn Zaw Win Thursday, June 21, 2012 3:53 AM Adding Workaround solutions
    Thursday, June 21, 2012 3:35 AM
  • Hi Linn,

    Thanks for you response and alternative workaround.

    I did think of the workflow but , CRM workflow has a annoying limitation of not showing child entity in parent entity workflow steps.

    So in my example, I can not access "Tenant"(child) entity from "Property"(parent) entity workflow step.

    Appreciate your time.

    Cheers

    H.Desai

    Thursday, June 21, 2012 4:31 AM
  • It's true that workflow from parent entity cannot access to the child records.

    But you can create a child record from the workflow in the parent entity.

    In the following example, the workflow will create a Contact (child) record when the Account (parent) record is created. Then, in the properties for the Contact record, set the newly created Account as a value of Parent Customer (to link the parent and child records) [lookup control for the Property in your case], and the value of Primary Contact from the Account to the Last Name of the Contact [Tenant name in your case]. I hope you get the logic.

    • Marked as answer by H.Desai Friday, June 22, 2012 4:52 AM
    Thursday, June 21, 2012 5:41 AM
  • Hi Desai,

    To access your child entity from parent entity, you can use the Distribute Workflow Activity.

    http://crm2011distributewf.codeplex.com/

    You need to create child workflow only for your child entity first.

    After that you can use Distribute Workflow Activity to access your child workflow (ie Tenant)



    Regards, Kyaw Kyaw Tun

    • Marked as answer by H.Desai Friday, June 22, 2012 4:53 AM
    Thursday, June 21, 2012 5:42 AM
  • Hi Linn and Kyaw,

    Thanks for your follow ups and sugestions. It helped me to achieve my business goal which I managed with combination of Javascript and wokflow.

    Cheers,

    H.Desai

    Friday, June 22, 2012 4:57 AM