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
kladizkovkladizkov 

Changing the lead to "Converted" using Apex Trigger

Hi,

 

I had a requirement to automatically insert/update Contact when a new lead comes in. So, I wrote a before update trigger. Everything worked great, but I need the lead to get into "Converted" state once I have the trigger finish its job. Is there any way to mark it as Converted inside the trigger code?

Best Answer chosen by Admin (Salesforce Developers) 
kenfkenf

Ibascle,

 

In the code above, the LeadConvert object has a method called "setOwnerId"... that is where you specify the ID of one of the users in your org, prior to calling LeadConvert.

 

--k 

All Answers

kladizkovkladizkov
Basically, I need to "make" a lead as converted using Apex. Is it possible to do it?
jkucerajkucera

Yes:

 http://www.salesforce.com/us/developer/docs/apexcode/index.htm

 

 

Database Method Example

swfobject.registerObject("clippy.d6314e523", "9");
Lead myLead = new Lead(lastname = 'Fry', company='Fry And Sons');
insert myLead;

Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(myLead.id);

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());
jkucerajkucera

Basic Steps for Converting Leads

Converting leads involves the following basic steps:

  1. Your application determines the IDs of any lead(s) to be converted.
  2. Optionally, your application determines the IDs of any account(s) into which to merge the lead. Your application can use SOQL to search for accounts that match the lead name, as in the following example:
    swfobject.registerObject("clippy.d6314e149", "9");
    select id, name from account where name='CompanyNameOfLeadBeingMerged'
  3. Optionally, your application determines the IDs of the contact or contacts into which to merge the lead. The application can use SOQL to search for contacts that match the lead contact name, as in the following example:
    swfobject.registerObject("clippy.d6314e154", "9");
    select id, name from contact where firstName='FirstName' and lastName='LastName' and accountId = '001...'
  4. Optionally, the application determines whether opportunities should be created from the leads.
  5. The application queries the LeadSource table to obtain all of the possible converted status options (SELECT ... FROM LeadStatus WHERE IsConverted='1'), and then selects a value for the converted status.
  6. The application calls convertLead.
  7. The application iterates through the returned result or results and examines each LeadConvertResult object to determine whether conversion succeeded for each lead.
  8. Optionally, when converting leads owned by a queue, the owner must be specified. This is because accounts and contacts cannot be owned by a queue. Even if you are specifying an existing account or contact, you must still specify an owner.
kladizkovkladizkov

Hi,

 

The code below converts the lead and works fine for me. 

 

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);
Database.LeadConvertResult lcr = Database.convertLead(lc);

 

But, is it possible to set the status of the lead to "converted" without actually converting it. This is because, I create an custom object from the lead. After creating the custom object, I don't want the lead to be "convertable" but should be available in lead reports.

 

I tried skipping the convertLead() call, but it didn't work for me. Any idea, how to accomplish this?

jkucerajkucera

You could have a dummy contact & account that you merge all leads into when converting to avoid dupes in the system.  Also, you could delete the newly created contact & account with a trigger if that's easier.

 

I don't believe you can simply convert without creating any objects or merging into others however.  A 3rd option would be a custom field you use for reporting in replacement of the IsConverted field.

 

lbasclelbascle

\"Optionally, when converting leads owned by a queue, the owner must be specified. This is because accounts and contacts cannot be owned by a queue. Even if you are specifying an existing account or contact, you must still specify an owner."

 

i am having that very problem, how do i specify an owner?

Message Edited by lbascle on 11-06-2009 12:44 PM
kenfkenf

Ibascle,

 

In the code above, the LeadConvert object has a method called "setOwnerId"... that is where you specify the ID of one of the users in your org, prior to calling LeadConvert.

 

--k 

This was selected as the best answer