Skip to main content
Dynamics 365 general forum
Suggested answer

Impersonation when retrieve records using JS

editSubscribe (1) ShareShare
ReportReport
Posted on by 2,139

Hi all!

As default any user in our system should see only contacts in his bussiness unit. If user tries to create a new record that already exists in database, for example with the same email address, he must receive an alert. The main problem of my task is that the existing record could be created by any user in the system i.e. user from another business user for example.

I have created a contact with my admin account and I'm making a request from "simple" account with impersonation using system admin Id. My code is below:

this.OnSave = function (executionContext) {
        let formContext = executionContext.getFormContext();
        let email1 = formContext.getAttribute("emailaddress1").getValue();
        inpersonateRetrieveCollectionReq(email1);
    }

    function inpersonateRetrieveCollectionReq(email) {
        let adminId = 'XXXXXXXXXXXXXXXXXX' /*CRMAdmin*/;
        try {

            let globalContext = Xrm.Utility.getGlobalContext();
            let req = new XMLHttpRequest();
            req.open("GET", globalContext.getClientUrl()   "/api/data/v9.0/contacts?$select=fullname,emailaddress1&$filter=emailaddress1 eq '"   email   "'", 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", adminId); //CRM Admin impersonation
            req.onreadystatechange = function () {
                if (this.readyState === 4) {
                    req.onreadystatechange = null;
                    if (this.status === 200) {
                        //Success - No Return Data - Do Something
                        let res = JSON.parse(req.responseText).value[0];
                        alert("Contact: "   res.fullname   " with email: "   res.emailaddress1   "already exists" );
                    } else {
                        //Xrm.Utility.alertDialog(this.statusText);
                        alert(this.statusText);
                    }
                }
            };
            req.send(null);
        }
        catch(e) {
            throw new Error(e);
        }
    }

My request does not retrieve the record created. The value part of the response is empty.

There is any error on my request? There is possible to use impersonation on getting records?

Attachments
  • betlejuice Profile Picture
    betlejuice 2,139 on at
    RE: Impersonation when retrieve records using JS

    Hello Mehdi! Thank you for your answer. My custom role has enable the privilege Act on Behalf of another anyway i cannot retrieve the existing record created by my system admin account.

  • Suggested answer
    meelamri Profile Picture
    meelamri 13,188 UG Leader on at
    RE: Impersonation when retrieve records using JS

    Hello Oscar,

    I just replayed the scenario on my environment. Yes, it's possible to use impersonation on getting records. However, there is a prerequisite to make it succeed. The impersonator needs the privilege Act on Behalf of Another User (prvActOnBehalfOfAnotherUser), which is included in the Delegate security role or can be enabled for any security role.

    pastedimage1615363448842v1.png

Helpful resources

Quick Links

New Blog Features Released!

Check out the new community blog features for viewers and authors…

Setting Up Knowledge Sources for Copilot…

Look at how configuring a comprehensive knowledge base is crucial…

Demystifying Copilot with Georg Glantschnig…

Industry experts answer burning questions directly from our amazing Community…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,126 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 222,646 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,138

Featured topics

Product updates

Dynamics 365 release plans