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
EJWEJW 

ConvertLead doesn't work in ApexCode or with the Flex API.

LeadConvert isn’t considered a valid class in Apex Code.  Database.ConvertLead was released with Winter ’08 but using that call requires you pass in a single LeadConvert object or an array of LeadConvert objects but the compiler doesn’t recognize the class.  I’ve verified that I’m compiling it using the 11.1 URL.

The documentation states that ConvertLead is untested in the Flex API, but I'd like to confirm that it doesn’t work.  I’m getting an error message: “Invalid Lead ID” when I attempt to use it.  I’ve tried with 15 and 18 character IDs and verified that every ID being passed (account, contact, lead, owner) are all valid.  The Ids themselves are pulled directly from SFDC via the API using the same session the browser.   I’ve verified the whole process by stepping through it in the Flex debugger but without digging through the Flex API source directly I’m not sure what the issue is.

Here’s sample Flex code that’s failing, along with the error message:

Error:

Message:            valid leadId is required
statusCode:        INVALID_CROSS_REFERENCE_KEY

 Code:

private function convertSingleLead( leadId:String, accountId:String, contactId:String, ownerId:String ):void
{
      var converter:LeadConvert = new LeadConvert;
      
      converter.accountId = accountId;
      converter.contactId = contactId;
      converter.doNotCreateOpportunity = true;
      converter.overwriteLeadSource = true;
      converter.convertedStatus = “Closed – Converted”;
      converter.ownerId = ownerId;
      converter.leadId = leadId;

      apex.convertLead( new Array( converter ), new AsyncResponder( postConversion, sfdcCallFailed ) );
}

Thanks,

Ron WildRon Wild

Yes, I'm seeing the same problem.   I checked to make sure I had the latest release of the toolkit - and I do.

The sample code also raises this error.

 

 

EJWEJW
Ron, I resolved this issue (at least in Apex Code).  Basically the problem is that I didn't release the LeadConvert class is a sub-class of the Database class for some reason.  So to convert a lead in Apex Code this sample should get you started:

public void ConvertLead()
{
  Database.LeadConvert[] leadsToConvert = new Database.LeadConvert[0];
  Database.LeadConvert converter;

  converter = new Database.LeadConvert();
  converter.setLeadId( <lead id here> );
  converter.setAccountId( <account id here> );
  converter.setContactId( <contact id here> );
  converter.setOwnerId( <owner id here> );
  converter.setConvertedStatus( <status here> );
  converter.setDoNotCreateOpportunity( true );
  leadsToConvert.add( converter );

  Database.ConvertLead( leadsToConvert, true );
}


Ron WildRon Wild
Thanks, E.J. ... that's good to know on the Apex side.   It doesn't seem to help for the current Flex project though.

I saw a post from about a year ago about the same issue, and they had a release to the toolkit that was supposed to fix it.  But I'm wondering if it broke again, when they sub-classed LeadConvert under Database.

Can someone post a quick update to the Flex Toolkit to get us rolling again???

Regards,
EJWEJW
I didn't bother with fixing the lead convert call with Flex, I just wrote an Apex web service to do the lead conversion, then called the web service from Flex.
IC-TannerIC-Tanner
Hi everyone,

I am trying to utilize the convertlead method but am not familiar with the database utilization. I'm trying to run a trigger than converts a single or group of lead records. I receive the following error:

Data integrity: to save an EntityObject, you need to have it locked during load, or else you may overwrite someone else's changes. 
 
Class Code: 
 
public class tester {

public static void ConvertLead(lead[] leads)
{
  Database.LeadConvert[] leadsToConvert = new Database.LeadConvert[0];
  Database.LeadConvert converter = new Database.LeadConvert();
for (lead l : leads){
  
  converter.setLeadId(l.id);
  account account = new account(name='test');
  insert account;
  converter.setAccountId(account.id);
  contact contact = new contact(firstname='test', lastname='test', accountid=account.id);
  insert contact;
  converter.setContactId(contact.id);
  //converter.setOwnerId('00570000000oflK');
  converter.setConvertedStatus('Qualified - Converted');
  converter.setDoNotCreateOpportunity( true );
  leadsToConvert.add( converter );
 }
  Database.ConvertLead( leadsToConvert, true );
}

 
public class tester {public static void ConvertLead(lead[] leads){  Database.LeadConvert[] leadsToConvert = new Database.LeadConvert[0];  Database.LeadConvert converter = new Database.LeadConvert();for (lead l : leads){    converter.setLeadId(l.id);  account account = new account(name='test');  insert account;  converter.setAccountId(account.id);  contact contact = new contact(firstname='test', lastname='test', accountid=account.id);  insert contact;  converter.setContactId(contact.id);  //converter.setOwnerId('00570000000oflK');  converter.setConvertedStatus('Qualified - Converted');  converter.setDoNotCreateOpportunity( true );  leadsToConvert.add( converter ); }  Database.ConvertLead( leadsToConvert, true );}} 
Trigger Code
 
trigger tester on Lead (after insert) {

tester.convertLead(trigger.new);

trigger tester on Lead (after insert) {tester.convertLead(trigger.new); }
 
If I uncomment the following class code line:
  //converter.setOwnerId('00570000000oflK');
 
I receive the following error:
java.sql.SQLException: ORA-20025: ORA-06512: at "BASHFUL.SACCESS", line 916 ORA-06512: at "BASHFUL.SACCESS", line 981 ORA-06512: at "BASHFUL.SLEAD", line 1488 ORA-06512: at line 1 : {call sLead.update_leads(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)})} 
 
Any help with being able to implement this would be greatly appreciated.
 
Thanks! 
Message Edited by IC-Tanner on 02-12-2009 05:57 AM
Message Edited by IC-Tanner on 02-12-2009 05:57 AM
Message Edited by IC-Tanner on 02-12-2009 06:01 AM
TannerTanner

Hi everyone,

 

I figured out what the problem was. It was based on the assigned rules checkbox being checked and also that the default lead owner was a queue. 

PamSalesforcePamSalesforce

Hi everyone,

 

I am trying to implement the apex lead convertion.Here is what i have done.

I have created Custom List Button, which calls the javacsript

 {!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
 {!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
  var records= {!GETRECORDIDS($ObjectType.Lead)};
  sforce.apex.execute("tester","ConvertLead",{leads:records});

 

the code ofr the class is :

global class tester {
    WebService static void ConvertLead(Id[] leads){
         Database.LeadConvert[] leadsToConvert = new Database.LeadConvert[0];
         Database.LeadConvert converter = new Database.LeadConvert();
         for (Id l : leads){
             converter.setLeadId(l);
                //converter.setLeadId(l.id);
                System.debug(1);
                account account = new account(name='test');
                insert account;
                System.debug(2);
                converter.setAccountId(account.id);
                contact contact = new contact(firstname='test', lastname='test', accountid=account.id);
                insert contact;
                System.debug(3);
                converter.setContactId(contact.id);
                //converter.setOwnerId('00570000000oflK');
                converter.setConvertedStatus('Closed - Converted');
                converter.setDoNotCreateOpportunity( true );
                leadsToConvert.add( converter );
         }
         Database.ConvertLead( leadsToConvert, true );
    }
    public static testMethod void testbasicAuthCallout(){
    System.debug('Calling test');
     Lead le=new Lead(Email='svreddych@gmail.com', Company='Saksoft',LastName='reddy');
     insert le;
     List<Id> idList = new List<Id>();
     idList.add(le.Id);
     System.debug(idList);
    tester.ConvertLead(idList);
    }
}

 

But i am getting the following error:

A problem with the OnClick JavaScript for this button or link was encountered:

{faultcode:'soapenv:Client', faultstring:'System.DmlException: ConvertLead failed. First exception on row 1; first error: CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead: []

Class.MileageTrack.tester.ConvertLead: line 22, column 4
External entry point', }

 

Please help me