+ Start a Discussion
rahul Shukla 37rahul Shukla 37 

how write a trigger for opportunity stage cannot be move backward like if set qualification not move to previous stage Prospecting

How write a trigger for opportunity stage cannot be move backward like if set 'qualification' not move to previous stage 'Prospecting'
Best Answer chosen by rahul Shukla 37
Dhanya NDhanya N
Yes, you can do this by using map. You can assign values to map while defining it.
Something like below code:
 
public Map<String, Integer> stageValue = new Map<String, Integer> {'Prospecting' => 1,
        																'Qualification' => 2,
                                                                        'Needs Analysis'=> 3, 
                                                                        'Value Proposition'=> 4, 
                                                                        'Id. Decision Makers'=> 5, 
                                                                        'Perception Analysis' => 6, 
                                                                        'Proposal/Price Quote' => 7, 
                                                                        'Negotiation/Review' => 8, 
                                                                        'Closed Won' => 9, 
                                                                        'Closed Lost'=> 9};
    
 

        
    public void stageCannotbeMovedBackward(Map<Id, Opportunity> newMap, Map<Id, Opportunity> oldMap) {
        
        for(Opportunity opp : newMap.values()) {
            
            Opportunity oldOpp = oldMap.get(opp.Id);
            if(stageValue.containsKey(opp.stageName) &&  stageValue.containsKey(oldOpp.stageName)
               && stageValue.get(opp.stageName) < stageValue.get(oldOpp.stageName)) {
                opp.addError('Error : Stage can not be moved backward');
            }         
        }
        
    }

 

All Answers

Dhanya NDhanya N
Hi Rahul,

You can do this via validation rule. Here is the formula:
 
CASE(StageName, 
"Prospecting", 1, 
"Qualification", 2, 
"Needs Analysis", 3, 
"Value Proposition", 4, 
"Id. Decision Makers", 5, 
"Perception Analysis", 6, 
"Proposal/Price Quote", 7, 
"Negotiation/Review", 8, 
"Closed Won", 9, 
"Closed Lost", 10, 
0) 
< 
CASE(PRIORVALUE(StageName), 
"Prospecting", 1, 
"Qualification", 2, 
"Needs Analysis", 3, 
"Value Proposition", 4, 
"Id. Decision Makers", 5, 
"Perception Analysis", 6, 
"Proposal/Price Quote", 7, 
"Negotiation/Review", 8, 
"Closed Won", 9, 
"Closed Lost", 10, 
0)
Then set the required error message.

Thanks,
Dhanya
 
rahul Shukla 37rahul Shukla 37
Thanks Dhanya but this is i want to do this question through trigger by using map you have any idea about that.
Dhanya NDhanya N
Yes, you can do this by using map. You can assign values to map while defining it.
Something like below code:
 
public Map<String, Integer> stageValue = new Map<String, Integer> {'Prospecting' => 1,
        																'Qualification' => 2,
                                                                        'Needs Analysis'=> 3, 
                                                                        'Value Proposition'=> 4, 
                                                                        'Id. Decision Makers'=> 5, 
                                                                        'Perception Analysis' => 6, 
                                                                        'Proposal/Price Quote' => 7, 
                                                                        'Negotiation/Review' => 8, 
                                                                        'Closed Won' => 9, 
                                                                        'Closed Lost'=> 9};
    
 

        
    public void stageCannotbeMovedBackward(Map<Id, Opportunity> newMap, Map<Id, Opportunity> oldMap) {
        
        for(Opportunity opp : newMap.values()) {
            
            Opportunity oldOpp = oldMap.get(opp.Id);
            if(stageValue.containsKey(opp.stageName) &&  stageValue.containsKey(oldOpp.stageName)
               && stageValue.get(opp.stageName) < stageValue.get(oldOpp.stageName)) {
                opp.addError('Error : Stage can not be moved backward');
            }         
        }
        
    }

 
This was selected as the best answer
rahul Shukla 37rahul Shukla 37
Dhanya thanks alot to help me on this can you please let me know how to call this code in trigger because i am trying but it shows error.
Dhanya NDhanya N
Trigger should be on before update event. Create a instance to handler and then call handler method using the instance.
 
trigger OpportunityTrigger on Opportunity (before update) {
	
    OpportunityTriggerHandler handler = new OpportunityTriggerHandler();
    
    if(trigger.isBefore && trigger.isUpdate) {
        handler.stageCannotbeMovedBackward(trigger.newMap, trigger.oldMap);	
    }
}

Thanks,
Dhanya
Dhanya NDhanya N
I hope it helps you.
If so, please close your query by marking it as solved so that it can help others in the future. 
rahul Shukla 37rahul Shukla 37
Thank you so much Dhanya to help me.
rahul Shukla 37rahul Shukla 37
I have one custom object in which two field is available available start date and previous enroll if previous enroll is not null then Available start date will be read only if null then it should be editable please help me to achieve this scenario