web
You’re offline. This is a read only version of the page.
close
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?

I have the same question (0)
  • Suggested answer
    meelamri Profile Picture
    13,216 User Group Leader on at

    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

  • betlejuice Profile Picture
    2,139 on at

    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 45 Most Valuable Professional

#2
iampranjal Profile Picture

iampranjal 36

#3
Satyam Prakash Profile Picture

Satyam Prakash 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans