locked
How to update value in child entity? RRS feed

  • Question

  • HI all,

    In the order entity i have one boolean button, If the value is yes it will create a record in a custom entity (Passing value from order entity to custom entity thorugh workflow) which is working. I also have another cutom field (drop down) in order entity. If i do any changes in this field that should get reflected field in the custom  entity of the same record which i passed value. 

    I tried this through workflow. but it is not working.  any solutions?

    Tuesday, January 17, 2012 12:57 PM

Answers

  • With Workflow you cannot update a child entity from a parent because there can be more than 1.  The current workflow engine does not give you the capability to query child entities and perform an update.    In order to accomplish, you can do 1 of 3 things:

    Custom Workflow Assembly:

    1. You would need to write a .NET workflow assembly for CRM that you could call from your main workflow that would handle updating the child entities for the parent entity.

    CRM Plug-In:

    2. You could write a PostUpdate plug-in on the Order that is triggered when the specified field changes.  When the field changes the plug-in would then retrieve the child objects and update the field accordingly.

    JavaScript OnSave:

    3. You could write a JScript function that would fire OnSave of the Order Entity and where the field you are monitoring has been changed (IsDirty flag).  Your JScript could call the CRM Web Service and update the associated child records as well.

    Options 1 & 2 use .NET and there are some very good examples of how to write a plug-in or custom workflow assembly in the CRM SDK.  JavaScript would be a little bit more work.  My preference in situations like this is to use a plug-in personally because the update to the child records happen within the normal save process of the main entity.

    Jeremy

    Wednesday, January 18, 2012 4:37 AM

