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
A.V.A.V. 

Convert Lead in Apex

Is it possible to leverage lead conversion process in Apex? I did not find any relevant information on this in the documentation.

This is how conversion is done via API, can I do this with Apex Code?

sforce.LeadConvert leadConvert = new sforce.LeadConvert();
...
...
lcr = binding.convertLead(new sforce.LeadConvert[] {leadConvert});
mtbclimbermtbclimber
Not today. Support for the leadConvert API call is not yet available in Apex.
A.V.A.V.
Is this feature on the roadmap now or not at all?
mtbclimbermtbclimber
This capability is definitely on the roadmap.

Thanks,
mtbclimbermtbclimber
By the way. Did you know that we have a developer category in the ideaexchange? The ideaexchange is a place where the community can submit requests for functionality and vote on which ones they like best.

We then take this feedback into consideration when prioritizing what we will incorporate into future releases.

I went ahead and added this idea. I recommend that you and anyone reading this that wants it please promote it:

http://ideas.salesforce.com/article/show/75427/LeadConvert_in_Apex

Be sure to only promote this if it's something you really would use.

Regards,
A.V.A.V.
Thanks for posting it! I promoted it.

AV.
A.V.A.V.
As afollow up to the above thread:

I created a trigger Lead after Update trigger that adds some additional functionality to the Lead conversion process.

Due to the fact that the APex does not have a convertLead() call, I am unable to create unitTests for this code. This issue is compounded now with the latest requirements of having 75% coverage in order to deploy the code to production.

So far, my only resort is to "fake" some test to get my test coverage to 75%, just so I can deploy my code. Obviously, this is a very ugly work around.

Does anyone have any suggestions? 

Thank you.
kpitkpit
A.V. - I'm facing the same issue and wondering how do you 'fake' the test?
AxxxVAxxxV
Hi kpit,

Actually, you no longer have to 'fake' the test coverage for lead conversion in Apex. The leadConvert() method has been added to Apex with the winter '08 release. I have not used it myself yet, but here is the documentation link:

http://www.salesforce.com/us/developer/docs/api/index_CSH.htm#sforce_api_calls_convertlead.htm#kanchor285





Message Edited by AxxxV on 12-11-2007 09:55 AM
kpitkpit
Thank you very much, A.V. !!!
 
My issue is solved by the following code. Please note the ConvertLead is only available as a database method.

 

// convert the lead

Database.LeadConvert lc = new database.LeadConvert();

lc.setLeadId(lead.Id);

LeadStatus convertstatus = [select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];

lc.setConvertedStatus(convertStatus.MasterLabel);

Database.LeadConvert[] lcArray = new Database.LeadConvert[] {lc};

Database.LeadConvertResult[] results = Database.convertLead(lcArray);

System.assert(results[0].IsSuccess());

 
KunlunKunlun

I want to do a trigger to forbid lead conversion to opportunity, if lead.xxx == yyy.

I need to write it as:
trigger forbid on lead (after update)
{
    //how can I check if this lead did converting operation.
    if(lead.xxx == yyy)
    {
        System.assert('xxxxxxxxxx');//throw error message.
        //now this lead should be converted to opportunity, and how can I roll it back to lead?
    }
}
kpitkpit

We use S-Control for the same purpose:

<script type="text/javascript">

// check if the Lead's Status is "Qualified" before allowing the user to convert it

if ({!NOT( ISPICKVAL(Lead.Status , "Qualified") )})

{

alert("The Lead Status must be 'Qualified' before you can convert the Lead to an Account.");

window.parent.location.href = "{!URLFOR( $Action.Lead.View , Lead.Id)}";

}

else

{

window.parent.location.href = "{!URLFOR( $Action.Lead.Convert , Lead.Id , [retURL= URLFOR( $Action.Lead.View , Lead.Id) ], true)}";

}

</script>

kpitkpit
There is boolean field - lead.IsConverted.  Once a lead is converted, not only this field is set to "True", but also Opportunity, Account, and Contact are created.
kpitkpit

If you really want to "Roll Back" a converted lead. Use these fields "lead.ConvertedOpportunityId, lead.ConvertedContactId, lead.ConvertedAccountId "  to delete the account, opportunity, and contact that generated during converting process, and set "lead.IsConverted" to false.

Again, we use S-Control instead of Apex Code for this purpose.

 

SimbaSimba
Is it possible to use these attributes lead.ConvertedOpportunityId and others, to identify which ones are just created and set the Newly created Contact (during the process of Convert Lead as the primary contact for the new Opp(created during lead process). Also, at what point can I use these features.

I would like to set the Contact as the primary when the user is converting lead into Opp. However, the Trigger on the Contact / Opp does not work during lead conversion process.

I have contacted the Salesforce Support people to give the option to Enable validation and Triggers on Lead Convert. But that's going quite slow.

Is there some other way to do this. ?

I really qppreciate a quick answer to this, since we have an immediate reqmt from the client for doing this.

kpitkpit
Did you mean set contact as primary on opportunityContactRole?  How about this work around?

1. Set a trigger on newly created Contact, and get the newContactId

2. Querry Lead table to see if the newContactId is converted

3. If it is, update Primary field on the OpportunityContactRole record.

trigger SetConvertedContactAsPrimary on Contact (after insert) {

       if (trigger.isInsert) {

                SetConvertedContactAsPrimary.setConvertedContactAsPrimary(Trigger.new);

         }

}

 

 

public class SetConvertedContactAsPrimary {

public static void setConvertedContactAsPrimary(Contact[] contacts) {

for(Contact c:contacts) {

try {

for (Lead lead:[Select l.Id, l.ConvertedOpportunityId From Lead l where l.IsConverted = True and l.ConvertedContactId = :c.Id limit 1]) {

if(lead.Id != null) {

for (OpportunityContactRole opptyCR:[Select o.IsPrimary, o.Id From OpportunityContactRole o where o.ContactId =:c.Id and o.OpportunityId = :lead.ConvertedOpportunityId and o.IsPrimary = false limit 1]) {

if (opptyCR.Id != null) {

opptyCR.IsPrimary = true;

update opptyCR;

}

}

}

}

} catch(System.QueryException e) {

}

}

}

}

 

 

 

 

SimbaSimba
Thanks a million for your suggestion and the effort that you had put in to write a sample code.  This has been very helful to resolve my issue..

However, following are some of th emajor changes I had to do to make the code work..
1. The trigger has to be on Lead After Update, rather than on Contact. This is because during lead conversion all the lead fields get updated way at the end, after the insertion of the records in Contacts / Opportunity.
2. Following is the code that I wrote to achieve the functionality that we need...

trigger BC_OnLeadUpdate on Lead (after update) {
    SetConvertedContactAsPrimary.convertedToPrimary(Trigger.new);   
}

public class SetConvertedContactAsPrimary {
public static void convertedToPrimary(Lead[] leads) {
System.debug('*****In SetConvertedContactAsPrimary.setConvertedContactAsPrimary');
        for (Lead lead:leads) {
            try {
System.debug('****l.Id ' + lead.Id);
Lead lead1 = [SELECT l.Id, l.Name, l.ConvertedOpportunityId, l.ConvertedContactId , l.isConverted FROM Lead l WHERE l.Id = :lead.Id limit 1];
if (lead1 != null) {
    System.debug ('****lead 1 ' + lead1.id);
    System.debug ('****lead ConvertedOpportunityId ' + lead1.ConvertedOpportunityId);
    System.debug ('****lead ConvertedContactId ' + lead1.ConvertedContactId);
    System.debug ('****lead isConverted ' + lead1.isConverted);
}   
            for (OpportunityContactRole opptyCR:[SELECT o.IsPrimary,  o.Id From OpportunityContactRole o WHERE o.ContactId = :lead.ConvertedContactId AND o.OpportunityId = :lead.ConvertedOpportunityId and o.IsPrimary = false limit 1]) {
                opptyCR.IsPrimary = true;
                update(opptyCR);                           
            }       
            //Get Opportunity Record Type selected by the user and then set that as the record type for this new opp
           

        }
            catch (System.QueryException e) {
                System.debug (e.getMessage());                   
            }         
        }
    }


}

Regards..