function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ivan WinzerIvan Winzer 

converting lead to person account

So we have a lead convert class that is now throwing a Cannot Insert Update error. It says that is is Unable to create/Update field: Name. Below is a part of the code that i believe is the issue but im not 100% sure on how to tweak it so that it doesnt try to place first and last name into the name field but doesnt break the code.
 
if(leadsToConvert.size()>0){
            List<Database.LeadConvertResult> results = Database.convertLead( leadsToConvert, false);
            List<Account> accountsToUpdate = new List<Account>();
            for(Database.LeadConvertResult result : results){
                lead l = leadIdMap.get(result.getLeadId());
                if(result.success && result.getAccountId()!=null){
                    String name = ((l.FirstName!=null)?l.FirstName+' '+l.LastName:l.LastName);
                    if(name!=null){
                        accountsToUpdate.add(new Account(Id=result.getAccountId(), name = name));
                    }
                }
                else{
                    for(Database.Error error : result.getErrors()){
                        l.addError(error.getMessage());
                    }
                }
            }

Any help or thoughts is grately appreciated. 

Ivan
Best Answer chosen by Ivan Winzer
PriyaPriya (Salesforce Developers) 

Hi Ivan,

Can you try with this below sample code:-

 

TriggerOnConvertLeads Trigger:-

trigger TriggerOnConvertLeads on Lead (after insert, after update) {
    List<lead> leadList = trigger.new;
    Set<ID> leadIds= new Set<ID>();
    for(lead leadRecord : leadList) {
        if (leadRecord.Status == ‘Qualified’ && leadRecord.isConverted == false){
              
             leadIds.add(leadRecord.Id);
        }
    //system.debug('size'+ leadIds.size());
    if(leadIds.size()  > 0){
              ConvertLeads.LeadAssign(leadIds);
    }
}

ConvertLeads class:- 
Public class ConvertLeads
{
   
    public static void LeadAssign(Set<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);
                Leadconvert.setDoNotCreateOpportunity(TRUE); 
                //you can remove this line if you want to create an opportunity during  conversion 
                MassLeadconvert.add(Leadconvert);
        }
        
        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        }
    }}

Please mark it as Best Answer so that it can help others in the future.

Regards,

Priya Ranjan




 

All Answers

PriyaPriya (Salesforce Developers) 

Hi Ivan,

Can you try with this below sample code:-

 

TriggerOnConvertLeads Trigger:-

trigger TriggerOnConvertLeads on Lead (after insert, after update) {
    List<lead> leadList = trigger.new;
    Set<ID> leadIds= new Set<ID>();
    for(lead leadRecord : leadList) {
        if (leadRecord.Status == ‘Qualified’ && leadRecord.isConverted == false){
              
             leadIds.add(leadRecord.Id);
        }
    //system.debug('size'+ leadIds.size());
    if(leadIds.size()  > 0){
              ConvertLeads.LeadAssign(leadIds);
    }
}

ConvertLeads class:- 
Public class ConvertLeads
{
   
    public static void LeadAssign(Set<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);
                Leadconvert.setDoNotCreateOpportunity(TRUE); 
                //you can remove this line if you want to create an opportunity during  conversion 
                MassLeadconvert.add(Leadconvert);
        }
        
        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        }
    }}

Please mark it as Best Answer so that it can help others in the future.

Regards,

Priya Ranjan




 
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47

Hi Ivan Winzer

I am Sharing a link Please check it. it will help you

https://trailblazers.salesforce.com/answers?id=90630000000DI3pAAG#:~:text=If%20your%20organization%20uses%20person,to%20the%20new%20person%20account.


Please mark it as Best Answer so that it can help others in the future.

Thanks 

Suraj Tripathi