Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

Unable to get existing organization wide lead count

Posted on by Microsoft Employee

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
    cloflyMao 25,198 on at
    RE: Unable to get existing organization wide lead count

    Hi SRDogiparthy and Goutam,

    Thanks for sharing WCF Service as solution.

    Regards,

    Clofly

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Unable to get existing organization wide lead count

    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 .

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Unable to get existing organization wide lead 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.

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Unable to get existing organization wide lead count

    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
    Community Member Microsoft Employee on at
    RE: Unable to get existing organization wide lead count

    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.

  • ram r Profile Picture
    ram r on at
    RE: Unable to get existing organization wide lead count

    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

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Unable to get existing organization wide lead count

    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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Unable to get existing organization wide lead count

    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.

  • erhan.keskin Profile Picture
    erhan.keskin 2,247 on at
    RE: Unable to get existing organization wide lead count

    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,

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Unable to get existing organization wide lead count

    Hi,

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

    debajmecrm.com/.../

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans