문제

Is there a standard way of converting Leads with the same AccountId in Salesforce. When trying to do it with the following code, I get the error "Duplicate id in list"

List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
for(Lead myLead : leadsToConvert){
    Database.LeadConvert lc = new database.LeadConvert();

    if (accountMap.get(myLead.Company) <> null) {

        lc.setLeadId(myLead.Id);
        lc.setDoNotCreateOpportunity(true);
        lc.setAccountId(accountMap.get(myLead.Company).id);
        lc.setOverwriteLeadSource(true);

        //if we match to a contact, merge
        if (contactMap.get(myLead.Email) != null && myLead.Email != null) {
            lc.setContactId(contactMap.get(myLead.Email).id);
        }               

        lc.setConvertedStatus(convertStatus.MasterLabel);
        leadConverts.add(lc);           
    }
}

if(!leadConverts.isEmpty()){
    Database.convertLead(leadConverts, false);
}

This code only fails when I pass in a List of leads that have the same AccountID. I get the "Duplicate id in list" error.

Why can I not do this?
Is there a standard practice/ way to avoid this?

도움이 되었습니까?

해결책

You're code looks correct, converting multiple leads to the same account should work just fine.

Most likely your leadsToConvert list has the same lead more than once. You can quickly validate this by using a map to prevent duplicates.

List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
Map<Id, Lead> leadMap = new Map<Id, Lead>(leadsToConvert);
for(Lead myLead : leadMap.values()){
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top