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
ashish raiashish rai 

How to get the Index of Picklist inside the formula???

Hi All,

     Is there any way to get the index of picklist inside the validation rule????  I have a picklist having value a,b,c,d . So if i have selected c as picklist value next time when i update this record with picklist value as a,b it will through an error since this value is less in indexing(for example index of a is 1,b is 2 , c is 3 and d is 4). I can update the value of pickist which having greter than equal index value then existing.

SidzSidz

Hi Ashish,

 

Am not really sure if this works but,

if the picklist values are alphabetically sorted,

have you tried directly using this condition

PRIORVALUE(picklist)>picklist then throw the validation error.

 

hope this helps ,

sidz

force_archforce_arch

To my knowledge, you cannot do this using a validation rule. You'll need a trigger!

 

if(trigger.isBefore && trigger.isUpdate){

      for(Object o:trigger.new{
            newValue = o.picklist__c;
            oldValue = Trigger.oldMap.get(o.Id).picklist__c;

            if(newValue<oldValue){

                  o.addError('You cannot change the value of picklist to a smaller value');

            }

       }

}

 

Hope this should help!!

 

ashish raiashish rai

Hi,

I don't want this should be sorted alphabeticaly. I want it should give me the index in that order in which i have written inside the picklist. Any idea on this?????

SidzSidz

Hi Ashish,

 

a little modification to the solution provided by force,

 

Schema.DescribeFieldResult fieldResult = YourObjectName.YourFieldName__c.getDescribe();
		List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
		Integer i=1;
		Map<Integer,String> mapStat=new Map<Integer,String>();
		for(Schema.PicklistEntry pp:ple)
			mapStat.put(i++,pp.getValue());
if(trigger.isBefore && trigger.isUpdate){
	for(Object o:trigger.new{
		newValue = o.picklist__c;
		oldValue = Trigger.oldMap.get(o.Id).picklist__c;
		

		if(mapStat.get(newValue)<mapStat.get(oldValue)){
			o.addError('You cannot change the value of picklist to a smaller value');
		}
	}
}

 

Hope this helps,

Sidz
(please mark this as the solution if it works for you )