web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Unable to get existing organization wide lead count

(0) ShareShare
ReportReport
Posted on by

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();

}
}
}
}
}
}

  • cloflyMao Profile Picture
    25,210 on at

    Hi Partner,

    There is a method called "impersonation" which allows user who has been assigned with "Act On Behalf Of Another User" privilege to be on behalf on another user.

    https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/impersonate-another-user-web-api

    https://yanivrdt.wordpress.com/2016/04/11/mscrm-2016-admin-utility-personal-views-manager/

    In your scenario, if you had assigned your current user's security role with this privilege,

    then by adding a header in your request,

    req.setRequestHeader("MSCRMCallerID", "systemadminId");

    the current user could retrieves all records under system admin while he could still only see records with existing privileges.

    But it seems that it couldn't work as BU or Team.

    You can find the prv in Business Manament tab:

    pastedimage1572950690823v1.png

    Regards,

    Clofly

     

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi,

    You can try to call API , impersonating different system admin user GUID.

    debajmecrm.com/.../

  • erhan.keskin Profile Picture
    2,253 on at

    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,

  • Community Member Profile Picture
    on at

    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.

  • cloflyMao Profile Picture
    25,210 on at

    Hi Partner,

    1. Does your Sysadmin has organization level to read Lead entity? In other word, was your Sysadmin actually the System Administrator?

    pastedimage1573005770201v1.png

    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

  • ram r Profile Picture
    on at

    To overcome this issue, I would suggest two options,

    1. You can use an impersonated WebAPI call
    2. Write an action that returns the count. A plugin will be wired to action and executed as user with elevated privileges or system user

    For duplicate detection logic across the board(OrgService, import, UI and WebAPI), I would follow one of the below options

    1. Duplicate detection rules => Remember users can override this. Applies to records that are accessible by the user
    2. Pre-Validation plugin/Custom workflow assembly => Gives more flexibility and custom error messages
    3. Alternate Keys => Always enforced except for blank/empty field

    Refer https://exptechsolutions.blogspot.com/2019/11/d365-ce-duplicate-detection-approach.html

  • Community Member Profile Picture
    on at

    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.

  • cloflyMao Profile Picture
    25,210 on at

    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:

    https://ashwaniashwin.wordpress.com/2013/09/15/crm-2011-plugin-to-show-alert-for-duplication-with-impersonation/

    So you could run a custom workflow at organization level to achieve your requirement.

    Regards,

    Clofly

  • Community Member Profile Picture
    on at

    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.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    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 .

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 93 Super User 2026 Season 2

#2
Travis South Profile Picture

Travis South 67

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans