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
sdfasdsdfasd 

How to Create the Bulk Trigger

i Created the Trigger for Lead Conversion to Custom object. Now i Need BulkTrigger for Lead Conversion. i can try but still i am not completed how to  create the BulkTrigger. in this Trigger i am using Two custom objects.i.e Agency and Broker once Lead is converted Lead Name goes to Broker and Company goes to Agency object.This code working fine but this not Bulk Process. i need BulkProcess for this Trigger.Please help me.................

Trigger

=======

trigger ConvertLead on Lead (after insert, after update) {

 

if (Trigger.new.size()> 0){

if(Trigger.new[0].isConverted == true && Trigger.new[0].Lead_type_picklist__c =='Broker') {
   

   if (Trigger.new[0].ConvertedAccountId != null){

for(Account a :[Select a.Id,a.Name,a.Description,a.BillingStreet,a.BillingState, a.BillingPostalCode, a.BillingCountry, a.BillingCity,a.county__c,a.phone From Account a Where a.Id =:Trigger.new[0].ConvertedAccountId]){
ag = new Agency__c();
ag.Name = a.Name;
ag.Mailing_Address__c = a.BillingStreet;
ag.City__c =a.BillingCity;
ag.State__c = a.BillingState;
ag.County__c = a.County__c;
ag.Zip_Code__c = a.BillingPostalCode;
ag.Account__c = Trigger.new[0].ConvertedAccountId;
ag.Phone__c = a.Phone;
}
insert ag;
}
List<Contact> con = new List<Contact>();
List<Broker__c> brokers = new List<Broker__c>();

if(Trigger.new[0].ConvertedContactId != null){

for(Contact c :[Select id,Name,Description,AccountId from Contact where Id = :Trigger.new[0].ConvertedContactId]){
Broker__c b = new Broker__c();
b.Name = c.Name;
b.Agency__c = ag.id;
b.Contact_ID__c = c.Id;
brokers.add(b);
}
insert brokers;
}
}
}
}

how can i created BulkProcess for this Trigger pls help................

Best Answer chosen by Admin (Salesforce Developers) 
RajivRajiv

// Use this code. 

 

since you are using agency custom object so we can't assign account to it.

 

trigger ConvertLead on Lead (after insert, after update) {
List<Id> accountIds = new List<Id>();
List<Id> contactIds = new List<Id>();
list<lead> leadobject = new List<lead>();
for(Lead led: Trigger.New){
if(led.isConverted == true && led.Lead_type_picklist__c =='Broker'){
if(led.ConvertedAccountId != null && led.ConvertedContactId != null )
leadobject.add(led);
accountIds.add(led.ConvertedAccountId);
contactIds.add(led.ConvertedContactId);
}
}
Map<Id,Account> accountMap = new Map<Id,Account>([Select Id from Account where Id IN: accountIds]);
Map<Id,contact> contactMap = new Map<Id,contact>([Select Id from contact where Id IN: contactIds]);
List<Agency__c > agencyinsert = new List<Agency__c >();
List<Broker__c> brokers = new List<Broker__c>();
for(integer i=0; i<leadobject.size(); i++){
Id accId = leadobject[i].ConvertedAccountId; // Use [i] instead of using [0]
Id cId = leadobject[i].ConvertedContactId;
Account accountobj = accountMap.get(accId);
Contact contactobj = contactMap.get(cId);
Agency__c ag = new Agency__c();
ag.Name = accountobj.Name;
ag.Mailing_Address__c = accountobj.BillingStreet;
ag.City__c =accountobj.BillingCity;
ag.State__c = accountobj.BillingState;
ag.County__c = accountobj.County__c;
ag.Zip_Code__c = accountobj.BillingPostalCode;
ag.Account__c = accountobj.Id; //use id instead of using ConvertedAccountId;
ag.Phone__c = accountobj.Phone;
agencyinsert.add(ag);

}

if( agencyinsert.size()>0)
insert agencyinsert;

 

for(integer i=0; i<leadobject.size(); i++){
for(integer j=0; j<agencyinsert.size(); j++){
if(leadobject[i].ConvertedAccountId == agencyinsert[j].Account__c){
Id cId = leadobject[i].ConvertedContactId;
Contact contactobj = contactMap.get(cId);
Broker__c b = new Broker__c();
b.Name = contactobj.Name;
b.Agency__c = agencyinsert[j].id;
b.Contact_ID__c = contactobj.Id;
brokers.add(b);
}
}
}

if(brokers.size() > 0)
insert brokers;
}

All Answers

Chamil MadusankaChamil Madusanka

Refer this link. It will very helpfull for you.

 

http://wiki.developerforce.com/page/Apex_Code_Best_Practices

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

RajivRajiv

Hi you can use this code it's a bulkfy code

 

trigger ConvertLead on Lead (after insert, after update) {
List<Id> accountIds = new List<Id>();
List<Id> contactIds = new List<Id>();
list<lead> leadobject = new List<lead>();
for(Lead led: Trigger.New){
if(led.isConverted == true && led.Lead_type_picklist__c =='Broker'){
if(led.ConvertedAccountId != null && led.ConvertedContactId != null )
leadobject.add(led);
accountIds.add(led.ConvertedAccountId);
contactIds.add(led.ConvertedContactId);

}
}

Map<Id,Account> accountMap = new Map<Id,Account>([Select Id from Account where Id IN: accountIds]);
Map<Id,contact> contactMap = new Map<Id,contact>([Select Id from contact where Id IN: contactIds]);
List<account> accountinsert = new List<Account>();
List<Broker__c> brokers = new List<Broker__c>();

for(integer i=0; i<leadobject.size(); i++){
Id accId = leadobject.ConvertedAccountId;
Id cId = leadobject.ConvertedContactId;
Account accountobj = accountMap.get(accId);
Contact contactobj = contactMap.get(cId);
Agency__c ag = new Agency__c();
ag.Name = accountobj.Name;
ag.Mailing_Address__c = accountobj.BillingStreet;
ag.City__c =accountobj.BillingCity;
ag.State__c = accountobj.BillingState;
ag.County__c = accountobj.County__c;
ag.Zip_Code__c = accountobj.BillingPostalCode;
ag.Account__c = accountobj.ConvertedAccountId;
ag.Phone__c = accountobj.Phone;

accountinsert.add(ag);

Broker__c b = new Broker__c();
b.Name = contactobj.Name;
b.Agency__c = accountobj.id;
b.Contact_ID__c = contactobj.Id;
brokers.add(b);
}

if( accountinsert.size()>0)
insert accountinsert;
if(brokers.size() > 0)
insert brokers;
}

 

if it's work.please mark as a solution

 

sdfasdsdfasd

Thanks for your post Rajiv. i am using your code but that code not saved Rajiv showing error.

 

i.e   Initial term of field expression must be a concrete SObject: LIST<Lead> at line 20 column 12

 

trigger ConvertLead on Lead (after insert, after update) {

List<Id> accountIds = new List<Id>();
List<Id> contactIds = new List<Id>();
list<lead> leadobject = new List<lead>();

for(Lead led: Trigger.New){
if(led.isConverted == true && led.Lead_type_picklist__c =='Broker'){
if(led.ConvertedAccountId != null && led.ConvertedContactId != null )
leadobject.add(led);
accountIds.add(led.ConvertedAccountId);
contactIds.add(led.ConvertedContactId);
}
}
Map<Id,Account> accountMap = new Map<Id,Account>([Select Id from Account where Id IN: accountIds]);
Map<Id,contact> contactMap = new Map<Id,contact>([Select Id from contact where Id IN: contactIds]);
List<account> accountinsert = new List<Account>();
List<Broker__c> brokers = new List<Broker__c>();
for(integer i=0; i<leadobject.size(); i++){
Id accId = leadobject[0].ConvertedAccountId;
Id cId = leadobject[0].ConvertedContactId;
Account accountobj = accountMap.get(accId);
Contact contactobj = contactMap.get(cId);
Agency__c ag = new Agency__c();
ag.Name = accountobj.Name;
ag.Mailing_Address__c = accountobj.BillingStreet;
ag.City__c =accountobj.BillingCity;
ag.State__c = accountobj.BillingState;
ag.County__c = accountobj.County__c;
ag.Zip_Code__c = accountobj.BillingPostalCode;
ag.Account__c = accountobj.ConvertedAccountId;
ag.Phone__c = accountobj.Phone;
accountinsert.add(ag);
Broker__c b = new Broker__c();
b.Name = contactobj.Name;
b.Agency__c = accountobj.id;
b.Contact_ID__c = contactobj.Id;
brokers.add(b);
}
if( accountinsert.size()>0)
insert accountinsert;
if(brokers.size() > 0)
insert brokers;
}

 

how can solve this problem please help...........

RajivRajiv

 

trigger ConvertLead on Lead (after insert, after update) {

List<Id> accountIds = new List<Id>();
List<Id> contactIds = new List<Id>();
list<lead> leadobject = new List<lead>();

for(Lead led: Trigger.New){
if(led.isConverted == true && led.Lead_type_picklist__c =='Broker'){
if(led.ConvertedAccountId != null && led.ConvertedContactId != null )
leadobject.add(led);
accountIds.add(led.ConvertedAccountId);
contactIds.add(led.ConvertedContactId);
}
}
Map<Id,Account> accountMap = new Map<Id,Account>([Select Id from Account where Id IN: accountIds]);
Map<Id,contact> contactMap = new Map<Id,contact>([Select Id from contact where Id IN: contactIds]);
List<account> accountinsert = new List<Account>();
List<Broker__c> brokers = new List<Broker__c>();
for(integer i=0; i<leadobject.size(); i++){
Id accId = leadobject[i].ConvertedAccountId;  // Use [i] instead of using [0]
Id cId = leadobject[i].ConvertedContactId;
Account accountobj = accountMap.get(accId);
Contact contactobj = contactMap.get(cId);
Agency__c ag = new Agency__c();
ag.Name = accountobj.Name;
ag.Mailing_Address__c = accountobj.BillingStreet;
ag.City__c =accountobj.BillingCity;
ag.State__c = accountobj.BillingState;
ag.County__c = accountobj.County__c;
ag.Zip_Code__c = accountobj.BillingPostalCode;
ag.Account__c = accountobj.ConvertedAccountId;
ag.Phone__c = accountobj.Phone;
accountinsert.add(ag);
Broker__c b = new Broker__c();
b.Name = contactobj.Name;
b.Agency__c = accountobj.id;
b.Contact_ID__c = contactobj.Id;
brokers.add(b);
}
if( accountinsert.size()>0)
insert accountinsert;
if(brokers.size() > 0)
insert brokers;
}

 

sdfasdsdfasd

ok Rajiv i am using  like this

 

Id accId = leadobject[i].ConvertedAccountId;
Id cId = leadobject[i].ConvertedContactId;

 

Now display the error this line

 

ag.Account__c = accountobj.ConvertedAccountId;

// Error is Invalid field ConvertedAccountId for SObject Account at line 31 column 17

 

here also i am using  ag.Account__c = accountobj[i].ConvertedAccountid like thits then also showing error

 i.e   Expression must be a list type: SOBJECT:Account at line 31 column 17

 

how can solve this problem  pls help.............


RajivRajiv

trigger ConvertLead on Lead (after insert, after update) {

List<Id> accountIds = new List<Id>();
List<Id> contactIds = new List<Id>();
list<lead> leadobject = new List<lead>();

for(Lead led: Trigger.New){
if(led.isConverted == true && led.Lead_type_picklist__c =='Broker'){
if(led.ConvertedAccountId != null && led.ConvertedContactId != null )
leadobject.add(led);
accountIds.add(led.ConvertedAccountId);
contactIds.add(led.ConvertedContactId);
}
}
Map<Id,Account> accountMap = new Map<Id,Account>([Select Id from Account where Id IN: accountIds]);
Map<Id,contact> contactMap = new Map<Id,contact>([Select Id from contact where Id IN: contactIds]);
List<account> accountinsert = new List<Account>();
List<Broker__c> brokers = new List<Broker__c>();
for(integer i=0; i<leadobject.size(); i++){
Id accId = leadobject[i].ConvertedAccountId;  // Use [i] instead of using [0]
Id cId = leadobject[i].ConvertedContactId;
Account accountobj = accountMap.get(accId);
Contact contactobj = contactMap.get(cId);
Agency__c ag = new Agency__c();
ag.Name = accountobj.Name;
ag.Mailing_Address__c = accountobj.BillingStreet;
ag.City__c =accountobj.BillingCity;
ag.State__c = accountobj.BillingState;
ag.County__c = accountobj.County__c;
ag.Zip_Code__c = accountobj.BillingPostalCode;

ag.Account__c = accountobj.Id;        //use id instead of using ConvertedAccountId;
ag.Phone__c = accountobj.Phone;
accountinsert.add(ag);
Broker__c b = new Broker__c();
b.Name = contactobj.Name;
b.Agency__c = accountobj.id;
b.Contact_ID__c = contactobj.Id;
brokers.add(b);
}
if( accountinsert.size()>0)
insert accountinsert;
if(brokers.size() > 0)
insert brokers;
}

 

If it work. Mark as a solution.

 

 

Thanks,

 

sdfasdsdfasd

i am using but still that code showing error

 

i.e  Incompatible element type SOBJECT:Agency__c for collection of SOBJECT:Account at line 35 column 9

 

  

for(integer i=0; i<leadobject.size(); i++){
Id accId = leadobject[i].ConvertedAccountId;
Id cId = leadobject[i].ConvertedContactId;
Account accountobj = accountMap.get(accId);
Contact contactobj = contactMap.get(cId);
Agency__c ag = new Agency__c();
ag.Name = accountobj.Name;
ag.Mailing_Address__c = accountobj.BillingStreet;
ag.City__c =accountobj.BillingCity;
ag.State__c = accountobj.BillingState;
ag.County__c = accountobj.County__c;
ag.Zip_Code__c = accountobj.BillingPostalCode;
ag.Account__c = accountobj.Id;
ag.Phone__c = accountobj.Phone;
accountinsert.add(ag);

Broker__c b = new Broker__c();
b.Name = contactobj.Name;
b.Agency__c = accountobj.id;
b.Contact_ID__c = contactobj.Id;
brokers.add(b);
}

 

after  i am using like this

  

 Agency__c ag;

for(Account accountobj :accountMap.get(accId)){
ag = new Agency__c();
ag.Name = accountobj.Name;
ag.Mailing_Address__c = accountobj.BillingStreet;
ag.City__c =accountobj.BillingCity;
ag.State__c = accountobj.BillingState;
ag.County__c = accountobj.County__c;
ag.Zip_Code__c = accountobj.BillingPostalCode;
ag.Account__c = accountobj.Id;
ag.Phone__c = accountobj.Phone;
}
accountinsert.add(ag);

 

now showing errore like this

     Incompatible element type SOBJECT:Agency__c for collection of SOBJECT:Account at line 38 column 13

how can solve this problem pls help..................

RajivRajiv

// Use this code. 

 

since you are using agency custom object so we can't assign account to it.

 

trigger ConvertLead on Lead (after insert, after update) {
List<Id> accountIds = new List<Id>();
List<Id> contactIds = new List<Id>();
list<lead> leadobject = new List<lead>();
for(Lead led: Trigger.New){
if(led.isConverted == true && led.Lead_type_picklist__c =='Broker'){
if(led.ConvertedAccountId != null && led.ConvertedContactId != null )
leadobject.add(led);
accountIds.add(led.ConvertedAccountId);
contactIds.add(led.ConvertedContactId);
}
}
Map<Id,Account> accountMap = new Map<Id,Account>([Select Id from Account where Id IN: accountIds]);
Map<Id,contact> contactMap = new Map<Id,contact>([Select Id from contact where Id IN: contactIds]);
List<Agency__c > agencyinsert = new List<Agency__c >();
List<Broker__c> brokers = new List<Broker__c>();
for(integer i=0; i<leadobject.size(); i++){
Id accId = leadobject[i].ConvertedAccountId; // Use [i] instead of using [0]
Id cId = leadobject[i].ConvertedContactId;
Account accountobj = accountMap.get(accId);
Contact contactobj = contactMap.get(cId);
Agency__c ag = new Agency__c();
ag.Name = accountobj.Name;
ag.Mailing_Address__c = accountobj.BillingStreet;
ag.City__c =accountobj.BillingCity;
ag.State__c = accountobj.BillingState;
ag.County__c = accountobj.County__c;
ag.Zip_Code__c = accountobj.BillingPostalCode;
ag.Account__c = accountobj.Id; //use id instead of using ConvertedAccountId;
ag.Phone__c = accountobj.Phone;
agencyinsert.add(ag);

}

if( agencyinsert.size()>0)
insert agencyinsert;

 

for(integer i=0; i<leadobject.size(); i++){
for(integer j=0; j<agencyinsert.size(); j++){
if(leadobject[i].ConvertedAccountId == agencyinsert[j].Account__c){
Id cId = leadobject[i].ConvertedContactId;
Contact contactobj = contactMap.get(cId);
Broker__c b = new Broker__c();
b.Name = contactobj.Name;
b.Agency__c = agencyinsert[j].id;
b.Contact_ID__c = contactobj.Id;
brokers.add(b);
}
}
}

if(brokers.size() > 0)
insert brokers;
}

This was selected as the best answer
sdfasdsdfasd

Thanks for your help Rajiv Now Trigger working fine. 

Thanks.................... Rajiv