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
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
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);
}
});
}
Hi Melanie,
From the sound of it, you only have three options here:
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
Daivat Vartak (v-9d...
225
Super User 2025 Season 1
Muhammad Shahzad Sh...
106
Vahid Ghafarpour
82
Super User 2025 Season 1