Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Assign a record to the current user (user does not have write privileges) via javascript

(0) ShareShare
ReportReport
Posted on by 1,004

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

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Assign a record to the current user (user does not have write privileges) via javascript

    Hi Sachith,

    Ideally impersonation doesnt work well on the client side scripting also it puts your CRM on security risk.

    the ideal way of doing is to call and action using javascript and register a plugin and do the impersonation there post which you can perform assignment or any other action you wish to.

  • Suggested answer
    RE: Assign a record to the current user (user does not have write privileges) via javascript
     req.setRequestHeader("MSCRMCallerID", impersonateUserId);
    why is this used?
  • RE: Assign a record to the current user (user does not have write privileges) via javascript

    Act on behalf privilege?

    Impersonation involves two different user accounts: one user account (A) is used when executing code to perform some task on behalf of another user (B).

    User account (A) needs the prvActOnBehalfOfAnotherUser privilege, which is included in the Delegate security role.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

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,432 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans