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
ezdhanhussainezdhanhussain 

Convert list of leads which are already saved in lead sobject

I have a list of leads which i retrieve through query, now i want to convert this list of leads to account and contacts without using any trigger/validation. only through apex code.  Below is code on which i am working.

list<Lead> myLead = [select id,LastName,company from Lead where company='sambodhi'];
return myLead;
for(integer i=0; i<=mylead.size();i++){
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(myLead[i].id);
lc.setAccountid(myLead[i].id);
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true limit 199
ALL ROWS];
lc.setConvertedStatus(convertStatus.MasterLabel);
Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
}

Best Answer chosen by Admin (Salesforce Developers) 
vbsvbs

Try this:

 

Database.LeadConvert[] lcList = new List<Database.LeadConvert>();
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true limit 1];
for(Lead myLead : [select id,LastName,company from Lead where company='sambodhi']){
	Database.LeadConvert lc = new database.LeadConvert();
	lc.setLeadId(myLead.id);
	lc.setAccountid(myLead.id);	
	lc.setConvertedStatus(convertStatus.MasterLabel);
	lcList.add(lc);
}
Database.LeadConvertResult[] lcr = Database.convertLead(lcList);

 

All Answers

souvik9086souvik9086

Can you explain what is the issue coming with the above code?

 

Thanks

ezdhanhussainezdhanhussain

first 2 lines of the code are getting executed, rest is skipped. It is not even showing any errors. The above code is to convert the list of leads i have obtained in first 2 lines.

vbsvbs
The return statement on Line 2 will exit the function/code block. Is this part of same code or written as a part of 2 separate functions? Also can you confirm the SOQL does return rows?
ezdhanhussainezdhanhussain

thank you vbs, i removed return statement and my code is getting executed completely, but my condition to convert those list of leads is not getting done.

vbsvbs

Can you confirm if the SOQL returns any data? Here are some other pointers:
1. Convert the for loop from a traditional one to a SOQL for loop for e.g.

for (Lead myLead : [select id,LastName,company from Lead where company='sambodhi'])

2. The embedded SOQL in the for loop for getting a list of LeadStatus obects will throw an error for governor limits. Place it outside the loop.

3. Database leadconvert will need to be moved out of the loop as well.

 

Just fix these pieces and let me know if you have eny errors coming up.

 

ezdhanhussainezdhanhussain

The SOQL is returning list of data from sobject, i placed the embedded SOQL and database.leadconvert outside the loop, but it is returning error like unknown variable myLead. 

vbsvbs

Try this:

 

Database.LeadConvert[] lcList = new List<Database.LeadConvert>();
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true limit 1];
for(Lead myLead : [select id,LastName,company from Lead where company='sambodhi']){
	Database.LeadConvert lc = new database.LeadConvert();
	lc.setLeadId(myLead.id);
	lc.setAccountid(myLead.id);	
	lc.setConvertedStatus(convertStatus.MasterLabel);
	lcList.add(lc);
}
Database.LeadConvertResult[] lcr = Database.convertLead(lcList);

 

This was selected as the best answer
ezdhanhussainezdhanhussain
Thanks vbs it's done
ezdhanhussainezdhanhussain

it's completed atlast.. thank you vbs