Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Alert if Topic value is already existed when creating new opportunity using code

Posted on by 15

Hi,

I am a fresher, now I want to get all the existed topic value to compare with the new one created, if it has been existed, the system will display the error and can not create new opp. However, I don't know how to get the topic value from the system. How can I get by code?

Thanks.

  • Huong Gau Profile Picture
    Huong Gau 15 on at
    RE: Alert if Topic value is already existed when creating new opportunity using code

    That's really helpful

  • Verified answer
    Rakesh90 Profile Picture
    Rakesh90 200 on at
    RE: Alert if Topic value is already existed when creating new opportunity using code

    '$select' is used to specify which columns are needed in the results

    '$filter' is used to specify the filter condition to retrieve the results, which in your case should value entered by the user in the Topic field.

    If found useful, please mark the answer as verified

  • Huong Gau Profile Picture
    Huong Gau 15 on at
    RE: Alert if Topic value is already existed when creating new opportunity using code

    Thanks for your help, I can do it now. Can you explain what insides "?$select=name$filter=name eq '"? I don't get it clearly

  • Huong Gau Profile Picture
    Huong Gau 15 on at
    RE: Alert if Topic value is already existed when creating new opportunity using code

    Thanks for your help

  • Verified answer
    Rakesh90 Profile Picture
    Rakesh90 200 on at
    RE: Alert if Topic value is already existed when creating new opportunity using code

    Hi Huong,

    What you are trying to do is a duplicate detection check before an opportunity can be created in the system.

    There are 3 ways to do this. 

    1. Using the out of the book Duplicate detection settings.

    If you are an administrator, go to Settings -> Data management -> Duplicate detection rules. Create a new rule. Select 'Opportunity' in the primary and Matching entity types. Select Topic as the field and match type as 'Exact Match'. Save and publish this rule.

    Now whenever an opportunity is created, the user will get a Warning saying an opportunity with the same Topic already exists in the system. The user can opt to either navigate to the existing opportunity and complete their process. Or click on save and continue to save the opportunity with the duplicate Topic.

    2. Set 'Topic' as an alternate key.

    Go to Settings -> Customization -> Customize the system. In the default solution, go to Opportunity entity and click on 'Keys'. Create a new key and select 'Topic' and save. Please note that when creating an alternate key the system will create an indexing for the Topic column in the Opportunity table. PLEASE ENSURE THERE ARE NO DUPLICATES EXISTING IN THE SYSTEM before creating the alternate key. If not the creating process will fail. I normally Export the concerned column to an excel via advanced Find and check for duplicates. If duplicates exist, I will remove the value from the column and upload the excel back. This ensures there are no duplicates before I create the alternate key.

    Once an alternate key is set up for the Topic column, whenever a user tries to save an opportunity with the same Topic, the system will throw an error indicating that the Topic value has to be unique.

    3. On-change JavaScript

    This is done by creating a javascript web resource that checks for duplicates as soon as the Topic is entered by the user (ideal user experience but requires coding)

    Create a new JavaScript function with the below code. The function should trigger on change of the Topic field

    var topic = formContext.getAttribute("name").getValue();

    Xrm.WebApi.retrieveMultipleRecords("opportunity", "?$select=name$filter=name eq '" + topic + "'").then(

    function success(result) {

            if(result.entities.length > 0) {
                 formContext.getControl("name").setNotification("Please enter a unique value", "err1");
            },                  
        function (error) {
            console.log(error.message);
        }
    );

    Hope the above helps. 

    If found useful, please mark the answer as verified

  • Verified answer
    Nya Profile Picture
    Nya 29,056 on at
    RE: Alert if Topic value is already existed when creating new opportunity using code

    Hi,

    You may use the Alternate Key to restrict the creations of opportunities with existed topics.

    6354.pastedimage1629707758833v1.png

    After creating the key, when an above mentioned opportunity is created, an error message will be shown and the creation will be prohibited.

    8255.pastedimage1629708043373v2.png

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans