locked
CRM 2013 JScript Update Contact Record RRS feed

  • Question

  • Respected Community Members,

     I have a requirement to Update Contact Field from JScript in CRM2013 where I am calling below function, but it doesn't work. The error says:- " SDK.JQuery.updateRecord requires the id parameter".

    the alert displays the ContactId as GUID correctly. I also uploaded JSON2/JQuery1.4.1/SDK.JQuery.js and added that library into Contact form library.

    Am I missing something here?? Please assist.

    function updateContact(ContactId) {
    try {
     var contact = {};
    var customefieldvalue = new Date().toDateString();
    contact.customefield = customefieldvalue ;
    alert(contact);
    alert(ContactId);
    alert(customefieldValue);
     
     SDK.JQuery.updateRecord(
         ContactId,
         contact,
         "contact",
         function () {
          writeMessage("The contact record changes were saved");
          deleteContact(ContactId);
         },
         errorHandler
       );
    
     } catch (ex) { alert('JavaScript Error In updateContact:' + ex.description); }
    }
    
    function deleteContact(ContactId){
    alert('delete contact');
    }
    
    function errorHandler(error) {
     writeMessage(error.message);
    }
    
    //Helper function to write data to this page:
    function writeMessage(message) {
     $("<li>" + message + "</li>").appendTo(output);
    }
    
    
    
    
    
    


    Monday, April 21, 2014 2:11 PM

Answers

  • For starters, you need to reference the entity by Schema Name. So the lowercase "contact" should be "Contact"

    SDK.JQuery.updateRecord(
         ContactId,
         contact,
         "Contact",
         function () {
          writeMessage("The contact record changes were saved");
          deleteContact(ContactId);
         },
         errorHandler
       );

    I'm assuming your alert on the ContactId displays a valid CRM Contact record Id...


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Monday, April 21, 2014 5:09 PM
    Moderator
  • Jason,

    Thanks you were right. I tried the uppercase and changed the contactID input using Xrm.Page.data.entity.getId(); method and it did work. After changing using JScript i used Xrm.Page.data.refresh(); method and it did refresh the data correctly onto page as well. The only thing i had to make sure was to use the SchemaName(Which is usually Uppercase/LowerCase ) instead of its Name/DisplayName.

    function updateContact(ContactId) {
    try {
     var contact = new Object();
    var customefieldvalue = new Date().toDateString();
    contact.CustomField = customefieldvalue ;
    Var ContactIDString = Xrm.Page.data.entity.getId();
     SDK.JQuery.updateRecord(
         ContactIDString,
         contact,
         "Contact",
         function () {
          writeMessage("The contact record changes were saved");
          deleteContact(ContactId);
         },
         errorHandler
       );
    
     } catch (ex) { alert('JavaScript Error In updateContact:' + ex.description); }
    }
    
    function deleteContact(ContactId){
    alert('delete contact');
    Xrm.Page.data.refresh();
    }
    
    function errorHandler(error) {
     writeMessage(error.message);
    }
    
    //Helper function to write data to this page:
    function writeMessage(message) {
     $("<li>" + message + "</li>").appendTo(output);
    }
    
    

    Thanks,

    Nick

    • Marked as answer by Nicksoft2011 Tuesday, April 22, 2014 1:38 PM
    Tuesday, April 22, 2014 1:37 PM

All replies

  • For starters, you need to reference the entity by Schema Name. So the lowercase "contact" should be "Contact"

    SDK.JQuery.updateRecord(
         ContactId,
         contact,
         "Contact",
         function () {
          writeMessage("The contact record changes were saved");
          deleteContact(ContactId);
         },
         errorHandler
       );

    I'm assuming your alert on the ContactId displays a valid CRM Contact record Id...


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Monday, April 21, 2014 5:09 PM
    Moderator
  • Jason,

            Thanks for the update. I tried lower case and upper case, but still same error.

            The alert message  display valid CRM Contact ID with brackets {....}. so I tried hard coded string without bracket, but it error out completely and it doesn't work at all.

            Do I need to set up any thing in CRM, to have this working?? or do I need to reference other scripts into this JScript file??

             Please assist.

    Thanks,

    Nick

    Monday, April 21, 2014 6:12 PM
  • Jason,

    Thanks you were right. I tried the uppercase and changed the contactID input using Xrm.Page.data.entity.getId(); method and it did work. After changing using JScript i used Xrm.Page.data.refresh(); method and it did refresh the data correctly onto page as well. The only thing i had to make sure was to use the SchemaName(Which is usually Uppercase/LowerCase ) instead of its Name/DisplayName.

    function updateContact(ContactId) {
    try {
     var contact = new Object();
    var customefieldvalue = new Date().toDateString();
    contact.CustomField = customefieldvalue ;
    Var ContactIDString = Xrm.Page.data.entity.getId();
     SDK.JQuery.updateRecord(
         ContactIDString,
         contact,
         "Contact",
         function () {
          writeMessage("The contact record changes were saved");
          deleteContact(ContactId);
         },
         errorHandler
       );
    
     } catch (ex) { alert('JavaScript Error In updateContact:' + ex.description); }
    }
    
    function deleteContact(ContactId){
    alert('delete contact');
    Xrm.Page.data.refresh();
    }
    
    function errorHandler(error) {
     writeMessage(error.message);
    }
    
    //Helper function to write data to this page:
    function writeMessage(message) {
     $("<li>" + message + "</li>").appendTo(output);
    }
    
    

    Thanks,

    Nick

    • Marked as answer by Nicksoft2011 Tuesday, April 22, 2014 1:38 PM
    Tuesday, April 22, 2014 1:37 PM