All replies

  • I'm assuming you have the workflow set to Organization scope and the trigger to run the worklfow is When a field changes with the drop down field (option set) selected.

    My suggestion for this is to use an IF statement.  The workflow would look something like this:

    • Check Condition - If Order Option Set = x then Custom Entity field = 1
    • If Order Option Set = y then Custom Entity field = 2
    • Else Order Option Set = z then Custom Entity field = 3

    I hope this helps get things working


    Regards, Donna

    Tuesday, January 17, 2012 1:19 PM
  • Hi Donno,

    I have used both, trigger workflow when field changed & also the if condition. but still its not working.

    Tuesday, January 17, 2012 1:25 PM
  • Can you open the system job that was created from the workflow and post a screenshot of the error please.

    Regards, Donna

    Tuesday, January 17, 2012 1:56 PM
  • With Workflow you cannot update a child entity from a parent because there can be more than 1.  The current workflow engine does not give you the capability to query child entities and perform an update.    In order to accomplish, you can do 1 of 3 things:

    Custom Workflow Assembly:

    1. You would need to write a .NET workflow assembly for CRM that you could call from your main workflow that would handle updating the child entities for the parent entity.

    CRM Plug-In:

    2. You could write a PostUpdate plug-in on the Order that is triggered when the specified field changes.  When the field changes the plug-in would then retrieve the child objects and update the field accordingly.

    JavaScript OnSave:

    3. You could write a JScript function that would fire OnSave of the Order Entity and where the field you are monitoring has been changed (IsDirty flag).  Your JScript could call the CRM Web Service and update the associated child records as well.

    Options 1 & 2 use .NET and there are some very good examples of how to write a plug-in or custom workflow assembly in the CRM SDK.  JavaScript would be a little bit more work.  My preference in situations like this is to use a plug-in personally because the update to the child records happen within the normal save process of the main entity.

    Jeremy

    Wednesday, January 18, 2012 4:37 AM
  • Hi jeremy,

    In my case i need to go by step 3 (Using javascript) if u have any sample javascript code kindly share with me.

    Wednesday, January 18, 2012 6:06 AM
  • Shank,

    Go ahead and e-mail me directly and I can send you some code.  It's fairly long so I don't want to post it here.

    Jeremy

    Wednesday, January 18, 2012 5:52 PM
  • Jeremy, would it be possible for you to send me the example as well?

    If so, how do I go about getting your email?

    Thanks so much.

    Wednesday, January 18, 2012 9:27 PM
  • Shank/Jason,

    I've had a few requests for this.  I'm going to write a quick blog post with an example, either tonight or Thursday.  Once I post it, I'll provide the link to the post and code in this post.  Hopefully, I'm not holding you up by waiting until Thursday.

    Actually, I should ask whether both of you are using CRM 2011?

    Jeremy


    Thursday, January 19, 2012 12:57 AM
  • hi jeremy,

    i am using online ms crm 2011, we are waiting for your post.

    thanks,

    Shank

     

    Thursday, January 19, 2012 4:37 AM
  • Jeremy, I am on CRM 2011 as well. I am looking forward to your post.

    Again, thanks for your help and effort on this.

    Thursday, January 19, 2012 1:48 PM
  • Jason/Shank,

    I got a little tied up and having been able to write a blog post about this but I pulled the sample code together that demonstrates how to update a field on a Child Contact from the Parent OnSave Event.  I updated a simple string value (Telephone 1), but you can update more complex types like Lookups/Option Sets etc.   For an option set in REST you would say Contact.OptionSet.Value = 1 when setting the value.

    The script does require the JSON2.js Library file which can be accessed from the SDK Download for CRM 2011.  This .js file has to be included in the script libraries for your main phone.  Now, I haven't tested this code out so there may be a bug but not anything major since it came from code snippets I use reguarly.  I've commented a bunch of it so I hope this helps out. 

     

    // JScript source code
    //Requires JSON2.js library -- Can be grabbed from the CRM 2011 SDK Download  -- samplecode\js\restendpoint\javascriptrestdataoperations\javascriptrestdataoperations\scripts
    // Sample Code to Retrieve Child Contacts for an Account
    // Then Update  the Children
    
    
    //Filed_OnSave represents the Save of the form
    //This will trigger our JScript if the field we are watching is dirty
    function Form_OnSave() {
        if ((Xrm.Page.getAttribute("telephone1").getIsDirty()) && (Xrm.Page.ui.getFormType() == 2)) {
            //Field has been changed
            //Call JScript Function
            Account_RetrieveChildContacts(Xrm.Page.data.entity.getId());
        }
    }
    
    //This function will retrieve our Contact Objects
    function Account_RetrieveChildContacts(accountid) {
        var req = new XMLHttpRequest();
        //This is a CRM 2011 REST Query that is retrieving the ContactId where Parent Customer = This Account and Contact is Active
        req.open("GET",Xrm.Page.context.getServerUrl() + "/xrmservices/2011/OrganizationData.svc/ContactSet?$select=ContactId&$filter=ParentCustomerId/Id eq guid'" + accountid + "' and StateCode/Value eq 0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    
        //Determines what execute to function the request is sent and being processed
        req.onreadystatechange = function () { RetrieveChildContactsCallback(this); };
        req.send();
    }
    
    //Processes the Result Set
    function RetrieveChildContactsCallback(_retrieve) {
        if (_retrieve.readyState == 4) {
            if (_retrieve.status == 200) {
                //Using JSON to parse the XML into Object Notation, much easier than parsing XML
                var _contacts = JSON.parse(_retrieve.responseText).d;
    
                //Makes sure there is at least 1
                //Probably a better way to do it but this is how I typically do it
                if (_contacts.results[0] != null) {
                    //Loop the Result Set
                    for (var x in _contacts.results) {
                        //Get the Contact GUID
                        var _id = _contacts.results[x].ContactId;
    
                        //Set New Object
                        //Assign Value
                        //CAll Update Method
                        var _obj = {};
                        _obj.Telephone1 = Xrm.Page.getAttribute("telephone1").getValue();
                        UpdateChildContact(_id, _obj);
                    }
                }
                else {
                }
            }
        }
    }
    
    //REST Update Function
    function UpdateChildContact(id, object) {
        var req = new XMLHttpRequest();
        //THe message name is "POST" rather than "GET" since we want to post a change
        //This tells us what entity and what the GUID is of the record to update
        req.open("POST", Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/ContactSet(guid'" + id + "')", true);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    
        //Method is different here as well as we are merging the changes in
        req.setRequestHeader("X-HTTP-Method", "MERGE");
        req.onreadystatechange = function () {
            if (this.readyState == 4 /* complete */) {
                if (this.status == 204 || this.status == 1223) {
                    //Success Handler
                }
                else {
                    //Error Handling
                }
            }
        };
        //This actually takes your JScript object record for hte contact and converts it
        //so that it can be read/processed by the REST End POint
        req.send(JSON.stringify(object));
    }


    Let me know if you run into any issues or have additional questions!

     

    Thanks,

    Jeremy



    • Edited by Jeremy Winchell Tuesday, January 24, 2012 3:25 PM Fixed Script Error
    Tuesday, January 24, 2012 3:01 PM
  • All,

    Here is the post to the blog as well. 

    http://blog.avtex.com/?p=1676

    Thanks,

    Jeremy

    Tuesday, January 24, 2012 3:59 PM
  • Jeremy, thanks so much.

    I'm going to be looking at this over the next few days. I really appreciate your help and effort with this.

    Wednesday, January 25, 2012 3:30 PM