Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 46379

Error in Update operation coding using javascript between two entities.

$
0
0

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!


Viewing all articles
Browse latest Browse all 46379

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>