Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

How do I retrieve the number of records in the 1:N relationship from Account to Opportunity (the customer one)? I want to limit the number of opportunities that can be created for a given account

(0) ShareShare
ReportReport
Posted on by

Has anyone written code to limit the number of records within a relationship? For instance, I want to limit the number of opportunities a user can create based on the account. So an account can only have 10 or so open opportunities at a time

  • Suggested answer
    Saad Kabarousse Profile Picture
    734 on at
    RE: How do I retrieve the number of records in the 1:N relationship from Account to Opportunity (the customer one)? I want to limit the number of opportunities that can be created for a given account

    Hello,

    You will have to call this function on your opportunity form, when you change the account field this function has to be called, it will check if the selected account has more than 10 opportunities:

    function checkNumberOfOpportunities()

    {

    var lookupObj = Xrm.Page.getAttribute(lookupSchemaName); //Check for Lookup Object

    if (lookupObj != null) {

    var lookupObjValue = lookupObj.getValue();//Check for Lookup Value

    if (lookupObjValue != null) {

    var lookupEntityType = lookupObjValue[0].entityType, //To get EntityName

    lookupRecordGuid = lookupObjValue[0].id, // To get record GUID

    $.ajax({

       type: "GET",

       contentType: "application/json; charset=utf-8",

       datatype: "json",

       url: Xrm.Page.context.getClientUrl() + "/api/data/v9.0/opportunities?$select=_accountid_value&$filter=_accountid_value eq "+lookupRecordGuid+"",

       beforeSend: function(XMLHttpRequest) {

           XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");

           XMLHttpRequest.setRequestHeader("OData-Version", "4.0");

           XMLHttpRequest.setRequestHeader("Accept", "application/json");

           XMLHttpRequest.setRequestHeader("Prefer", "odata.include-annotations=\"*\",odata.maxpagesize=10");

       },

       async: true,

       success: function(data, textStatus, xhr) {

           var results = data;

           for (var i = 0; i < results.value.length; i++) {

              if(results.value.length >= 10)  Xrm.Utility.alertDialog("Account already has 10Opportunities");

           }

       },

       error: function(xhr, textStatus, errorThrown) {

           Xrm.Utility.alertDialog(textStatus + " " + errorThrown);

       }

    });

    }

  • Suggested answer
    - Matt Bayes - Profile Picture
    890 on at
    RE: How do I retrieve the number of records in the 1:N relationship from Account to Opportunity (the customer one)? I want to limit the number of opportunities that can be created for a given account

    Hi Melanie,

    From the sound of it, you only have three options here:

    1. Write a plugin that will trigger on the pre-create message, which will retrieve the count of the related records and validate against the number set. Once validated, if the count is less than that of the max, then the process will proceed, else you will throw an error to the user alerting them that they cannot proceed.
    2. Write a custom workflow activity / action (plugin attached) that will retrieve the number of records in a workflow based on the relationship passed in, and from that Output Parameter you can determine if it falls below the max, and if not, you can add a "Stop Workflow" step in with a message for the user.
    3. Write custom javascript code that will run when a new record is being opened or when it is being saved. The code will retrieve the related records via a WebAPI call (asynchronous) and validate against the max count, at which point the user can be prompted with a message that will not allow them to proceed.

    Unfortunately there's no way to achieve this functionality out of the box, as 1:N relationships cannot be retrieved or interacted with in workflows without some sort of customization.

    If you need assistance or some direction, please let me know and I can give you a starting point for whichever option seems most reasonable for your situation.

    Thank you!

    Matt Bayes

     

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 106

#3
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 82 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans