locked
CRM record Update is not reflecting immediately RRS feed

  • Question

  • Hello Everyone,

    I am facing problem in retrieving the form field value in javascript immediately after the field is updated by using plugin.

    Below is the brief of my functionality.

    1. In contact entity, I have a field called "MappingIds". I'm updating this field using Javascript. On Update of this field, plugin will fire which is registered on post operation and in synchronous mode. This plugin will do some custom logic and again update the field "MappingsIds" to null and along with it, it will update one more field say, "MappingsName".
    2. And in the same javascript function with some delay, I tried to retrieve both "MappingsIds" and MappingsName". Only "MappingsIds" came correct which is null but "MappingsName" value was not correct which was having old value. I even queried contact entity directly using OData. there also same. Later, I directly checked in advanced find there I could see it updated only "MappingIds" immediately but not MappingsName. After refreshing few seconds that value got reflected. I couldn't understand what is the actual issue.
    3. Note: I checked this by debugging the javascript.

    Please share your thoughts,if you aware. Thank you!


    Wednesday, June 3, 2015 2:02 PM

Answers

  • There are a few things I would suggest trying.

    Try messing with the timeout timer in both cases, if this is working I doubt you have to wait this long.

    First, .then with the save function, see below. This is an async call that will call the onSuccessCallBack function after the save is completed. This will only work if the entity you are working with is one of the so called Updated Entities, https://msdn.microsoft.com/en-us/library/41462684-3e5d-4858-8be4-1a7c4fcdeff6(v=crm.7)

    function CreateDigitalServices() {   // some custom logic here    // Save to person    Xrm.Page.getAttribute("vc_mappingsids").setValue("calculatedvalue");    Xrm.Page.data.save().then(onSuccessCallBack, onErrorCallBack); } function onSuccessCallBack() {    setTimeout(getAppContractID(), 5000); }

    function onErrorCallBack(code, message)
    {
    //Some error functionality
    }


    Or you could try and add a onSave event to the form.

    function CreateDigitalServices() {
       // some custom logic here
        // Save to person
        Xrm.Page.getAttribute("vc_mappingsids").setValue("calculatedvalue");
        Xrm.Page.data.entity.addOnSave(timeOutAndRunGetAppContractID);
        Xrm.Page.data.entity.save();
    }
    
    
    function timeOutAndRunGetAppContractID()
    {
        setTimeout(getAppContractID(), 5000);
    }
    
    function getAppContractID() {
        var Id = Xrm.Page.data.entity.getId(); // /xrmservices/2011/OrganizationData.svc/vc_appcontractSet?$select=vc_AppID,vc_Error&$filter=vc_appcontractId eq guid'E8E19C13-C202-E511-89BC-005056B70A08'
        var oDataURI = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/ContactSet(guid'" + Id + "')?$select=vc_mappingsname,vc_mappingsids";
        try {
            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                url: oDataURI, //Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/IncidentSet(guid'" + id + "')",
                async: true,
                beforeSend: function (XMLHttpRequest) {
                    //Specifying this header ensures that the results will be returned as JSON.
                    XMLHttpRequest.setRequestHeader("Accept", "application/json");
                },
                success: function (data, textStatus, XmlHttpRequest) {
                    if (data.d.vc_mappingsids == null) {
                        if (data.d.vc_mappingsname != null) {
                            alert("This person is already having " + data.d.vc_mappingsname);
                            var updateURI = Xrm.Page.context.getClientUrl()
                        + "/XRMServices/2011/OrganizationData.svc/"
                        + "ContactSet(guid'" + Id + "')";
                            $.ajax({
                                type: "POST",
                                async: false,
                                contentType: "application/json;charset=utf-8",
                                datatype: "json",
                                data: '{ "vc_mappingsname": "" }',
                                url: updateURI,
                                beforeSend: function (XMLHttpRequest) {
                                    //Specifying this header ensures that the results will be returned as JSON.
                                    XMLHttpRequest.setRequestHeader("Accept", "application/json");
                                    //Specify the HTTP method MERGE to update just the changes you are submitting.
                                    XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
                                },
                                success: function (data, textStatus, XmlHttpRequest) {
                                    
                                },
                                error: function (XmlHttpRequest, textStatus, errorThrown) {
                                    
                                }
                            });
                        }
                        addRefreshCount = 1;
                        subSrid.refresh();
                        return;
                    }
                    else if (addRefreshCount == 5) {
                        addRefreshCount = 1;
                        return;
                    }
                    else {
                        addRefreshCount++;
                        setTimeout(getAppContractID(), 5000);
                    }
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    if (XmlHttpRequest && XmlHttpRequest.responseText) {
                        subSrid.refresh();  //alert("Error while updating " + odataSetName + " ; Error – " + XmlHttpRequest.responseText);
                    }
                }
            });
        } catch (ex) {
    	
    Xrm.Page.data.entity.removeOnSave(timeOutAndRunGetAppContractID);
            console.log(ex.toString());
        }
    Xrm.Page.data.entity.removeOnSave(timeOutAndRunGetAppContractID);
    }

    Halldór Jóhannsson



    Thursday, June 4, 2015 9:28 AM

All replies

  • It is quite normal that you are unable to retrieve the changes from the plugin in the same javascript context. The plugin should not be running until the record is saved, so if you

    1. Make your changes in the javascript code. 
    2. Save the form in the javascript code
    3. The plugin should then run and update the record in the background
    4. You might have to wait here a bit in the javascript while the plugin runs
    5. Then you need to query the changed data from CRM not from the form.
    6. And then do your secondary javascript part.

    Couple of thoughts.

    • Can you move the secondary javascript functionality (the part that is suppose to run after the plugin) to the plugin itself?
    • Can you move the plugin functionality to the javascript?
    • Can you move the secondary javascript functionality to a workflow?
    • Can you move all the functionality to 1 place (Javascript, plugin or workflow)

    Halldór Jóhannsson



    Wednesday, June 3, 2015 2:18 PM
  • Thank you for your suggestions. I agree with your point 4. we need to wait unit plugin completes its execution. I have added delay as well.  And, here I'm using odata query to get these two fields. But I'm wondering about how it is giving "MappingIds" as null (which actually got updated from Plugin. Because in my javascript I'm updating this field with a value. I need to show alert message to user based on the value of the other field "MappingsName". That's the reason I preferred javascript.
    Wednesday, June 3, 2015 2:40 PM
  • Are you positive your plugin has fired?

    Are you saving the form inside your javascript to force the plugin to fire?

    Can you post your javascript code here?


    Halldór Jóhannsson

    Wednesday, June 3, 2015 2:46 PM
  • Just a thought...but is it possible you have auto-save turned on?  I'm pretty sure that would cause the plugin to fire even though the user hasn't done anything.
    Wednesday, June 3, 2015 5:53 PM
  • Yes. I'm saving the form in javascript to force the plugin to fire.

    PFB code for your reference.

    function CreateDigitalServices() {
       // some custom logic here
        // Save to person
        Xrm.Page.getAttribute("vc_mappingsids").setValue("calculatedvalue");
        Xrm.Page.data.save();
        setTimeout(getAppContractID(), 5000);
    }
    
    
    function getAppContractID() {
        var Id = Xrm.Page.data.entity.getId(); // /xrmservices/2011/OrganizationData.svc/vc_appcontractSet?$select=vc_AppID,vc_Error&$filter=vc_appcontractId eq guid'E8E19C13-C202-E511-89BC-005056B70A08'
        var oDataURI = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/ContactSet(guid'" + Id + "')?$select=vc_mappingsname,vc_mappingsids";
        try {
            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                url: oDataURI, //Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/IncidentSet(guid'" + id + "')",
                async: true,
                beforeSend: function (XMLHttpRequest) {
                    //Specifying this header ensures that the results will be returned as JSON.
                    XMLHttpRequest.setRequestHeader("Accept", "application/json");
                },
                success: function (data, textStatus, XmlHttpRequest) {
                    if (data.d.vc_mappingsids == null) {
                        if (data.d.vc_mappingsname != null) {
                            alert("This person is already having " + data.d.vc_mappingsname);
                            var updateURI = Xrm.Page.context.getClientUrl()
                        + "/XRMServices/2011/OrganizationData.svc/"
                        + "ContactSet(guid'" + Id + "')";
                            $.ajax({
                                type: "POST",
                                async: false,
                                contentType: "application/json;charset=utf-8",
                                datatype: "json",
                                data: '{ "vc_mappingsname": "" }',
                                url: updateURI,
                                beforeSend: function (XMLHttpRequest) {
                                    //Specifying this header ensures that the results will be returned as JSON.
                                    XMLHttpRequest.setRequestHeader("Accept", "application/json");
                                    //Specify the HTTP method MERGE to update just the changes you are submitting.
                                    XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
                                },
                                success: function (data, textStatus, XmlHttpRequest) {
                                    
                                },
                                error: function (XmlHttpRequest, textStatus, errorThrown) {
                                    
                                }
                            });
                        }
                        addRefreshCount = 1;
                        subSrid.refresh();
                        return;
                    }
                    else if (addRefreshCount == 5) {
                        addRefreshCount = 1;
                        return;
                    }
                    else {
                        addRefreshCount++;
                        setTimeout(getAppContractID(), 5000);
                    }
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    if (XmlHttpRequest && XmlHttpRequest.responseText) {
                        subSrid.refresh();  //alert("Error while updating " + odataSetName + " ; Error – " + XmlHttpRequest.responseText);
                    }
                }
            });
        } catch (ex) {
            console.log(ex.toString());
        }
    }

    Thursday, June 4, 2015 4:41 AM
  • Yes. You are correct. I checked auto save was enabled. I disabled and checked the functionality. But still it is not working!
    Thursday, June 4, 2015 4:42 AM
  • Try by increasing Delay time to 10 Seconds or 15

    setTimeout(getAppContractID(), 10000);

    because may be plugin not updated the "MappingsName" while script run earlier

    On which event your script is running (onload,save,onchange)??


    Muhammad Sohail



    • Edited by sohail450 Thursday, June 4, 2015 8:49 AM
    Thursday, June 4, 2015 8:48 AM
  • JavaScript is registered on custom button's action. My puzzle is, I'm updating both the fields, "MappingsIds" to null and "MappingsName" to some calculated value  in Plugin only and when I retrieve these two fields using odata query it is giving MappingsIds as null which is correct but MappingsName is also null but which should have value which got update in plugin. I couldn't understand how only one field got updated first and second field got updated with few mins delay !!!
    Thursday, June 4, 2015 9:18 AM
  • There are a few things I would suggest trying.

    Try messing with the timeout timer in both cases, if this is working I doubt you have to wait this long.

    First, .then with the save function, see below. This is an async call that will call the onSuccessCallBack function after the save is completed. This will only work if the entity you are working with is one of the so called Updated Entities, https://msdn.microsoft.com/en-us/library/41462684-3e5d-4858-8be4-1a7c4fcdeff6(v=crm.7)

    function CreateDigitalServices() {   // some custom logic here    // Save to person    Xrm.Page.getAttribute("vc_mappingsids").setValue("calculatedvalue");    Xrm.Page.data.save().then(onSuccessCallBack, onErrorCallBack); } function onSuccessCallBack() {    setTimeout(getAppContractID(), 5000); }

    function onErrorCallBack(code, message)
    {
    //Some error functionality
    }


    Or you could try and add a onSave event to the form.

    function CreateDigitalServices() {
       // some custom logic here
        // Save to person
        Xrm.Page.getAttribute("vc_mappingsids").setValue("calculatedvalue");
        Xrm.Page.data.entity.addOnSave(timeOutAndRunGetAppContractID);
        Xrm.Page.data.entity.save();
    }
    
    
    function timeOutAndRunGetAppContractID()
    {
        setTimeout(getAppContractID(), 5000);
    }
    
    function getAppContractID() {
        var Id = Xrm.Page.data.entity.getId(); // /xrmservices/2011/OrganizationData.svc/vc_appcontractSet?$select=vc_AppID,vc_Error&$filter=vc_appcontractId eq guid'E8E19C13-C202-E511-89BC-005056B70A08'
        var oDataURI = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/ContactSet(guid'" + Id + "')?$select=vc_mappingsname,vc_mappingsids";
        try {
            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                url: oDataURI, //Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/IncidentSet(guid'" + id + "')",
                async: true,
                beforeSend: function (XMLHttpRequest) {
                    //Specifying this header ensures that the results will be returned as JSON.
                    XMLHttpRequest.setRequestHeader("Accept", "application/json");
                },
                success: function (data, textStatus, XmlHttpRequest) {
                    if (data.d.vc_mappingsids == null) {
                        if (data.d.vc_mappingsname != null) {
                            alert("This person is already having " + data.d.vc_mappingsname);
                            var updateURI = Xrm.Page.context.getClientUrl()
                        + "/XRMServices/2011/OrganizationData.svc/"
                        + "ContactSet(guid'" + Id + "')";
                            $.ajax({
                                type: "POST",
                                async: false,
                                contentType: "application/json;charset=utf-8",
                                datatype: "json",
                                data: '{ "vc_mappingsname": "" }',
                                url: updateURI,
                                beforeSend: function (XMLHttpRequest) {
                                    //Specifying this header ensures that the results will be returned as JSON.
                                    XMLHttpRequest.setRequestHeader("Accept", "application/json");
                                    //Specify the HTTP method MERGE to update just the changes you are submitting.
                                    XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
                                },
                                success: function (data, textStatus, XmlHttpRequest) {
                                    
                                },
                                error: function (XmlHttpRequest, textStatus, errorThrown) {
                                    
                                }
                            });
                        }
                        addRefreshCount = 1;
                        subSrid.refresh();
                        return;
                    }
                    else if (addRefreshCount == 5) {
                        addRefreshCount = 1;
                        return;
                    }
                    else {
                        addRefreshCount++;
                        setTimeout(getAppContractID(), 5000);
                    }
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    if (XmlHttpRequest && XmlHttpRequest.responseText) {
                        subSrid.refresh();  //alert("Error while updating " + odataSetName + " ; Error – " + XmlHttpRequest.responseText);
                    }
                }
            });
        } catch (ex) {
    	
    Xrm.Page.data.entity.removeOnSave(timeOutAndRunGetAppContractID);
            console.log(ex.toString());
        }
    Xrm.Page.data.entity.removeOnSave(timeOutAndRunGetAppContractID);
    }

    Halldór Jóhannsson



    Thursday, June 4, 2015 9:28 AM
  • If what I suggested previously fails then try looking into using the refresh function however both .refresh and .then only work on Updated Entities.

    https://msdn.microsoft.com/en-us/library/41462684-3e5d-4858-8be4-1a7c4fcdeff6(v=crm.7)

    Xrm.Page.data.refresh(save).then(successCallback, errorCallback);
    // Parameters
    // save => A Boolean value to indicate if data should be saved after it is refreshed.
    // successCallback => A function to call when the operation succeed

    // errorCallbak => A function to call when the operation fails. It gets called with 2 parameters (an error code and a localized error message)


    Halldór Jóhannsson

    • Proposed as answer by MaheshBB Friday, June 5, 2015 12:41 PM
    Thursday, June 4, 2015 9:41 AM