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
brijender singh rathore 16brijender singh rathore 16 

Trigger to convert lead into account,contact,opportunity .

PLZ HELP ME HOW GET ConvertedAccountid TO WHICH LEAD IS GEETING CONVERTED
IT IS SHOWING NULL IN SYSTEM DEBUG.
trigger converted on Lead (after insert)
{
set<id> s= new set<id>();
list<account> acc = new list<account>();
list<contact> cs = new list<contact>();
list<task> tsk = new list<task>();
list<opportunity> ops = new list<opportunity>();
for (lead ls:trigger.new)
{

s.add(ls.id);

}
for(lead l:[select id,ConvertedAccountId,company from lead where id in:s])
{
 system.debug('*************ConvertedAccountId************'+l.ConvertedAccountId);
account a = new account();
a.id = l.ConvertedAccountId;
a.name = l.company;

acc.add(a);
system.debug('@@@@@@'+a);
opportunity o = new opportunity();
o.name = l.company;
o.accountid = l.ConvertedAccountId;
o.StageName = 'Prospecting';
o.CloseDate = system.today();

ops.add(o);
system.debug('@@@@@@'+o);
task t = new task();
t.subject = 'converted lead task';
t.whatid = l.ConvertedAccountId;
t.priority = 'Normal';
t.status = 'Not Started';

tsk.add(t);
system.debug('@@@@@@'+t);
contact c = new contact();
c.lastname = l.company;
c.accountid = l.ConvertedAccountId;

cs.add(c);
system.debug('@@@@@@'+c);
}
insert acc;
insert cs;
insert tsk;
insert ops;


}
HARSHIL U PARIKHHARSHIL U PARIKH
In my opinion ConvertedAccountid will only be available for the lead who had already been converted. 

Your code quaries the ConvertedAccountid right after the lead record has been inserted, yet there is no ConvertedAccountid at this point. However, let's say if you convert the lead and then ask for ConvertedAccountid  then you will get one.

I would put one extra condition on for loop.
 
for (lead ls:trigger.new)
{
  If(ls.IsConverted)
  {
     s.add(ls.id); // at this point you are only collecting the ids for the lead which are converted.
  }
}

In addition, your trigger is on After Insert and so you may still not get it since you need to update a lead record if you want to convert it. You insert first and then you update to convert it. You may need to add After Update condition on trigger parameters.

I would say when user converts the lead, the account, conatct, AND/OR opportunity record gets automatically created so why would you need a trigger yo do the job?

You can also specify the insert statements in the bulk pattern.

See below example,
 
List<Account> actsToInsert = New List<Account>();
acsToInsert.add(act);

// samething for contact, opps, and tasks lists

If(!actsToInsert.IsEmpty()){
   insert actsToInsert;
}


Hope this helps and if it solves your question then kindly mark it solved!
brijender singh rathore 16brijender singh rathore 16
hello HARSHIL
thanks for the response
plz tell me what are the changes i have to make in the CURRENT TRIGGER.
IF I NEED TO CONVERT A LEAD IN CASE OF AFTER INSERT EVENT AUTOMATICALLY  BY USING TRIGGER .

 
HARSHIL U PARIKHHARSHIL U PARIKH
Hi Brijender,

I am happy to help!

I need to know your actual requirements in detail in order to help you further.

I am not sure why are you creating a trigger for a functionality which already almost exists. Is your requirement is to create an Account, Contact, Opportunity and Task record for ANY lead which makes it to your database? If yes, then you don't need ConvertedAccountid at all. In other words, you don't need to even convert leads because trigger would create an account, contact, opportunity and task record regardless.

Hope it helps!
HARSHIL U PARIKHHARSHIL U PARIKH
Hi BriJender,

There are two ways here:

1) First Standard functionality is doing exactly the same thing that you have described in trigger. I have checked it in my org

2) Trigger approach (Again, if you can manage with standard then GO FOR STANDARD. If you are doing these for let's say learning purpose then I would suggest following,)
In this particular case would recommend that you completely go for the Trigger alone approch and avoid using anything else.

Big Picture.

a) Create a field named Convert__c checkbox (by default uncheck) on Lead object.
b) Create a trigger which would fire when Convert__c checkbox is set to true and then creates appropriate Account, Contact, Opportunity, and Task record in a PERFECT manner. In other words, exactly the way you wanted.
c) Tell your users to click the checkbox to TRUE only for the Leads which they need to convert. Once User clicks convert checkbox or you update all the leads with convert checkbox to TRUE - Trigger would start and does its job which is to create an appropriate Account, Contact, Opportunity, and Task Record.

If you are taking a 2) second approach / trigger approach then you won't need to deal with convertedAccountID field at all.

Hope this helps!
brijender singh rathore 16brijender singh rathore 16
THANKS FOR THE HELP HARSHIL I AM A NEWBEE AND I AM DOING IT FOR THE LEARNING PURPOSE
JUST CLEAR MY ONE DOUBT WHAT IF I WANT TO ATTACH LEAD TO AN EXISTING ACCOUNT  BY USING TRIGGER.
LIKE WE CAN DO BY USING STADARD FUNCTIONALITY.
HARSHIL U PARIKHHARSHIL U PARIKH
If you are taking a Trigger approach then I would say create field named account on LEAD object which would lookup to the Account and then when you create account, update that Lead's account__c field with newly created account.ID. In this case you may need to stop the recursion of a trigger with a help of static variable but it won't be too hard (just few lines of extra code;) )

Happy coding Sir!
brijender singh rathore 16brijender singh rathore 16
U didn't get my question Harshil, I was saying at the time of lead conversion we get an option of converting the lead below an existing account, how can we do that by using trigger
Robert CheekRobert Cheek

Harshil,

I have a related/similar question and I'm hoping you can modify your response to Brijender above - and apologies on reviving an old thread here.

I recognize that the standard functionality is doing exactly the thing I'm looking for - however, we have a need to separate specialized prospects, leads, accounts and opportunities from our general sales leads, accounts and opportunities. 

I created new Apps (in the Lightning experience) for each specialized list (TypeA, TypeB, etc) and on those Apps, I've created specific objects (TypeA_Leads, TypeA_Accounts). I want the standard functionality to apply to these duplicate objects the way that they do to the standard objects - or build triggers which will operate in the same way for ease of training and function for staff.

I'd prefer the easy solution - applying the standard functionality - but would your trigger example work for duplicating those results via a trigger?