Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

update a record using POST Odata calls

(0) ShareShare
ReportReport
Posted on by 95

I am working on a scenario where i want to update a record some other entity using POST method OData calls javascript.

Below is my code :

function SetAverageSalitaRating() {
try {
debugger;
if (Xrm.Page.getAttribute("sal_interpreter").getValue()) {

var interpreterId = Xrm.Page.getAttribute("sal_interpreter").getValue()[0].id;

interpreterId = interpreterId.substring(1, interpreterId.length - 1);
var ServerUrl = Xrm.Page.context.getClientUrl();

var Entity_name1 = "/api/data/v8.1/sal_interpreterses(";
var Entity_name2 = ")";
odataselect1 = ServerUrl + Entity_name1 + interpreterId + Entity_name2;
var objAccount = new Object();
objAccount.sal_salitarating = 1;
objAccount.sal_address1visit = "vivek";
var jsonEntity = window.JSON.stringify(objAccount);
$.ajax({

type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: odataselect1,
data: jsonEntity,
async: false,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
alert("yeah");
},
success: function (data, textStatus, XmlHttpRequest) {
alert("Updated successfully");
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
}
});

}
}
catch (e) {
throw new Error(e.message);
}
}

Here my control is going inside 'success' method . Please help

*This post is locked for comments

  • Verified answer
    ganeshm Profile Picture
    ganeshm on at
    RE: update a record using POST Odata calls

    well 'post' is only used for create operations using web api.This link give a brief explanation on http methods on rest  ,

    www.restapitutorial.com/.../httpmethods.html.

  • Bobins Bansala Profile Picture
    Bobins Bansala 95 on at
    RE: update a record using POST Odata calls

    also can you please explain why we use PATCH and not POST

  • Suggested answer
    Bobins Bansala Profile Picture
    Bobins Bansala 95 on at
    RE: update a record using POST Odata calls

    thxns a lot

    type: "PATCH" solved my problem

  • Suggested answer
    ganeshm Profile Picture
    ganeshm on at
    RE: update a record using POST Odata calls

    If u have make update using Post then, try change ur odata_endpoint  to https://[orgname]/XRMServices/2011/OrganizationData.svc/(entity_name),

    check web api call type for crud operation.refer:

    msdn.microsoft.com/.../mt628816.aspx

  • Verified answer
    ganeshm Profile Picture
    ganeshm on at
    RE: update a record using POST Odata calls


    function set() {
    var accountId = Xrm.Page.data.entity.getId().slice(1,-1);

    var obj = new Object();
    // set the name of Account
    // objAccount={name:Account test update};
    obj.name="ttt";
    var jsonEntity = window.JSON.stringify(obj);

    var serverUrl = Xrm.Page.context.getClientUrl();
    var ODATA_ENDPOINT = "/api/data/v8.1/accounts";
    var ODataPath = serverUrl + ODATA_ENDPOINT;
    $.ajax({
    type: "PATCH",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    async: false,
    url: ODataPath + "(" + accountId + ")",
    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);
    }});}

    try using Patch operation for update not post

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,317 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans