Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Impersonation when retrieve records using JS

(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?

  • betlejuice Profile Picture
    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
    13,212 User Group 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

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... 293,278 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,983 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans