2

I have written the common lead conversion snippets within for loop and I got this Error. Yes following code creates Account and Contacts properly but it doesnt create any Opportunity.

ERROR :

ConvertLead failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

Here Is my Code

Database.LeadConvert objLeadConvert = new Database.LeadConvert();
        LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
        List<Database.LeadConvert> leadsToConvert = new List<Database.LeadConvert>();

        for (Lead myLead : selectedItems)
        {   //From the information of first lead create new account
            Account objAccount1= new Account();
            objAccount1.Name = myLead.Company;
            objAccount1.Phone = myLead.Phone;
            objAccount1.BillingCity = myLead.City;
            objAccount1.BillingCountry = myLead.Country;
            objAccount1.BillingPostalCode = myLead.PostalCode;
            objAccount1.BillingState = myLead.State;
            objAccount1.BillingStreet = myLead.Street;
            objAccount1.Fax = myLead.Fax;
            objAccount1.Rating = myLead.Rating;
            objAccount1.NumberOfEmployees = myLead.NumberOfEmployees;
            objAccount1.Website = myLead.Website;
            insert objAccount1;

            //From the information of first lead create new Contact
            Contact objContact1=new Contact();
            objContact1.FirstName=myLead.FirstName;
            objContact1.LastName=myLead.LastName;
            **objContact1.AccountId=objAccount1.id;**
            insert objContact1;

            objLeadConvert.setConvertedStatus(convertStatus.MasterLabel);
            objLeadConvert.setLeadId(myLead.Id);
            objLeadConvert.setAccountId(objAccount1.id);
            objLeadConvert.setContactId(objContact1.id);
            objLeadConvert.setDoNotCreateOpportunity(false);
            objLeadConvert.setOpportunityName(myLead.Company);
            if (objAccount.OwnerId != null)
            {
                objLeadConvert.setOwnerId(objAccount.OwnerId);
            }
            System.Debug('------ Leads to Convert---'+objLeadConvert);
            leadsToConvert.add(objLeadConvert);
            System.Debug('------ListLeadstoConvert---'+leadsToConvert);


            try{
                ConvertResult= Database.convertLead(objLeadConvert,true);
               return new PageReference('/');// + listConvertResult[0].getContactId());
        }
        catch(Exception ex){
}
4
  • Where you populating the selectedItems list? – highfive Aug 22 '14 at 7:35
  • I have used standard Controller, and am assiging selected leads into "selectedItems", It shows selectedItems properly on VF page too. – Salesforce Steps Aug 22 '14 at 9:32
  • And there is no any other Trigger or Validations on Lead or anyother objects. – Salesforce Steps Aug 22 '14 at 12:24
  • objContact1.AccountId=objAccount1.id; I have edited question & made this line bold, Is this Mapping causing an any issue ???? – Salesforce Steps Aug 26 '14 at 5:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.