Hi Guys,
I have a requirement to click a ribbon button on a custom entity and get the record assigned to the current user. (I don’t want to use the out of the box assign button as I need to do a bit more on this button click than just getting it assigned.) If the current user has write privileges to the record my code works. But if the record is read only for the current user my code does not work.
This is my code:
var currentRecordId = Xrm.Page.data.entity.getId().replace("{", '').replace("}", ''); var currentUserId = Xrm.Page.context.getUserId().replace("{", '').replace("}", ''); var entityName = "new_entityname"; var entity = {}; entity["ownerid@odata.bind"] = "/systemusers(" + currentUserId + ")"; var impersonateUserId = "A734DA29-7993-DE11-BECE-005056B47DB0";// GUID of a system administrator var req = new XMLHttpRequest(); req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_entitynames(" + currentRecordId + ")", false); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("MSCRMCallerID", impersonateUserId); req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 204) { //Success - No Return Data - Do Something } else { Xrm.Utility.alertDialog(this.statusText); } } }; req.send(JSON.stringify(entity));
*This post is locked for comments