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?