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
RishuRishu 

Trigger when stage is changed

I need to write custom logic  to throw an error if - 
Field 1 is blank when
stage is updated from stage 1 to stage 2 and field 2 type = A or B or C 

I know I can achieve this using validation rule but I need to write custom logic for this
Validation rule for the above logic is :-
ISPICKVAL(PRIORVALUE (Stage__c), 'previousStage'),
ISPICKVAL(Stage__c, ' currentStage'),
ISBLANK(field1))
CharuDuttCharuDutt
Hii Rishu 
Try Below Validation
AND(ISBLANK(field_1__c),ISPICKVAL(PRIORVALUE(STAGE),'STAGE 1'), ISPICKVAL(STAGE,'STAGE 2') ,OR(ISPICKVAL(Field2,'A'),ISPICKVAL(Field2,'B'),ISPICKVAL(Field2,'C')))
Please Mark It As Best Answer If It Helps
Thank You!

 
RishuRishu
I have to achieve above rule using trigger not through validation rule
CharuDuttCharuDutt
Hii Rishu
Try Below Code
trigger opptrigger on opportunity(Before Update){
    for(Opportunity opp:trigger.new){
       if(opp.StageName!=Trigger.oldMap.get(opp.Id).StageName){
           if(opp.field1__c == null && 
              (opp.field2__c == 'A' || opp.field2__c == 'B' || opp.field2__c == 'C') && 
              opp.stage == ' Stage 2' && 
              Trigger.oldMap.get(opp.Id).StageName == 'stage 1'){
                  opp.AddError('Error');
    }
}
Please Mark It As Best Answer If It Helps
Thank You!