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 :
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

I have the same question (0)
  • Suggested answer
    - Matt Bayes - Profile Picture
    890 on at

    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

     

  • Suggested answer
    Saad Kabarousse Profile Picture
    734 on at

    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);

       }

    });

    }

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 72 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 29 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans