Hi,
We are using a JavaScript function on form field onchange event to detect the duplicate entries in the entity and alerting the users.
This script works perfectly but considering only records based on role. If admin user checks in the entire organization records whereas specific BU user checks the records only specific to the BU.
Problem is the if lead record is already available in other BU cant be notified to the BU users as the role restricts the records to specific BU. Anyway to override without compromising on the security roles?
My script is :
function CaptureDuplicates_email_onchange(eventArgs) {
var email = Xrm.Page.getAttribute("emailaddress1");
var isNull = email.getValue() == null;
var isBlank = email.getValue() == "";
var notificationMessage = "Error";
if (isNull || isBlank) {
Xrm.Page.ui.setFormNotification("To save this record, please enter either an Email address, Main Phone or Mobile number.", "ERROR", notificationMessage);
eventArgs.returnValue = false;
}
else {
Xrm.Page.ui.clearFormNotification(notificationMessage);
}
if (email.getValue() != null) {
var emailvalue = Xrm.Page.getAttribute("emailaddress1").getValue();
var context = Xrm.Page.context;
var serverUrl = context.getClientUrl();
var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
var retrieveResult = new XMLHttpRequest();
retrieveResult.open("GET", ODataPath + "/LeadSet?$select=FullName&$filter=(EMailAddress1 eq '" + emailvalue + "')", false);
retrieveResult.setRequestHeader("Accept", "application/json");
retrieveResult.setRequestHeader("Content-Type", "application/json; charset=utf-8?");
retrieveResult.send();
if (retrieveResult.readyState == 4 /* complete */) {
if (retrieveResult.status == 200) {
var retrieved = this.parent.JSON.parse(retrieveResult.responseText).d;
var Result = retrieved.results;
if (typeof Result !== "undefined") {
var count = Result.length;
if (count > 0) {
Xrm.Page.ui.setFormNotification("Duplicates found. Please enter a unique value for email.", "ERROR", notificationMessage);
alert("Duplicates found. Please enter a unique value for email");
email.setValue(null);
Xrm.Page.getControl("emailaddress1").setFocus();
}
}
}
}
}
}
Hi SRDogiparthy and Goutam,
Thanks for sharing WCF Service as solution.
Regards,
Clofly
Yes create a WCF service exposed as rest service and keep System Admin credentials in web configuration or in a secured CRM configuration entity. Call the WCF REST service onchange of the field and validate the count .
Thanks. I checked it.
But it is not suitable for our case as plugin will only update on server side. means that it will have to refresh before telling user that data is duplicate.
We do have jobs scheduled for doing similar jobs at server side.
I guess we only left with the alternative of creating a WCF Service to check with admin credential and use it within JavaScript.
Thanks again for your help.
Hi SRDogiparthy,
It would be unfortunate for us that the javascript version was only available since from crm 2016.
However, as the article below mentioned, it seems that impersonation has been supported in plug-in(backend) since crm 2011:
So you could run a custom workflow at organization level to achieve your requirement.
Regards,
Clofly
Yes, System Administrator role has complete access. However our suspect that may be because of the version (2013), impersonation doesn't work. Read somewhere that impersonation available and compatible in versions 2016 on wards.
To overcome this issue, I would suggest two options,
For duplicate detection logic across the board(OrgService, import, UI and WebAPI), I would follow one of the below options
Refer https://exptechsolutions.blogspot.com/2019/11/d365-ce-duplicate-detection-approach.html
Hi Partner,
1. Does your Sysadmin has organization level to read Lead entity? In other word, was your Sysadmin actually the System Administrator?
2. Try to set BU user id to MSCRMCallerID when you're logged as sysadmin to test reversely whether it could only retrieve available records under the user.
Regards,
Clofly
Thanks for quick response. I tried enabling BU user role with "Act on Behalf of another User" and included the header into the JavaScript with Sysadmin ID. retrieveResult.setRequestHeader("MSCRMCallerID", "D2B02ADA-01B0-E311-93F7-F0921C122FA1");
But still doesnt work, no effect.
Hi,
The client side queries will use the existing user's privileges, so the query cannot find any lead that the current user doesn't have privilege to read.
If you need to do that on the client side, it would be better to create a WCF service to check with admin credential. You can call the WCF method in jscript and show the form notification.
Regards,
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156