function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Phil WitzmannPhil Witzmann 

Modify Picklist after a value is picked

Hi,

I have a picklist with 6 values (1, 2, 3, 4, 5, 6)

The default value is 1.

When someone chooses another value from that picklist (imagine 2), I would like that value 1 and 2 were not able to be picked again.

How can I do this?

Thanks
pconpcon
You would have to do this either in a trigger that pulls previous values and then does an addError if the value has already been used.  Or you can do it in a Visualforce page and build out a custom picklist that is backed by your picklist field and disable the ones that have already been chosen.
James LoghryJames Loghry
Phil,

Could you ellaborate more on your design?

Do you want the values to disappear for any record? E.g. If record A uses "1", then record B, record C, record D will not have 1 listed as an option?

To modify the picklist values you're looking at utilizing the metadata api, which is tricky (but doable).

I would suggest a different approach here.  Instead of utilizing an actual picklist field type, why not a separate sobject or custom setting?  That way you could populate the "picklist" via data loader or Apex.  This has the added benefit of deleting (or adding) picklist values through simple Apex triggers or Flows, where as with a Picklist field type you would need to bring a more complex Metadata API callout into the picture.
Vishal Negandhi 16Vishal Negandhi 16
If you mean that on a single record, no value should be repeated. Then you'll need a mechanism to store the value every time it is changed. It can be either in the form of a related list (child object - similar to Standard History object) or a field which stores all previous values in csv format. 

Now, when you implement a trigger you need to compare the current value with the values in this list (either a csv field or a related list) and throw an error if it's repeating.
Arun Deepan LJArun Deepan LJ
Hi, 
In the Before context of the trigger, perform a SOQL on the same object with the "Where" clause as selected picklist value. If the list size is greater than 1, through error using addError. 

It means, one picklist value can only be used only once. It cannot be used second time.

 
Phil WitzmannPhil Witzmann
Could you please tell me how to do the code? I am new into Salesforce have no idea how to start!
ManojjenaManojjena
Hi Phil Witzmann,

Try with below code it will help !!
Few things you need to modify to justify your requirment .
Change Opportunity to your object name .
Change StageName to your pickList field name .
Chnage the variable name accordigly .
 
trigger PickListCheck on Opportunity (before insert,before update ) {
    if(Trigger.isBefore){
		Map<String,Integer> oppStageWithRecCntMap=new Map<String,Integer>();
		AggregateResult[] groupedResults=[ SELECT StageName, COUNT(Id) cnt
		FROM Opportunity WHERE StageName != null
		GROUP BY ROLLUP(StageName)];
		if(!groupedResults.isEmpty()){
			for (AggregateResult ar : groupedResults) {
				oppStageWithRecCntMap.Put(String.valueOf(ar.get('StageName')),Integer.ValueOf(ar.get('cnt')));
			}
		}
		for(Opportunity opp : Trigger.new){
		 if(opp.StageName!= null && oppStageWithRecCntMap.get(opp.StageName)!= null ){
			if(oppStageWithRecCntMap.get(opp.StageName) >0){
			   opp.addError('This pickList value has one record already present');
			
			}
		 }
	  }
	}if(Trigger.isUpdate){
	    Set<Id> oppIdSet=new Set<Id>();
	    for(Opportunity opp : Trigger.new){
		   if(opp.StageName!= null && Trigger.oldMap(Opp.Id).StageName != opp.StageName){
		       oppIdSet.add(ópp.Id)
		   }
		}
		if(!oppIdSet.isEmpty()){
			Map<String,Integer> oppStageWithRecCntMap=new Map<String,Integer>();
			AggregateResult[] groupedResults=[ SELECT StageName, COUNT(Id) cnt
			FROM Opportunity WHERE StageName != null AND Id NOT IN :oppIdSet
			GROUP BY ROLLUP(StageName)];
			for (AggregateResult ar : groupedResults) {
				oppStageWithRecCntMap.Put(String.valueOf(ar.get('StageName')),Integer.ValueOf(ar.get('cnt')));
			}
		}
		for(Opportunity opp : Trigger.new){
			if(opp.StageName!= null && oppStageWithRecCntMap.get(opp.StageName)!= null ){
				if(oppStageWithRecCntMap.get(opp.StageName) >0){
				   opp.addError('This pickList value has one record already present');
				
				}
			}
	    }
		
	}
}

Let me know if it helps  !!
Thanks 
Manoj