+ Start a Discussion
Ajay Kumar 583Ajay Kumar 583 

Please help how to achieve this?

Hi,
I dont want to allow any change in Opportunity where the Stage = “Closed Won” or “Closed Lost”. 
Show an error message on the “Opportunity Stage” field of standard page “Since the Opportunity has been Closed, no change is allowed on record”.

Thanks,
Aj
Ajay K DubediAjay K Dubedi
Hi Ajay,

Use bellow code for showing error when stage name is closed won or closed lost

Trigger :
trigger TriggerOnOportunity on Opportunity (before update) {
    if(trigger.isBefore && trigger.isUpdate)
        {
            TriggerController.updateOppMethod(trigger.new);

        }

}

Apex Controller
public class TriggerController {
    public static void updateOppMethod(List<Opportunity> oppList){
        if(oppList.get(0).StageName=='Closed Lost')
        {
            List<Opportunity> NewOppList=new List<Opportunity>();
            
            NewOppList=[select Id,name,StageName from Opportunity where Id in: oppList];
            if(ollList.size()<=0)
            {
                for(Opportunity oppInst:NewOppList)
                {
                    if(oppInst.StageName == 'Closed Won' || oppInst.StageName == 'Closed Lost'){
                        oppInst.addError('No change is allowed on record');
                    }
                    
                }
            }
        }
    }
    
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Ajay Kumar 583Ajay Kumar 583
Hi Ajay... 
There is an error in the code where oppList it is => if(ollList.size()<=0) 

Even i clear this the trigger is not working as per the logic
Ajay Kumar 583Ajay Kumar 583
Hi Ajay,

Error Throws as  Below :

TriggerOnOportunity4: execution of BeforeUpdate caused by: System.FinalException: SObject row does not allow errors Class.TriggerController4.updateOppMethod: line 13, column 1 Trigger.TriggerOnOportunity4: line 4, column 1
Deepali KulshresthaDeepali Kulshrestha
Hi Ajay,

You can try below code, it is working according to your requirement.
Trigger:-
trigger YA on Opportunity(before update,before insert){

    if(Trigger.isInsert || trigger.isUpdate){
        System.debug('Inside isUpdate');
        opportunityController.firstMethod(Trigger.new);
    }
}

Controller:-

public class opportunityController{
    public static void firstMethod(List<Opportunity> opportunities){
        if(!opportunities.isEmpty()){
            for (Opportunity opp: opportunities){
                if((opp.StageName == 'Closed Won' || opp.StageName == 'Closed Lost' ) && (opp.Amount >= 500000)  ){
                    opp.addError('Your Amount is greater than 500000');
                }
            }
        }
    }
    }

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,

Deepali Kulshrestha.

 
Ajay Kumar 583Ajay Kumar 583
Hi Deepali,

This one is different query which is achieved through Validation Rule. Please go through the query once.
Thanks,