Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Error in Update operation coding using javascript between two entities.

Posted on by Microsoft Employee

Hi All,

I have two entities named Parent and Child.

Aim: To update the fields of the Child entity when Parent entity fields are modified and saved.

I have all the type of fields which I have used in these two entities.

Before this operation, when I create a record in Parent entity a record gets created in the Child entity with the Parent's name field.

I have a lookup field in the Child entity referring to the Parent entity. When I create a record in the Parent entity, the lookup field in the child entity also gets mapped with the corresponding Parent entity's name. 

So, after this, I trigger this update script in the on change event of a field, where if the fields of the parent is modified then automatically the edited values should be set in the child entity as well.

I have written the code. My code fails in the Updating part. I have no idea where it fails in the Updating part. 

Following is my code: 

//Javascript

function retrievenupdate() {
    debugger;

    // Get current form ID

    var Id = Xrm.Page.data.entity.getId();

    //Object declaration

    var crmobject = new Object();

    //Parent entity ( getting field's logical names)

    var name = Xrm.Page.getAttribute("new_name").getValue();

    var ops = Xrm.Page.getAttribute("new_mypicklist").getValue();

    var str = Xrm.Page.getAttribute("new_mystring").getValue();

    var inti = Xrm.Page.getAttribute("new_myinteger").getValue();

    var dec = Xrm.Page.getAttribute("new_mydecimal").getValue();

    var dt = Xrm.Page.getAttribute("new_mydate").getValue();

    var float = Xrm.Page.getAttribute("new_myfloat").getValue();

    var bool = Xrm.Page.getAttribute("new_myboolean").getValue();

    var add = Xrm.Page.getAttribute("new_myaddress").getValue();

    var currency = Xrm.Page.getAttribute("new_mycurrency").getValue();

    var lookup = Xrm.Page.getAttribute("new_mylookup").getValue();
    var lookupid = Xrm.Page.getAttribute("new_mylookup").getValue()[0].id;
    var lookuplogicalname = Xrm.Page.getAttribute("new_mylookup").getValue()[0].entityType;



    var context = Xrm.Page.context;

    var serverUrl = context.getClientUrl();

    var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";

    var retrieveResult = new XMLHttpRequest();

    retrieveResult.open("GET", ODataPath + "/new_childSet?$select=new_name,new_String,new_Picklist,new_Integer,new_Decimal,new_Date,new_Currency,new_Address,new_Float,new_Boolean,new_Lookup,new_LookupTo,new_childId&$filter=new_LookupTo/Id eq guid'" + Id + "' ", false);

    retrieveResult.setRequestHeader("Accept", "application/json");

    retrieveResult.setRequestHeader("Content-Type", "application/json; charset=utf-8?");

    retrieveResult.send();

    if (retrieveResult.readyState == 4 /* complete */) {
        if (retrieveResult.status == 200) {
            var retrieved = this.parent.JSON.parse(retrieveResult.responseText).d;


            var Result = retrieved.results;
            var Name = "";

            if (retrieved.results.length > 0)
            {
                if (typeof Result !== "undefined")
                {
                    for (var i = 0; i < retrieved.results.length; i++)
                    {


                        //Variable declaration for Child entity's primarykey (ID)

                        var childId = Result[i].new_childId;

                        //Setting child entitiy's value with parent entity's variable

                        //string
                        crmobject.new_Name = name;
                        crmobject.new_String = str;
                        crmobject.new_Address = add;

                        //Optionset
                        crmobject.new_Picklist = { Value: ops };

                        //Integer
                        crmobject.new_Integer = parseInt(inti);

                        //Decimal
                        crmobject.new_Decimal = parseFloat(dec).toFixed(2);

                        //Float
                        crmobject.new_Float = parseFloat(float).toFixed(2);

                        //Currency
                        crmobject.new_Currency = { Value: currency };

//Boolean
crmobject.new_Boolean = bool; //Lookup crmobject.new_Lookup = { Id: lookupid, LogicalName: lookuplogicalname, Name: lookup }; //Date field var dt = new Date(); crmobject.new_Date = dt; // Parse the entity object into JSON var jsonEntity = window.JSON.stringify(crmobject); var serverUrl = Xrm.Page.context.getClientUrl(); var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc/new_childSet"; var ODataPath = serverUrl + ODATA_ENDPOINT; $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", datatype: "json", url: ODataPath + "(guid'" + childId + "')", //child entity guid with variable childId data: jsonEntity, beforeSend: function (XMLHttpRequest) { //Specifying this header ensures that the results will be returned as JSON. XMLHttpRequest.setRequestHeader("Accept", "application/json"); XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE"); }, success: function (data, textStatus, XmlHttpRequest) { alert("Record Updated"); }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert("fail to update"); } }); } } } } } }

Please guide me where I am wrong. Awaiting for your valid responses!

*This post is locked for comments

  • Felippe Profile Picture
    Felippe 796 on at
    RE: Error in Update operation coding using javascript between two entities.

    Hi Meiyappan Ram,

    If you doesn't have any tips for the reason of the fail, first Debug with a (F12 Debugger) on your preferred browser, and take the message of the error.

    After that, if isn't enough, you can use the excelent solution CRM Rest Builder by Jason Lattimer, to compare your url and parameters with the generated by the tool.

    After that, you'll can differ if the error are related with your sintax or with your object data.

    community.dynamics.com/.../crm-rest-builder-2-3-0-0-now-with-web-api

    If this solves, please let me know and  mark as "Verified Answer"

    Best Regards,

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans