0

We are using apex to convert a lead to a person account. We are getting the following error when we do this. This code is executed on a trigger on the update of the lead. We have a custom button on the lead detailed page which on clicked would call this method using javascript.

Error message: ConvertLead failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, cannot associate a contact with a consumer account:

Code below..

Webservice static void Error(ID LeadId) {       
       lead l = [SELECT id,ownerId,Account_Name__c 
                 FROM lead 
                 WHERE id =:leadid];

       Account Borrowercontactid=[SELECT id,PersonContactId 
                                 FROM Account 
                                 WHERE Id = :l.Account_Name__c limit 1 ];
       id leadid1= l.id;
       id accid= l.Account_Name__c;

        try{
                Database.LeadConvert leadConvert1 = new Database.LeadConvert();
                            leadConvert1.setOwnerId(l.ownerId);
                            leadConvert1.setLeadId(leadid1);
                            leadConvert1.setAccountId(accid);
                            leadConvert1.setContactId(null);
                            leadConvert1.setDoNotCreateOpportunity(true);
                            LeadStatus convertStatus = [SELECT Id, MasterLabel                     
                                                        FROM LeadStatus 
                                                        WHERE IsConverted=true LIMIT 1];

                            leadConvert1.setConvertedStatus(convertStatus.MasterLabel);

                            system.debug('$$$$$$$$$$444'+ leadConvert1);
                            Database.LeadConvertResult lcr = Database.convertLead(leadConvert1);

       }catch(Exception e){
            system.debug('Exception:'+e);  
       }                      
  }

Can you guys tell me what am i missing in this method? The Database.convertLead is failing at the moment..

1 Answer 1

1
If you are converting a lead into a person account, do not specify setContactId or an error will result. Specify only setAccountId of the person account.

please delete

leadConvert1.setContactId(null);

It should help

You must log in to answer this question.

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