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
KS KumaarKS Kumaar 

Status scenario Trigger

Hi All,
I am new to SFDC. I want a trigger on an object. The developing scenario is given below.
The object is contains a custom pick list field called Status__c which contains pending, processing and Approved as a values.  After creating a record, if we want to change the status field value from approved to processing and pending, we need to get an error, as well as we need to get one more error while changing the status field value from processing to pending. But no error has to come, while value changing from pending to processing and approved, and processing to Approved.
Please can anyone provide the trigger with above scenario.

Thanks in advance
KS Kumaar
 
 
Best Answer chosen by KS Kumaar
SalesFORCE_enFORCErSalesFORCE_enFORCEr
trigger ThrowError on CustomObject__c (before update){
for(CustomObject__c cusObj: trigger.new){
if(cusObj.Status!=trigger.oldMap.get(cusObj.Id).Status__c && cusObj.Status=='Processing' && trigger.oldMap.get(cusObj.Id).Status__c=='Approved'){
cusObj.addError('You can't change the status');
}
} 
}

You can add more condition checks for other status values by putting || (OR)

All Answers

Marcilio SouzaMarcilio Souza
Hi, 

You'd like something like this.
 
trigger BloqStatus on CustomObject__c (before update) {
  if(Trigger.old[0].Status__c == 'Approved' &&
     Trigger.new[0].Status__c == 'Pending'){
	 
	 Trigger.new[0].addError('You Can't do this!!');
  }
}

You put condition and message error that you want.

Marcilio
SalesFORCE_enFORCErSalesFORCE_enFORCEr
trigger ThrowError on CustomObject__c (before update){
for(CustomObject__c cusObj: trigger.new){
if(cusObj.Status!=trigger.oldMap.get(cusObj.Id).Status__c && cusObj.Status=='Processing' && trigger.oldMap.get(cusObj.Id).Status__c=='Approved'){
cusObj.addError('You can't change the status');
}
} 
}

You can add more condition checks for other status values by putting || (OR)
This was selected as the best answer
KS KumaarKS Kumaar
Hi Marcilio Souza and Shalabh_enFORCEr,
Thank you for your help. Both trigger are working good.