+ Start a Discussion
Andrew Hoban 6Andrew Hoban 6 

Having a limit on the amount of times a picklist values can be selected.

Hi all,

is there a way to have a limit on the amount of times a value in a picklist field can be selected? I have an object called Software_Assets__c and a vaule in a picklist called Microsoft Office. 

I want the Microsoft Office value to only be selected 10 times.

Many thanks
Best Answer chosen by Andrew Hoban 6
gokul bharatigokul bharati
Hi Andrew,

Please find the sample code,this might help you.

trigger stopOpportunityCreation on Opportunity(before insert,before update){
Integer count =[select count() from opportunity where LeadSource='Advertisement'];
for(Opportunity a:trigger.new){
if(count==10&&a.LeadSource=='Advertisement'){
a.addError('Please chose some other pick-list Values');
}
}


 

All Answers

Swagato RaySwagato Ray
Hi Andrew,

you can create a field in Software_Assets__c object as integer.Let's say it counter__c
Then each time when user selects the picklist value is "Microsoft Office", then increment a counter and update the value of counter__c. 
If it reaches 10 then throw an error and stops the execution.

Cheers 
Swagata
Andrew Hoban 6Andrew Hoban 6
Thanks for your reply. Would this work for individual records? So once 10 records saved that have the value Microsoft office the 11th record will bring up the error?
Thanks
gokul bharatigokul bharati
Hi Andrew,

Please find the sample code,this might help you.

trigger stopOpportunityCreation on Opportunity(before insert,before update){
Integer count =[select count() from opportunity where LeadSource='Advertisement'];
for(Opportunity a:trigger.new){
if(count==10&&a.LeadSource=='Advertisement'){
a.addError('Please chose some other pick-list Values');
}
}


 
This was selected as the best answer