Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum
Suggested answer

Impersonation when retrieve records using JS

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
    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,198 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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

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

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,524 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,493 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans