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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

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

(0) ShareShare
ReportReport
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.

I have the same question (0)
  • Verified answer
    Nya Profile Picture
    29,060 on at

    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

  • Verified answer
    Rakesh90 Profile Picture
    200 on at

    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

  • Huong Gau Profile Picture
    15 on at

    Thanks for your help

  • Huong Gau Profile Picture
    15 on at

    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

  • Verified answer
    Rakesh90 Profile Picture
    200 on at

    '$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
    15 on at

    That's really helpful

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 80

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans