Whats the best process for converting leads with external ids?
I have an integration where I need to convert leads. The conversion needs to happen on an external id. I can't pass in the Salesforce id, that would be too easy. The best thing I've thought of is to write a webservice that takes a list of lead external ids, creates Database.LeadConvert objects for all the relevant, non converted leads, and then converts them.
This comes at a price though: If I convert leads in the same list, under the same account, I get a "Duplicate Id list" exception thrown.
Here's a code snippet of the web service:
List<Database.LeadConvert> leadConversions = new List<Database.LeadConvert>();
for(Lead l :
[Select
Id,
External_Id__c,
Contact__r.AccountId
From
Lead
Where
External_Id__c in :externalIds
and External_Id__c != null
and IsConverted=false
Limit 100]){
Database.LeadConvert lc = new Database.LeadConvert();
lc.setAccountId(l.Contact__r.AccountId);
lc.setContactId(l.Contact__r.Id);
lc.setLeadId(l.Id);
lc.setDoNotCreateOpportunity(true);
lc.setConvertedStatus(status);
leadConversions.add(lc);
leadExternalIds.add(l.External_Id__c);
}
List<LeadConversionResult> lcr = new List<LeadConversionResult>();
Integer i=0;
for(Database.LeadConvertResult r : Database.convertLead(leadConversions,false)){
lcr.add(
new LeadConversionResult(
r.isSuccess(),
r.getErrors().isEmpty() ? null : r.getErrors().get(0).getMessage(),
leadExternalIds.get(i++)
)
);
}
There is a soap convertLead call I can make, and that DOES handle leads under the same account properly. However, it only takes an Id instead of also external ids. Is there a best practice approach for integrating lead conversions like this, or am I on the right track?