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
Mariappan PerumalMariappan Perumal 

Apex Trigger

Hi All ,

 

I have written this trigger . Its working fine for the single record and i am not sure with whether this trigger work well for bulk operation .

 

I don't know whats work with it. Can anyone help me out of this.

 

 

trigger EmailRemainder on Opportunity (after update)
{

Set<Id> ids=trigger.newmap.keySet();
Set<Id> idd=new Set<Id>();

List <OpportunityContactRole> ocr;
List<Contact> c;
List <Opportunity>op;
op=[select EmailRemainder__c from Opportunity where OpportunityId IN : ids];
ocr=[Select ContactId From OpportunityContactRole where OpportunityId IN : ids ];
for(OpportunityContactRole r:ocr)
{
idd.add(r.ContactId);
}
c=[select npe01__WorkEmail__c from Contact where ID IN : idd ];
for(Opportunity ops:op)
{
for(Contact ci : c)
{
ops.EmailRemainder__c = ci.npe01__WorkEmail__c;

}

update ops;
}

}

 

Thanks in advance

 

Best Answer chosen by Admin (Salesforce Developers) 
Prafull G.Prafull G.
You have to keep the DML statement (update ops;) outside the for loop. This is the only thing which will run into Governor Limits when operates in bulk.

Just create a list and add the ops in the list within loop and outside loop use the list to perform DML.

Hope it helps.

All Answers

Prafull G.Prafull G.
You have to keep the DML statement (update ops;) outside the for loop. This is the only thing which will run into Governor Limits when operates in bulk.

Just create a list and add the ops in the list within loop and outside loop use the list to perform DML.

Hope it helps.
This was selected as the best answer
Vinit_KumarVinit_Kumar

Hi ,

 

I have modifed the code,try the below one :-

 

trigger EmailRemainder on Opportunity (after update)
{
Set<Id> ids=trigger.newmap.keySet();
Set<Id> idd=new Set<Id>();
List <OpportunityContactRole> ocr;
List<Contact> c;
List <Opportunity>op;
List <Opportunity> opList = new List<Opportunity>();
op=[select EmailRemainder__c from Opportunity where OpportunityId IN : ids];
ocr=[Select ContactId From OpportunityContactRole where OpportunityId IN : ids ];
for(OpportunityContactRole r:ocr)
{
idd.add(r.ContactId);
}
c=[select npe01__WorkEmail__c from Contact where ID IN : idd ];
for(Opportunity ops:op)
{
for(Contact ci : c)
{
ops.EmailRemainder__c = ci.npe01__WorkEmail__c;
opList.add(ops);
}
}
update opList;
}