Update Record in Dynamics 365 using jQuery

Leave a comment

To update a record using jQuery, use the following code. You will need to pass the GUID of the record you are updating as well as the type of entity. This example updates an account record name:

function updateAccount() {

 var context = Xrm.Page.context;
 var serverUrl = context.getClientUrl();
 var guid = Xrm.Page.data.entity.getId();
 var endpoint = "/XRMServices/2011/OrganizationData.svc";

 var account = new Object();
 var collection = "/AccountSet";
 account.Name = "New Updated Name";

 var jsonEntity = window.JSON.stringify(account);

 if (typeof($) === 'undefined') {
 $ = parent.$;
 jQuery = parent.jQuery;
 }

 $.ajax({
 type: "POST",
 contentType: "application/json; charset=utf-8",
 datatype: "json",
 url: serverUrl + endpoint + collection + "(guid'" + guid + "')",
 data: jsonEntity,
 beforeSend: function(XMLHttpRequest) {
 XMLHttpRequest.setRequestHeader("Accept", "application/json");
 XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
 },

 success: function(data, textStatus, XmlHttpRequest) {
 alert("success");
 },

 error: function(XMLHttpRequest, textStatus, errorThrown) {
 alert("failure");
 }
 });
}

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

Your email address will not be published. Required fields are marked *