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
taaataaa 

Bulk trigger

Hi,

 

I need to insert 100 records at atime, and this trigger doesnt work, please help me writing bulk trigger.

 

trigger emailcopy on Lead (before update, before insert) {

Lead ld = Trigger.new[0];
if(ld.Contact__c!=null&& ld.RecordTypeId == '012V00000008atT') {
Contact con = [Select id,Email,AccountId,Phone,Phone_Extension__c,MobilePhone from Contact where id =: ld.Contact__c];
Account acc = [Select id,AutomationandControlProductsRegion__c from Account where id =: con.AccountId];

ld.AutomationandControlProductsRegion__c = acc.AutomationandControlProductsRegion__c;
ld.Email = con.Email;
ld.Phone = con.Phone;
ld.PhoneExtension__c = con.Phone_Extension__c;
ld.MobilePhone =con.MobilePhone;

}

Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

Hi,

 

Refer the below code:

 

 

trigger emailcopy on Lead (before update, before insert) {

     map<Id, Contact>mapRelatedContacts = new Map<Id, Contact>();

     set<Id> setContactIds = new set<Id>();

 

     for(Lead l : trigger.new)

     {

          setContactIds.add(i.Contact__c);

     }

 

     for(Contact c : [Select Id, Email, Phone, Phone_Extension__c, MobilePhone,     Account.AutomationandControlProductsRegion__c FROM Contact Where ID IN : setContactIds])

     {

          mapRelatedContacts.put(c.Id, c);

     }

  

     for(Lead l : trigger.new)

     {

         if(l.Contact__c!=null&& l.RecordTypeId == '012V00000008atT')

         {

               l.AutomationandControlProductsRegion__c =  mapRelatedContacts.get(l.Contact__c).Account.AutomationandControlProductsRegion__c;
               l.Email = mapRelatedContacts.get(l.Contact__c).Email;
               l.Phone = mapRelatedContacts.get(l.Contact__c).Phone;
               l.PhoneExtension__c = mapRelatedContacts.get(l.Contact__c).Phone_Extension__c;
               l.MobilePhone =mapRelatedContacts.get(l.Contact__c).MobilePhone;

     }
}

 

NOTE : The condition regarding Lead's recordtypeid is not the ideal way to check, you're hardcoding a recordtypeid and checking against it.

Ideally you should query the record type based on it's name and Sobject and then fetch its ID. Reason: Id's are different in different Orgs, so you may face issues while or post deployment.

 

Rest of the code should work fine.

All Answers

vishal@forcevishal@force

Hi,

 

Refer the below code:

 

 

trigger emailcopy on Lead (before update, before insert) {

     map<Id, Contact>mapRelatedContacts = new Map<Id, Contact>();

     set<Id> setContactIds = new set<Id>();

 

     for(Lead l : trigger.new)

     {

          setContactIds.add(i.Contact__c);

     }

 

     for(Contact c : [Select Id, Email, Phone, Phone_Extension__c, MobilePhone,     Account.AutomationandControlProductsRegion__c FROM Contact Where ID IN : setContactIds])

     {

          mapRelatedContacts.put(c.Id, c);

     }

  

     for(Lead l : trigger.new)

     {

         if(l.Contact__c!=null&& l.RecordTypeId == '012V00000008atT')

         {

               l.AutomationandControlProductsRegion__c =  mapRelatedContacts.get(l.Contact__c).Account.AutomationandControlProductsRegion__c;
               l.Email = mapRelatedContacts.get(l.Contact__c).Email;
               l.Phone = mapRelatedContacts.get(l.Contact__c).Phone;
               l.PhoneExtension__c = mapRelatedContacts.get(l.Contact__c).Phone_Extension__c;
               l.MobilePhone =mapRelatedContacts.get(l.Contact__c).MobilePhone;

     }
}

 

NOTE : The condition regarding Lead's recordtypeid is not the ideal way to check, you're hardcoding a recordtypeid and checking against it.

Ideally you should query the record type based on it's name and Sobject and then fetch its ID. Reason: Id's are different in different Orgs, so you may face issues while or post deployment.

 

Rest of the code should work fine.

This was selected as the best answer
taaataaa

thanks Vishal it worked!!

 

and thanks for suggestion too,

however i also wrote in meanwhile, it worked but not sure, it is rite way or not,

 

trigger CopyLeadEmail on Lead (before update, before insert) {

 

List<String> Lead = new List<String>{};

for (lead ld: trigger.new){

 

 

    //Lead ld = Trigger.new[0];

    if(ld.Contact__c!=null&& ld.RecordTypeId == '012V00000008atT') {

        Contact con = [Select id,Email,AccountId,Phone,Phone_Extension__c,MobilePhone from Contact where id =: ld.Contact__c];

        Account acc = [Select id,AutomationandControlProductsRegion__c from Account where id =: con.AccountId];

       

        ld.AutomationandControlProductsRegion__c = acc.AutomationandControlProductsRegion__c;

        ld.Email = con.Email;

        ld.Phone = con.Phone;

        ld.PhoneExtension__c = con.Phone_Extension__c;

        ld.MobilePhone =con.MobilePhone;

        }

   

    }

apex_keenapex_keen

This is not correct .You should always try avoiding writing 'soql queries' inside 'For loop' as this could create problem with governer limits set for organization.

vishal@forcevishal@force

Yes, the reason why I changed that code is because we should not have SOQL queries or DML statements inside a for loop. It's not a good practice to do so as in case of bulk data operations, this can hit the governor limits.

taaataaa

Hi Vishal,

 

Thanks for the suggestion. Your idea worked for another triggers too and i got success also, however i stucked in one mre trigger...it fails while inserting multiple records...pls have a look in this, and tell me how to approach..

 

trigger trg_HAS_CreateOpportunityNAwardOnClosedStage_BU on Opportunity (before update) {

if(!Validator_cls.hasAlreadyDone()){


if(Trigger.new[0].RecordTypeId == '012V00000008as6' && Trigger.old[0].StageName != Trigger.new[0].StageName && Trigger.new[0].StageName == 'Email Commitment/Closed Won (100%)') {
Trigger.new[0].check__c=true;
Award__c aw = new Award__c();
aw.Name = 'AW-'+Trigger.new[0].Name;
aw.Stage__c = 'Email Commitment';
aw.Account__c = Trigger.new[0].AccountId;
aw.Opportunity__c = Trigger.new[0].Id;
insert aw;


list<Opportunity> opp = new list<Opportunity>();


Opportunity oo = new Opportunity ();

if(Trigger.new[0].Name.contains('Ren')) {
string test =Trigger.new[0].Name;
system.debug('test@@@@@@@@@@@'+Trigger.new[0].Name.contains('Ren') );


string oppname=test.substringBeforeLast('-');
string test12=test.substring(test.indexOf('Ren-')+4);
system.debug('@@@@@@@@@@@'+test12);
integer test23=integer.valueOf(test12);
integer test1234=test23+1;
system.debug('@@@@@@@@@@@@@test1234'+test1234);


oo.Name =oppname+'-' + string.valueof(test1234) ;
system.debug('Increment'+oo.Name);
oo.StageName = 'Need Analysis (0%) ';
oo.RecordTypeId = '012V00000008as6';
oo.AccountId = Trigger.new[0].AccountId;
oo.CloseDate = System.Today().addmonths(6);
oo.Procurement__c=Trigger.new[0].Procurement__c;
oo.LeadSource=Trigger.new[0].LeadSource;
oo.HBS_Opportunity_Contact__c=Trigger.new[0].HBS_Opportunity_Contact__c;
oo.Key_Decision_Maker__c=Trigger.new[0].Key_Decision_Maker__c;
oo.HAS_Opp_Flag__c=Trigger.new[0].HAS_Opp_Flag__c;

}

else{
oo.Name = Trigger.new[0].Name+' Ren-'+1;
system.debug('@@@@@@@@@@@'+oo.Name);
oo.StageName = 'Need Analysis (0%) ';
oo.RecordTypeId = '012V00000008as6';
oo.AccountId = Trigger.new[0].AccountId;
oo.CloseDate = System.Today().addmonths(6);
oo.Procurement__c=Trigger.new[0].Procurement__c;
oo.LeadSource=Trigger.new[0].LeadSource;
oo.HBS_Opportunity_Contact__c=Trigger.new[0].HBS_Opportunity_Contact__c;
oo.Key_Decision_Maker__c=Trigger.new[0].Key_Decision_Maker__c;
oo.HAS_Opp_Flag__c=Trigger.new[0].HAS_Opp_Flag__c;

}

//try{
insert oo;
system.debug('@@@@@@@@@@@@@insert'+oo);
// }

//catch(exception e){
// }

//For attaching the product:

List<Product__c> pro= [select Amount__c,Category__c,Product_Type__c,Expected_Start_Date__c,Expected_End_Date__c,CurrencyIsoCode,No_of_Seats__c,No_Of_Training_Days__c, opportunity__c from Product__c where opportunity__c =: Trigger.new[0].Id];
System.debug('List size of Products------>>>>>'+pro.size());
List<Product__c> insProduct = new List<Product__c>();

for(Integer i=0;i<pro.Size();i++){
Product__c p = new Product__c();
p.Opportunity__c = oo.Id;
p.Amount__c = pro[i].Amount__c;
p.Category__c=pro[i].Category__c;
p.Product_Type__c=pro[i].Product_Type__c;

if(pro[i].Expected_End_Date__c!=NULL){
integer numberDaysDue = pro[i].Expected_Start_Date__c.daysBetween(pro[i].Expected_End_Date__c);

system.debug('difference value'+numberDaysDue);
p.Expected_End_Date__c=pro[i].Expected_End_Date__c+numberDaysDue;
p.Expected_Start_Date__c=pro[i].Expected_End_Date__c.addDays(1);
}
else
{
p.Expected_End_Date__c=NULL;
p.Expected_Start_Date__c=NULL;

}


p.CurrencyIsoCode=pro[i].CurrencyIsoCode;
p.No_of_Seats__c=pro[i].No_of_Seats__c;
p.No_Of_Training_Days__c=pro[i].No_Of_Training_Days__c;


insProduct.add(p);
}
insert insProduct;








}


Validator_cls.setAlreadyDone();
}
}

 

thanks

taaataaa

Hi Vishal,

 

Thanks for the suggestion. Your idea worked for another triggers too and i got success also, however i stucked in one mre trigger...it fails while inserting multiple records...pls have a look in this, and tell me how to approach..

 

trigger trg_HAS_CreateOpportunityNAwardOnClosedStage_BU on Opportunity (before update) {

if(!Validator_cls.hasAlreadyDone()){


if(Trigger.new[0].RecordTypeId == '012V00000008as6' && Trigger.old[0].StageName != Trigger.new[0].StageName && Trigger.new[0].StageName == 'Email Commitment/Closed Won (100%)') {
Trigger.new[0].check__c=true;
Award__c aw = new Award__c();
aw.Name = 'AW-'+Trigger.new[0].Name;
aw.Stage__c = 'Email Commitment';
aw.Account__c = Trigger.new[0].AccountId;
aw.Opportunity__c = Trigger.new[0].Id;
insert aw;


list<Opportunity> opp = new list<Opportunity>();


Opportunity oo = new Opportunity ();

if(Trigger.new[0].Name.contains('Ren')) {
string test =Trigger.new[0].Name;
system.debug('test@@@@@@@@@@@'+Trigger.new[0].Name.contains('Ren') );


string oppname=test.substringBeforeLast('-');
string test12=test.substring(test.indexOf('Ren-')+4);
system.debug('@@@@@@@@@@@'+test12);
integer test23=integer.valueOf(test12);
integer test1234=test23+1;
system.debug('@@@@@@@@@@@@@test1234'+test1234);


oo.Name =oppname+'-' + string.valueof(test1234) ;
system.debug('Increment'+oo.Name);
oo.StageName = 'Need Analysis (0%) ';
oo.RecordTypeId = '012V00000008as6';
oo.AccountId = Trigger.new[0].AccountId;
oo.CloseDate = System.Today().addmonths(6);
oo.Procurement__c=Trigger.new[0].Procurement__c;
oo.LeadSource=Trigger.new[0].LeadSource;
oo.HBS_Opportunity_Contact__c=Trigger.new[0].HBS_Opportunity_Contact__c;
oo.Key_Decision_Maker__c=Trigger.new[0].Key_Decision_Maker__c;
oo.HAS_Opp_Flag__c=Trigger.new[0].HAS_Opp_Flag__c;

}

else{
oo.Name = Trigger.new[0].Name+' Ren-'+1;
system.debug('@@@@@@@@@@@'+oo.Name);
oo.StageName = 'Need Analysis (0%) ';
oo.RecordTypeId = '012V00000008as6';
oo.AccountId = Trigger.new[0].AccountId;
oo.CloseDate = System.Today().addmonths(6);
oo.Procurement__c=Trigger.new[0].Procurement__c;
oo.LeadSource=Trigger.new[0].LeadSource;
oo.HBS_Opportunity_Contact__c=Trigger.new[0].HBS_Opportunity_Contact__c;
oo.Key_Decision_Maker__c=Trigger.new[0].Key_Decision_Maker__c;
oo.HAS_Opp_Flag__c=Trigger.new[0].HAS_Opp_Flag__c;

}

//try{
insert oo;
system.debug('@@@@@@@@@@@@@insert'+oo);
// }

//catch(exception e){
// }

//For attaching the product:

List<Product__c> pro= [select Amount__c,Category__c,Product_Type__c,Expected_Start_Date__c,Expected_End_Date__c,CurrencyIsoCode,No_of_Seats__c,No_Of_Training_Days__c, opportunity__c from Product__c where opportunity__c =: Trigger.new[0].Id];
System.debug('List size of Products------>>>>>'+pro.size());
List<Product__c> insProduct = new List<Product__c>();

for(Integer i=0;i<pro.Size();i++){
Product__c p = new Product__c();
p.Opportunity__c = oo.Id;
p.Amount__c = pro[i].Amount__c;
p.Category__c=pro[i].Category__c;
p.Product_Type__c=pro[i].Product_Type__c;

if(pro[i].Expected_End_Date__c!=NULL){
integer numberDaysDue = pro[i].Expected_Start_Date__c.daysBetween(pro[i].Expected_End_Date__c);

system.debug('difference value'+numberDaysDue);
p.Expected_End_Date__c=pro[i].Expected_End_Date__c+numberDaysDue;
p.Expected_Start_Date__c=pro[i].Expected_End_Date__c.addDays(1);
}
else 
{
p.Expected_End_Date__c=NULL;
p.Expected_Start_Date__c=NULL;

}


p.CurrencyIsoCode=pro[i].CurrencyIsoCode;
p.No_of_Seats__c=pro[i].No_of_Seats__c;
p.No_Of_Training_Days__c=pro[i].No_Of_Training_Days__c;


insProduct.add(p);
}
insert insProduct;








}


Validator_cls.setAlreadyDone();
}
}

 

thanks