web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Retrieving and Updating a Record not Working

(0) ShareShare
ReportReport
Posted on by

Hey guys, this is my scenario. As I am new to Microsoft Dynamics coding, need a solution for my code.

I have two entities named Acc and Con, where Acc is parent and Con is child entity of Acc.

I have created a lookup field of Acc in Con entity along with fields with similar datatype in the two entities.

So, whenever a change in parent record is made in the fields and saved, the changes should be done automatically in the corresponding fields of the child record.

The part I get the error is to update but am able to retrieve the code. So the record fails to update.

The error I get now is 

ReferenceError: FetchRecordsCallBack is not defined at Object.GetRecs.jQuery.ajax.success

Your answers and suggestions are requested and welcome. :)

Here below is my code.

 

function accupdate() {
    debugger;
    var PrvdLocData;
    var name;
    var age;
    var sex;
    var address;
    var phonenumber;
    var degree;
    var sslcpercentage;
    var hscpercentage;
    var ugpercentage;

    var PrvdLocData = new Array();
    
    var EmpId = Xrm.Page.data.entity.getId();

    //parent fields
    if (Xrm.Page.getAttribute("new_name").getValue() != null) {
        name = Xrm.Page.getAttribute("new_name").getValue();
    }

    if (Xrm.Page.getAttribute("new_age").getValue() != null) {
        age = Xrm.Page.getAttribute("new_age").getValue();
    }

    if (Xrm.Page.getAttribute("new_sex").getValue() != null) {
        sex = Xrm.Page.getAttribute("new_sex").getValue();
    }
    
    if (Xrm.Page.getAttribute("new_address").getValue() != null) {
        address = Xrm.Page.getAttribute("new_address").getValue();
    }

    if (Xrm.Page.getAttribute("new_phonenumber").getValue() != null) {
        phonenumber = Xrm.Page.getAttribute("new_phonenumber").getValue();
    }

    if (Xrm.Page.getAttribute("new_degree").getValue() != null) {
        degree = Xrm.Page.getAttribute("new_degree").getValue();
    }

    if (Xrm.Page.getAttribute("new_sslcpercentage").getValue() != null) {
        sslcpercentage = Xrm.Page.getAttribute("new_sslcpercentage").getValue();
    }

    if (Xrm.Page.getAttribute("new_hscpercentage").getValue() != null) {
        hscpercentage = Xrm.Page.getAttribute("new_hscpercentage").getValue();
    }

    if (Xrm.Page.getAttribute("new_ugpercentage").getValue() != null) {
        ugpercentage = Xrm.Page.getAttribute("new_ugpercentage").getValue();
    }
   

    var context = Xrm.Page.context;
    var serverurl = context.getClientUrl();
    var ODatapath = serverurl + "/XRMServices/2011/OrganizationData.svc";
    var retrieveResult = new XMLHttpRequest();
    var dtqeurl = ODatapath + "/new_conSet?$select=new_name,new_YourAge,new_YourSex,new_HomeAddress,new_Contact,new_DegreeType,new_SSLCPercent,new_HSCPercent,new_conId,new_UGPercent&$filter=new_AccName/Id eq guid'" + EmpId + "'";
    GetRecs("PrvtLoc", dtqeurl);

    function GetRecs(entity, url) {
        jQuery.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: url,
            async: false,
            beforeSend: function XMLHttpRequest(XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/json");

            },
            success: function (data, textStatus, XmlHttpRequest) {
                if (data && data.d != null && data.d.results != null) {
                    AddRecordsToArray(data.d.results, entity);
                    
                }
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                //  alert("Error :  has occured during retrieval of the records ");
            }
        });
    }
    
    function AddRecordsToArray(result, entity) {
        $.each(result, function () {

            if (entity == "PrvtLoc")
                PrvdLocData.push(this);


        });
    }
    if (PrvdLocData.length > 0) {
        
        for (i = 0; i < PrvdLocData.length; i++) {

            //Object Assigning

            var crmobject = new Object();

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

            var childId = PrvdLocData[i].new_conId;

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

                crmobject.new_name = name;
                crmobject.new_YourAge = parseInt(age);
                crmobject.new_YourSex = sex;
                crmobject.new_HomeAddress = toString(address);
                crmobject.new_Contact = parseInt(phonenumber);
                crmobject.new_DegreeType = degree;
                crmobject.new_SSLCPercent = sslcpercentage;
                crmobject.new_HSCPercent = hscpercentage;
                crmobject.new_UGPercent = ugpercentage;


            // update starts...

        var serverUrl = Xrm.Page.context.getClientUrl();

        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
        var crmobject = new Object();
        // Specify the ODATA entity collection 
        var ODATA_EntityCollection = "/new_conSet";
        var jsonEntity = window.JSON.stringify(crmobject);
            //Asynchronous AJAX function to Create a CRM record using OData 
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                url: serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection + "(guid'" + 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("updated");



                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("fail to update");
                }
            });
        }


    }

}

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Is there any specific reason you are doing this using JS and not plugin or workflow?

  • Community Member Profile Picture
    on at

    Hey Vikas, Thanks for your reply.

    Since am new to MS CRM coding, I am doing this internally to improve my coding skills. Thats why I am not using plugin or workflow.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans