Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Throw an error in Dynamics 365

Posted on by 1,075

Hello community experts,

I have a requirement where, if i try to enter a duplicate mail ID, that field should not allow me to enter the duplicate. I should throw an error, that this record already exist.

I tried using keys and duplicate detection rule.

Both does'nt meet this requirement.

Please Help.

Best Regards,

Sumaira Noor

  • Verified answer
    LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: Throw an error in Dynamics 365

    Hi Sumaira,

    Sorry for my mistake, in your  case we should use "retrievemultiplereocrds" to get data because "retrieveRecord" could only get an special record with GUID.

    So just use "retrievemultiplereocrds" instead, and which entity are you filling the emailaddress? According to your code, I think you might using contact. Then you need to replace the main entity "email" with "contact".

    function compareEmailID(executionContext){

       var formContext = executionContext.getFormContext();

       var emailID=formContext.getAttribute("emailaddress1").getValue();

       Xrm.WebApi.retrieveMultipleRecords("contact","?$select=emailaddress1&$filter=emailaddress1 eq '" + emailID+"'").then(

           function success(result) {

               if (result.length>0) {

                   Xrm.Page.ui.setFormNotification("This EmailID is exsited, please try another one!", "ERROR")

               }

           },

           function error(error) {

               Xrm.Navigation.openAlertDialog({ text: error.message });

           }

       );

    }

    docs.microsoft.com/.../retrievemultiplerecords

    Hope it helps.

    Best Regards,

    Leo

  • Sumaira Noor Profile Picture
    Sumaira Noor 1,075 on at
    RE: Throw an error in Dynamics 365

    Thanks for the response Leo.

    Here is the code.

    I have added it as a script webresource

    function compareEmailID(executionContext){

       var formContext = executionContext.getFormContext();

       var emailID=formContext.getAttribute("emailaddress1").getValue();

       Xrm.WebApi.retrieveRecord("email", "?$select=emailaddress1&$filter=emailaddress1 eq '" + emailID + "'").then(

           function success(result) {

               if (result.length>0) {

                   Xrm.Page.ui.setFormNotification("This EmailID is exsited, please try another one!", "ERROR")

               }

           },

           function error(error) {

               Xrm.Navigation.openAlertDialog({ text: error.message });

           }

       );

    }

  • LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: Throw an error in Dynamics 365

    Hi Sumaira,

    According to the error message, there should be something wrong in the  web api filter conditions. Could you kindly share your code to us and so that we could debug or check it for you.

    Best Regards,

    Leo

  • Sumaira Noor Profile Picture
    Sumaira Noor 1,075 on at
    RE: Throw an error in Dynamics 365

    Thanks for the response Leo

    I'm attaching there screenshot below.

    I have replaced the correct name

    ScriptErr.PNG

    Best Regards,

    Sumaira Noor

  • LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: Throw an error in Dynamics 365

    Hi partner,

    Please pay attention that you should replace the correct field name in your case, "emialid" is only a sample.

    If it is possible, please share the log file for more details.

    Best Regards,

    Leo

  • Sumaira Noor Profile Picture
    Sumaira Noor 1,075 on at
    RE: Throw an error in Dynamics 365

    Thanks a lot for your response Leo

    I'm getting this error

    One of the scripts for this record has caused an error. For more details, download the log file.

    Kindly guide

    Best regards,

    Sumaira Noor

  • Suggested answer
    Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: Throw an error in Dynamics 365

    Create a plugin and register it on create of respective entity.

    In this plugin retrieve record that has same email id , If record found then throw invalid pluginexecution exception.

  • Suggested answer
    LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: Throw an error in Dynamics 365

    Hi partner,

    First of all, we could not choose email as "base record type" in duplicate detection rule, so we could only use custom development.

    If the mail ID is a custom field not GUID, there are 2 ways to meet your requirement.

    1.Using JS code.

    You could create a js function the compare the email ID you've entered in the create form  with other existing emails and if this ID is existed, then throw an error or alert.

    function compareEmailID(executionContext){
        var formContext = executionContext.getFormContext();
        var emailID=formContext.getAttribute("emailid").getValue();
        Xrm.WebApi.retrieveRecord("email""?$select=emailid&$filter=emailid eq '" + emailID + "'").then(
            function success(result) {
                
                if (result.length>0) {
                    Xrm.Page.ui.setFormNotification("This EmailID is exsited, please try another one!""ERROR")
                }

            },
            function error(error) {
                Xrm.Navigation.openAlertDialog({ text: error.message });
            }
        );
    }

    2.Using Plug-in.

    You could refer to this doc with samples.

    https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/hh210213(v=crm.8)?redirectedfrom=MSDN

    https://www.inogic.com/blog/2015/11/use-retrieveduplicaterequest-in-dynamics-crm/

    Hope it helps.

    Best Regards,

    Leo

  • Sumaira Noor Profile Picture
    Sumaira Noor 1,075 on at
    RE: Throw an error in Dynamics 365

    Thanks for the response Lu

    Actually i tried many times the key feature is not working. It does'nt throw any error upon duplicate detection.

    Best Regards,

    Sumaira Noor

  • Verified answer
    LuHao Profile Picture
    LuHao 40,872 on at
    RE: Throw an error in Dynamics 365

    Hi Sumaira,

    Could you tell me why the alternate key and duplicate detection rules don't meet your needs?

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

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

Product updates

Dynamics 365 release plans