You need to sign in to do that
Don't have an account?
Trigger Fires for each 200 Insertions
Hi,
I have a problem with the Data Loader.
I am trying to insert 5000 records from the Data loader to One Custom Object and on the Custom Object I have written a Trigger.
My Scenario is, In the Trigger I have written a code to update User Object Fields from the custom object. Here I want to send an email from the Trigger those who are not updated in the User Object.
So I want to generate single email with that list but I am getting 25 emails. I observed on the Data loader Per each 200 inserts I am getting one email like for 5000 records finally i am getting 25 emails.
I have increased the batch size in data loader but of no use and checked the Bulk API option also and set the Batch size as 2000.Still i am getting an Email for every 200 insertions.
Please help me what is the solution for this.
Regards,
Narendra
You need to use a map instead of a nested for-each loop. It's hard to read your code, but it's apparent that your problem arises from the use of a for-each inside a for-each. Any time you have to do something like "if ( outer.X == inner.X ) ...", you can almost certainly guarantee that a Map is in order.
All Answers
Hi Thank you for Quick Response.
The Records got Updated successfully on the User Object . Some Users are not Updated means which are not inserted on the Custom Object . I need to get the List of not updated Users List on the User Object. How can i get this List.. please suggest me.
Thanks,
kumar
Hi SfdcFox,
Iam adding one condition in the trigger .The csv file Data is match up in the Trigger it cals the class. Iam adding that record as last record in the csv. so now iam getting only one mail.But, I am getting too many code statements:200001 while inserting the Records on the Custom Object.on the custom object one trigger is there.i am calling one class when the criteria is met in the Trigger.This is mmy class and Trigger code.. Can you please help on this.
trigger updateToUser on Export_Object__c (after insert,after update)
{
for(Export_Object__c e: Trigger.New)
{
if(e.Alias__c=='test'){
getUnupdatedUsers gu = new getUnupdatedUsers();
gu.mymethod();
}
}
}
class:
public class getUnupdatedUsers
{
List<Export_Object__c> eo_list = new List<Export_Object__c>();
set<String> eoid = new set<String>();
List<User> user_list = new List<User>();
List<User> updatelist = new List<user>();
List<User> updatefailedlist = new List<User>();
public void mymethod()
{
eo_list = [select id,Alias__c,City__c,Company__c,Country_Region__c,Department__c,FirstName__c,Mobile__c,state__c,Address__c,Telephone__c,Title__c from Export_Object__c];
for(Export_Object__c e: eo_list)
{
if(e.Alias__c!=null)
eoid.add(e.Alias__c);
}
user_list = [select id,adid__c from user where adid__c in: eoid];
for(Export_Object__c e: eo_list)
{
for(User u:User_List)
{
if(u.ADID__c == e.Alias__c)
{
if(e.City__c!=null)
u.City = e.City__c;
if(e.Company__c!=null)
u.CompanyName = e.Company__c;
if(e.Country_Region__c!=null)
u.Country = e.Country_Region__c;
if(e.Department__c!=null)
u.Department = e.Department__c;
if(e.FirstName__c!=null)
u.FirstName = e.FirstName__c;
if(e.Mobile__c!=null)
u.MobilePhone = e.Mobile__c;
if(e.state__c!=null)
u.State = e.state__c;
if(e.Address__c!=null)
u.Street = e.Address__c;
if(e.Telephone__c!=null)
u.phone = e.Telephone__c;
if(e.Title__c!=null)
u.Title = e.Title__c;
//u.available__c = true;
updatelist.add(u);
}
}
}
update updatelist;
updatefailedlist = [select id,ADID__c,name,email from user where id !=: updatelist];
system.debug('updatefailedlist '+updatefailedlist.size());
if(updatefailedlist.size()>0){
system.debug('mismatchlist size');
mismatchlist();
}
}
public void mismatchlist()
{
string header = 'Name , ADID, Email \n';
String finalstr = header;
for(user eo :updatefailedlist)
{
string recordString = '"'+eo.name+'","'+eo.adid__c+'","'+eo.email+'"\n';
finalstr = finalstr +recordString;
}
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.setFileName('Employee.csv');
attach.Body = Blob.valueOf(finalstr);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] toAddresses = new list<string> {'work@gmail.com'};
String subject ='Export CSV';
email.setSubject(subject);
email.setToAddresses( toAddresses );
email.setHtmlBody('Export CSV ');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}
You need to use a map instead of a nested for-each loop. It's hard to read your code, but it's apparent that your problem arises from the use of a for-each inside a for-each. Any time you have to do something like "if ( outer.X == inner.X ) ...", you can almost certainly guarantee that a Map is in order.
Hi SfdcFox,
Here my scenario is Iam inserting the Users Data on to the Export_object__c. On Export obj, i have written a trigger to match the fields in the User Object. Here my Unique field for comparision in Export object and User object is e.Alias__c and u.ADID__c.If this is same the records needs to be uploaded on User obj from ExportObj.other wise Records should be show in failed list and need to send an Email.
I do n't know how to use Map in this scenario. Could you please help on this.
Regards,
Narendra