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
WiznoWizno 

Having trouble programmatically converting Lead using Trigger.

Hey all,

 

I'm having a hard time figure out what I'm doing wrong.

 

I'm trying to have a lead get auto convertted if the lead source is of a specific type but, something is going wrong and I'm really not sure what it is.

 

If I try making a lead with the specified LeadSource and click save, the next page says that the lead was converted. The only problem is that there is no new Account and Contact object record related to that lead, and the lead also gets deleted.

 

Just ran apex tests in the sandbox and I'm not getting any errors.

 

Really would appreciate some guidance here... I've looked at like 5 tutorials and all of them have had essentially the same code, if not identical.

 

Thanks

 

 

if ( trigger.isAfter && trigger.isInsert)
    {
    	for( Lead l : trigger.new)
    	{
    		if( l.LeadSource == 'CustomPortal' && l.IsConverted == false)
            {
            	Database.Leadconvert lc = new Database.LeadConvert();
            	
            	lc.setLeadId(l.Id);
            	lc.setDoNotCreateOpportunity(true);
            	LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            	lc.setConvertedStatus(convertStatus.MasterLabel);
            	
            	Database.Leadconvertresult lcr = Database.convertLead(lc);
            	System.assert(lcr.isSuccess());
            	
            	Account newA = new Account( id = l.ConvertedAccountId, Primary_Contact__c = l.ConvertedContactId, Name = l.FirstName + ' ' + l.LastName);
            	//UpdateAccts.add(newA);
            	insert newA;
            }
    	}
    	//Insert UpdateAccts;
    }

 

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

This worked just fine for me. Just execute the Lead Convert bit, dont insert / update anything explicitly. The ConvertedContactId and ConvertedLeadId will be set on the Lead automatically post-conversion.

 

Lead l = [Select Id, Name from Lead LIMIT 1];

Database.Leadconvert lc = new Database.LeadConvert();
lc.setLeadId(l.Id);
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
lc.setDoNotCreateOpportunity(true);

Database.Leadconvertresult lcr = Database.convertLead(lc);

l = [Select Id, Name, ConvertedAccountId, ConvertedContactId from Lead where Id = :l.Id];
System.debug(l.ConvertedAccountId + ' ' + l.ConvertedContactId);

All Answers

Ritesh AswaneyRitesh Aswaney

For starters, you're setting the Id on the Account instance you're trying to insert. Don't reckon you can specify the Id in an insert call.

 

Also Lead Conversion should auto-create an Account and Contact, you shouldn't have to explicitly create an Account.

 

Just query for an Account, whose Id is ConvertedAccountId on the Lead to check if an Account has been created as a result of Lead Conversion.

 

so 

System.assertEquals(1, [Select Id, Name from Account where Id = :lead.convertedAccountId].size());

is worth a shot ?

WiznoWizno

Appreciate the quick response!

 

I added that in because I had no idea what else to do. Just took it out and tried adding a lead and the same thing happened. Updated my test class and now some errors are starting to show up. So I'll have to play around with that and see if it doesn't get me to the bottom of the problem I'm having.

 

Thanks again!

Ritesh AswaneyRitesh Aswaney

This worked just fine for me. Just execute the Lead Convert bit, dont insert / update anything explicitly. The ConvertedContactId and ConvertedLeadId will be set on the Lead automatically post-conversion.

 

Lead l = [Select Id, Name from Lead LIMIT 1];

Database.Leadconvert lc = new Database.LeadConvert();
lc.setLeadId(l.Id);
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
lc.setDoNotCreateOpportunity(true);

Database.Leadconvertresult lcr = Database.convertLead(lc);

l = [Select Id, Name, ConvertedAccountId, ConvertedContactId from Lead where Id = :l.Id];
System.debug(l.ConvertedAccountId + ' ' + l.ConvertedContactId);

This was selected as the best answer
WiznoWizno

The above works. The problem was a workflow rule that update a field on the type of record I was trying to add. Didn't know the rule was there :(

 

Thanks for the help!