0

I have one validation rule which make all the field read only except one field Stage, when stage is New This validation rule is working fine for all the scenarios but failing in one condition.

Whenever user is changing the other fields value with stage field then the validation rule is not working. In my case validation rule should not work only when stage field is changing.

AND( ISPICKVAL(PRIORVALUE(Stage__c), "New"),NOT(NEW()),NOT(ISCHANGED(TEXT(Stage__c))))
0

I think you may need to write trigger to handle this scenario as follows:

So, create a list of FieldAPIs excluding StageName and verify if any other field values have been modified.

trigger OpportunityTrigger on Opportunity(before insert, before update) 
{
    List<String> lstFieldApi = new List<String>();

    if(Trigger.isUpdate && Trigger.isBefore)
    {
        //create a field map which can be updateable
        Schema.DescribeSObjectResult desResult = Opportunity.sobjectType.getDescribe();
        Map<String,Schema.SObjectField> fieldMap = desResult.fields.getMap();
        for(String fieldStr:fieldMap.keySet())
        {   
            Schema.DescribeFieldResult dfr = fieldMap.get(fieldStr).getDescribe();
            if(dfr.isUpdateable() && fieldStr != 'StageName')
            {
                lstFieldApi.add(fieldStr);
            }
        }

        for(Opportunity opptyObj:Trigger.new)
        {
            for(String strFieldPI:lstFieldApi)
            {
                if(opptyObj.get(strFieldPI) != Trigger.oldMap.get(strFieldPI))
                {
                    opptyObj.addError('Only Stage can be modified');
                }
            }
        }
    }
}   
  • 1
    perhaps a more generalized solution would be to define a fieldset of either whitelist or blacklist fields and then logic can evolve without code change – cropredy Sep 9 '17 at 21:57
  • Thats a good idea – Santanu Boral Sep 9 '17 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.