FIELD_INTEGRITY_EXCEPTION, Account ID: id value of incorrect type: 00Q2w000006RwzQEAS: [AccountId]: Class.Lead_Contact.AfterInsert: line 14, column 1

Whenever a new Lead created create a corresponding contact with the same data.

public class Lead_Contact {
    
    public static void AfterInsert(List<Lead> leads){
        
        List<Contact> contacts= new List<Contact>();
        for(Lead l: leads){
             contact c = new Contact();
            c.LastName=l.LastName;
            c.Phone=l.Phone;
            c.Email=l.Email;
            c.FirstName=l.FirstName;
            c.AccountId=l.Id;
            contacts.add(c);
        }insert contacts;
    }

}

// trigger
trigger Lead_Contact on Lead (after insert) {
      Lead_Contact.AfterInsert(trigger.new);
}

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

As @DavidReed suggested consider using the LeadConvert class to perform the conversion instead of writing a Trigger.

Invoke the following Apex class using Process Builder

Public class AutoConvertLeads {     
 @InvocableMethod   
 public static void LeadAssign(List<Id> LeadIds) {         
    LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];    
    List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();    
    for(id currentlead: LeadIds){                
        Database.LeadConvert Leadconvert = new Database.LeadConvert();    
        Leadconvert.setLeadId(currentlead);                               
        Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);             
        
        MassLeadconvert.add(Leadconvert);    
    }                  
        if (!MassLeadconvert.isEmpty(){            
         List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);       
        }    
 } 
}


@isTest 
  public class TestAutoConvertLeads{
    static testMethod void createnewlead() {
    User myUserToCreate = [Select id from user where profile.name='System Administrator' Limit 1];
  
    Test.startTest();    
    Lead myLeadToCreate =new Lead();
    List<id> Ids= New List<Id>();
    myLeadToCreate.ownerid= myUserToCreate.id;
    myLeadToCreate.LastName ='Harrington';
    myLeadToCreate.Company='The Factory';
    myLeadToCreate.LeadSource='Web';
    myLeadToCreate.Rating='';
    myLeadToCreate.Status='';
    insert myLeadToCreate; 
  
    Ids.add(myLeadToCreate.id);
    AutoConvertLeads.LeadAssign(Ids);
  
    Test.stopTest();
  }
}

Ref: LeadConvert

Solution 2

The error message tells you exactly what is wrong here. You are assigning a value to the AccountId field that is not an Account Id (in fact, it is a Lead Id).

c.AccountId=l.Id;

Not sure what you intended to do there but this won’t work. I would encourage reconsidering this objective; it makes very little sense to copy Leads into Contacts. You might wish to perform a Lead conversion instead, in specific situations where that makes sense for your business processes.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply