locked
Can you update another entity from one entity using Javascript? RRS feed

  • Question

  • In the onLoad of an Order entity, I need to update an Enquiry entity with today's date. The relevant Enquiry is a lookup on the Order form, so I can easily get the Guid for the Enquiry that needs to be updated. However, I have no idea whatsoever how to update another record using javascript! Is it possible?

    Thanks.

    Thursday, April 10, 2014 10:08 AM

All replies

  • Hi

    Have look at this blog, it shows you the method and JavaScript code to update an Entity  >>>  http://lakshmanindian.wordpress.com/2012/10/11/update-record-using-jscript-in-microsoft-dynamics-crm-2011/


    Eric UNG [Senior Analyst Programmer :: Sydney, Australia]

    Thursday, April 10, 2014 10:39 AM
  • Thank you for your reply. I found that blog, and used that example, but had to tweak it a litle to suit my own purposes. However, it's still not working. I'm getting a 'Bad request' message.

    Here's my code:

    function UpdateLastValidatedDateOnEnquiry()
    {
    
        var enquirylookupid;
        var enquirylookup = new Array();
        enquirylookup = Xrm.Page.getAttribute("new_enquiryid").getValue();
    
        if (enquirylookup != null) {
    
            enquirylookupid = enquirylookup[0].id;
         }
    
        var objEnquiry = new Object();
        // set the name of enquiry
        objEnquiry.Name = "Enquiry Updated from jscript";
    
        // set the Enquiry Id
        objEnquiry.new_enquiryId = { Id: enquirylookupid };	
    	
         // set the Last Validated Date field
         objEnquiry.new_LastValidatedDate = { Value: new Date() };
        
        // Parse the entity object into JSON 
        var jsonEntity = window.JSON.stringify(objEnquiry);
    
        var serverUrl = Xrm.Page.context.getServerUrl();
        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc/new_enquirySet";
        var ODataPath = serverUrl + ODATA_ENDPOINT;
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: ODataPath + "(guid'" + enquirylookupid + "')",
            data: jsonEntity,
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
                XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
            },
            error: function (xmlHttpRequest, textStatus, errorThrown) {
                alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
            }
        });
    }
    

    Thursday, April 10, 2014 10:42 AM
  • Following is some code I used for my application.

    it works for me, I am creating record here. you need to add related libraries to use this method.

    function createAccount(mTransaction,mTransactionId,price) {
     var parentId = Xrm.Page.data.entity.getId();
     SDK.JQuery.retrieveRecord(
         parentId,
         "apn_mLine",
         null,null,
         function (parentline) {
          SDK.JQuery.createRecord({apn_name:mTransaction.apn_name,apn_Description:{Value:803080001},apn_GL:mTransaction.apn_GL,apn_User:mTransaction.apn_User,apn_InsertDate:new Date().toLocaleString()},
    		"apn_mTransactions",
    		function (childmTransaction) {
    		 // Associate the accounts that were created.
    		 associateAccounts(parentApline, childmTransaction);
    		},
    	 errorHandler);
         },
    	 errorHandler);
     }




    Thanks Regards Prashanth Kamasamudram Even the least work done for others awakens the power within; even thinking the least good of others gradually instills into the heart the strength of a lion.




    Thursday, April 10, 2014 12:06 PM
  • Can I suggest you remove the following lines from your code and try again.

        var objEnquiry = new Object();
       
    // set the name of enquiry
        objEnquiry
    .Name = "Enquiry Updated from jscript";

       
    // set the Enquiry Id
        objEnquiry
    .new_enquiryId = { Id: enquirylookupid };


    Eric UNG [Senior Analyst Programmer :: Sydney, Australia]

    Thursday, April 10, 2014 12:07 PM
  • Thanks, although that didn't work.

    I removed all the lines as you suggested, and got an error saying that objEnquiry was undefined. So I left in the line where that was declared:

    var objEnquiry = new Object

    Then tried again, this time I got en error saying 'Internal Server error'

    Thursday, April 10, 2014 12:14 PM
  • Hi, I have tested the revised attached code and it works OK in my system.

    Please note that new_LastValidatedDate and new_enquirySet is case sensitive so make sure they match the schema name of your system.

    function UpdateLastValidatedDateOnEnquiry()
    {
    
        var enquirylookupid;
        var enquirylookup = new Array();
        enquirylookup = Xrm.Page.getAttribute("new_enquiryid").getValue();
    
        if (enquirylookup != null) {
    
            enquirylookupid = enquirylookup[0].id;
         }
    
        var objEnquiry = new Object();
    	
         // set the Last Validated Date field
    	 var date = new Date();
         objEnquiry.new_LastValidatedDate = date;
        
        // Parse the entity object into JSON 
        var jsonEntity = window.JSON.stringify(objEnquiry);
    
        var serverUrl = Xrm.Page.context.getServerUrl();
        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc/new_enquirySet";
        var ODataPath = serverUrl + ODATA_ENDPOINT;
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: ODataPath + "(guid'" + enquirylookupid + "')",
            data: jsonEntity,
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
                XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
            },
            error: function (xmlHttpRequest, textStatus, errorThrown) {
                alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
            }
        });
    }


    Eric UNG [Senior Analyst Programmer :: Sydney, Australia]

    Friday, April 11, 2014 12:59 AM
  • Thanks. I used your code, but I'm still getting an internal server error thrown - it comes up in the alert message box.

    Wednesday, April 16, 2014 10:46 AM
  • This is the code I'm using, I'm just trying to access updateRecord in the 2013 SDK. I have JQUery added as a webresource with a name of new_JQuery.

    I got it from the following location:

    D:\2013 sdk\SDK\SampleCode\JS\RESTEndpoint\JQueryRESTDataOperations\JQueryRESTDataOperations\Scripts

    I keep getting an error that updateRecord is undefined, but if I open the SDK.JQuery.js file, I can see the updateRecord function is there.

    If I try calling retrieveRecord from the same file, I don't get any error.

    function UpdateLastValidatedDateOnEnquiry()
    {
    
        var enquirylookupid;
        var enquirylookup = new Array();
        enquirylookup = Xrm.Page.getAttribute("new_enquiryid").getValue();
    
        if (enquirylookup != null) {
    
            enquirylookupid = enquirylookup[0].id;
         }
    
        var objEnquiry = new Object();
    	// set the Last Validated Date field
    	var date = new Date();
    	objEnquiry.new_LastValidatedDate = date;
    	
         updateRecord(
         enquirylookupid,
         objEnquiry,
         "new_enquirySet",
         function () {
          alert("The enquiry record changes were saved");
    	  updateCRMObjectCompleted();      
         },
         errorHandler
       );
     
    }

    I have the following files included as webresources, in the following order:

    jquery.1.7.2.min.js

    jquery1.4.1vsdoc.js

    SDK.JQuery.js

    orders.js (my form code)

    FetchUtil.js

    json2.js

    Thanks.

    Wednesday, April 16, 2014 4:55 PM
  • I have this working now, but only when I attempt to update a text field on the other entity. When I try to update the date field I need to update, it causes an error. So I need to know how to pass in a date value to the updateRecord function.

    Here's my code, I've tried many different ways of passing a date value in:

    function UpdateLastValidatedDateOnEnquiry()
    {
        var enquirylookupid;
        var enquirylookup = new Array();
        enquirylookup = Xrm.Page.getAttribute("new_enquiryid").getValue();
    
        if (enquirylookup != null) {
    
            enquirylookupid = enquirylookup[0].id;
         }
    enquirylookupid = enquirylookupid.replace('{', '').replace('}', '');
    
    
        var objEnquiry = {}; 
    	// set the Last Validated Date field
    	var today = new Date();
    	
    	//today.setDate(new Date());
    
    	objEnquiry.new_LastValidatedDate = today; 
    
    //var myDate = new Date();
    
    //myDate.setFullYear(1980, 12, 29);
    
    //objEnquiry.new_LastValidatedDate= myDate;
    	 
         SDK.JQuery.updateRecord(
         enquirylookupid,
         objEnquiry,
         "new_enquiry",
         function () {
          alert("The enquiry record changes were saved");	       
         },
         errorHandler
       );
     
    }

    I'm REALLY stuck on this, so any help would be much appreciated!

    Thanks.


    Thursday, April 17, 2014 11:33 AM
  • Check the schema name of enquiry

    Is it new_enquiry or new_Enquiry?

    
    
    
    

    Regards Faisal

    Thursday, April 17, 2014 3:32 PM