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
Suhaas Kapardhi B SSuhaas Kapardhi B S 

Hi, how do I un-Bulkify this trigger on opportunity

trigger EmailUpdation on Opportunity (before update) {
    
    
    Set<Id> accId = new Set<Id>();
    Set<String> oppEmail = new Set<String>();
    Set<String> oldEmail = new Set<String>();
    List<Opportunity> accRelaOppEmail = new List<Opportunity>();
    

    for(Opportunity opp: Trigger.new){
        
        oppEmail.add(opp.Email__c);
        
        accId.add(opp.AccountId);
    }
    
    
    accRelaOppEmail= [Select Id, Email__c from Opportunity where AccountId in :accId and Account_Email__c in :opp.Email__c];
    
    for(Opportunity opp: accRelaOppEmail){
        
        oldEmail.add(opp.Email__c);
     }
    
    for(Opportunity opp: trigger.new){

        if(oldEmail.contains(opp.Email__c) && oldEmail.size() != accRelaOppEmail.size() ){
            

            

    
         }
       }
    }
Best Answer chosen by Suhaas Kapardhi B S
mukesh guptamukesh gupta
Hi SUhaas,

Please use below code:-
 
trigger EmailUpdation on Opportunity (before update) {
    
    
    Set<Id> accId = new Set<Id>();
    Set<String> oppEmail = new Set<String>();
    Set<String> oldEmail = new Set<String>();
    List<Opportunity> accRelaOppEmail = new List<Opportunity>();
    

    for(Opportunity opp: Trigger.new){
        
        oppEmail.add(opp.Email__c);
        
        accId.add(opp.AccountId);
    }
    
    
    accRelaOppEmail= [Select Id, Email__c from Opportunity where AccountId in :accId and Account_Email__c IN: oppEmail];
    
    for(Opportunity opp: accRelaOppEmail){
        
        oldEmail.add(opp.Email__c);
     }
    
    for(Opportunity opp: trigger.new){

        if(oldEmail.contains(opp.Email__c) && oldEmail.size() != accRelaOppEmail.size() ){
            

            

    
         }
       }
    }

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

All Answers

mukesh guptamukesh gupta
Hi SUhaas,

Please use below code:-
 
trigger EmailUpdation on Opportunity (before update) {
    
    
    Set<Id> accId = new Set<Id>();
    Set<String> oppEmail = new Set<String>();
    Set<String> oldEmail = new Set<String>();
    List<Opportunity> accRelaOppEmail = new List<Opportunity>();
    

    for(Opportunity opp: Trigger.new){
        
        oppEmail.add(opp.Email__c);
        
        accId.add(opp.AccountId);
    }
    
    
    accRelaOppEmail= [Select Id, Email__c from Opportunity where AccountId in :accId and Account_Email__c IN: oppEmail];
    
    for(Opportunity opp: accRelaOppEmail){
        
        oldEmail.add(opp.Email__c);
     }
    
    for(Opportunity opp: trigger.new){

        if(oldEmail.contains(opp.Email__c) && oldEmail.size() != accRelaOppEmail.size() ){
            

            

    
         }
       }
    }

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
This was selected as the best answer
Suhaas Kapardhi B SSuhaas Kapardhi B S
Thanks Mukesh