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
Akash DeokarAkash Deokar 

before update trigger on opportunity is not working

I have written before update trigger on opportunity but it is not working for below code, not providing error when changing stage to verbal agreement.

Note: I have created one opportunity stage as verbal agreement(80%).

for(Opportunity opp:[Select id,name, Legal_Entity_Name__r.SAP_CODE__c,StageName from opportunity where (StageName='Verbal Agreement' and id in:trigger.new)]){
        if(opp.Legal_Entity_Name__r.SAP_CODE__c==null && opp.Legal_Entity_Name__r.Name==null){
            opp.addError('Oppotunity cannot move to 80% as SAP CODE or LE is not present');


For below code there is error occured as mentioned in code but error is getting for all the stages instead of verbal agreement.

trigger SAP_CODE on Opportunity (before Update) {
    
    Set<Opportunity> updatedoppty = new Set<Opportunity>();
    for (Opportunity opp: trigger.new){
        updatedoppty.add(opp);
    }  
    
    Map<id,Opportunity> mapopty = new Map<id,Opportunity>();
    mapopty.putAll([Select id, name, Legal_Entity_Name__r.SAP_CODE__c,StageName from opportunity where (StageName='Verbal Agreement' and id in:updatedoppty)]);
    
    For (Opportunity opp: trigger.new){
        
        if(null==mapopty.get(opp.Legal_Entity_Name__r.name)){
             opp.addError('Oppotunity cannot move to 80% as LE is not present');
        }
        if(null==mapopty.get(opp.Legal_Entity_Name__r.SAP_CODE__c))
        {
               opp.addError('Oppotunity cannot move to 80% as SAP CODE is not present');
        }
    } 


 
Amol Rokade 26Amol Rokade 26
I guess your addError is not working because you are iterating over List<SObject> after querying using SOQL.AddError will only work if you are iterating over Trigger.New instead of using SOQL query.
Akash DeokarAkash Deokar
Hi amol, I didn't get what u wants to convey, will you please help with changes in code.
Amol Rokade 26Amol Rokade 26
You can use below code snippet :
for(Opportunity opp:trigger.new){
        if(opp.Legal_Entity_Name__r.SAP_CODE__c==null && opp.Legal_Entity_Name__r.Name==null && opp.StageName='Verbal Agreement'){
		
            opp.addError('Oppotunity cannot move to 80% as SAP CODE or LE is not present');
}
}

instead of your first code snippet because here you are iterating over SOQL query thats why it's not working
for(Opportunity opp:[Select id,name, Legal_Entity_Name__r.SAP_CODE__c,StageName from opportunity where (StageName='Verbal Agreement' and id in:trigger.new)]){
        if(opp.Legal_Entity_Name__r.SAP_CODE__c==null && opp.Legal_Entity_Name__r.Name==null){
            opp.addError('Oppotunity cannot move to 80% as SAP CODE or LE is not present');



 
Akash DeokarAkash Deokar
Done with the changes as mentioned but error is occurring for all opportunity stages. Please suggest.
Amol Rokade 26Amol Rokade 26
Can you please post your code here?
 
Amol Rokade 26Amol Rokade 26
trigger Oppty on Opportunity (before update) {
    for(Opportunity opp:Trigger.New){
        if(opp.StageName =='Verbal Agreement'){
            System.debug('Inside Trigger---->');
            opp.addError('Oppotunity cannot move to 80% as SAP CODE or LE is not present');
        }
  
}
}

Above code is working for me,Its only giving error when i try changes Stage to Verbal Agreement
Akash DeokarAkash Deokar
Please check below trigger, 
 
trigger SAP_CODE on Opportunity (before Update) {
   for(Opportunity opp:trigger.new){
           
        if(opp.StageName=='Verbal Agreement'){
            System.debug('Stage is verbal');
            
            if(opp.Legal_Entity_Name__r.Name==null){
                System.debug('inside entity name');
                opp.addError('Oppotunity cannot move to 80% as LE is not present');
                
                 If(opp.Legal_Entity_Name__r.SAP_CODE__c==null){
                    opp.addError('Oppotunity cannot move to 80% as SAPE CODE is not present');
                    System.debug('SAP code not available');
                }
            }
            else{
            system.debug('SAP code is available');
            }
        }
    }
}

Trying to run trigger with below code:
 
Opportunity opp= new Opportunity(name='demo', stagename='Qualification', closedate=system.today());
Account demoacc=[Select id from Account where name='Demo Account'];
opp.accountid=demoacc.id;
insert opp;
System.debug('opp inserted');
opp.stagename='verbal agreement';
try{
update opp;
}catch(Exception e)
{e.getmessage();}
Legal_Entity__c LEDemo = new Legal_Entity__c();
insert LEDemo;
opp.Legal_Entity_Name__c=LEDemo.id;
System.debug('LE inserted');
try{
update opp;
}catch(Exception e)
{e.getmessage();}
LEDemo.SAP_CODE__c='0008';
system.debug('SAP CODE updated');
try{
update opp;
}catch(Exception e)
{e.getmessage();}
Output:

User-added image
 
Akash DeokarAkash Deokar
I am not able to call Legal Entity object's field(providing null values) so the code is getting failed.
Please suggest how to call.