4

I'm trying to change the record type of a newly created account following lead conversion.

The documentation states that:

getAccountId() - Gets the ID of the account into which the lead will be merged.

However, the method returns nothing while I expect it to return the newly account's id.

for (Lead l : (List<Lead>)Trigger.New) {
    if (l.RecordTypeId == investorLeadRecordTypeId) {
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(l.id);
        lc.setConvertedStatus('Converted_to_Opportunity');
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());  

        Id accId = lc.getAccountId();
        accIds.add(accId);                 
    }
}
// code below isn't running because accIds is empty
if (!accIds.isEmpty()) {
    List<Account> accs = [SELECT Id,RecordTypeId FROM Account WHERE Id in: accIds];
    for (Account acc : accs) {
        acc.RecordTypeId = investorAccountRecordTypeId;
    }
    update accs;
}

1 Answer 1

2

The LeadConvert object won't have the Id for an Account that was to be created. Just use the LeadConvertResult.getAccountId() method instead - it will have the id used, whether it was a newly created or existing account. Only one additional letter needed in your code!

Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());  

Id accId = lcr.getAccountId();
accIds.add(accId);
1
  • Sweet! works like a charm, thank you.
    – Json
    Commented Nov 24, 2019 at 15:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .