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
Parteek Kumar 5Parteek Kumar 5 

Issue with trigger which autoConvert lead into Person Account and Opportunity

Hi All,

I am trying to write a trigger which autoconvert lead in to person account and opportunity. this trigger is working fine on after Insert but when i am trying to add after update event then trigger is throwing error on both insert and update.
Error: update failed, reference id not found..
this is my code.
 
trigger testConvert on Lead (after insert,after update) {
    Set<String> leademails = new Set<String>();
    List<Lead> newLeads = new List<Lead>();
    String LastNme;

    // Get all the new leads
    if(trigger.isInsert)
    {
        for(Lead l : trigger.new){
            if(l.QuoteProductTitle__c != null){
                newLeads.add(l);
                leademails.add(l.email);
                LastNme = l.LastName;
                System.debug('firstDebug>>>>'+l);
            }
            else{
                System.debug('SecondDebug>>>>'+l);
            }
            
        }
    }
   else if 
(trigger.isUpdate){
        for(Lead l : trigger.New){
            if(l.QuoteProductTitle__c != null){
                newLeads.add(l);
                leademails.add(l.email);
                LastNme = l.LastName;
                System.debug('newLeadsDebug>>>>'+newLeads);
            }
            else{
                System.debug('SecondDebug>>>>'+l);
            }
            
        }
    }
    /* Make some maps of account and email addresses */
    List<Account> AccountList = [select Id, PersonEmail, OwnerId, LastName from Account where PersonEmail IN: leademails and LastName =:LastNme];
    Map<ID, String> peAccounts = new Map<ID, String>();
    Map<ID, ID> peAccountsOwner = new Map<ID, ID>();
  //  Map<ID, String> peAccountLName = new Map<ID, String>();

    // Generic map for preventing loss of ids
  //  if(AccountList.size() > 0){
        for(Account a : AccountList){
            peAccounts.put(a.id, a.PersonEmail);
            peAccountsOwner.put(a.id, a.OwnerId);
           // peAccountLName.put(a.id, a.LastName);
           System.debug('ThirdDebug>>>>'+a);
        }
  //  }
    // We will need this to get the id from the email address
    Map<String, ID> peAccountsFlipped = new Map<String, ID>();
    for(ID i : peAccounts.keyset()){
        peAccountsFlipped.put(peAccounts.get(i), i);
        System.debug('FourDebug>>>>'+i);
    }
    
    /* System Conversion Requirements */
    leadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
    List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
    Database.LeadConvert lc = new Database.LeadConvert();

    /* Configuring Payload */    
    for (Lead nl : newleads) {
        lc = new Database.LeadConvert();
        lc.setLeadId(nl.id);
        lc.setOverwriteLeadSource(false);
        lc.setConvertedStatus(convertStatus.MasterLabel);
        System.debug('FifthDebug>>>>');
        // Check to see if account already exists
        if(!peAccounts.isEmpty()){
            if(peAccountsFlipped.get(nl.email)!=null){
                lc.setAccountId(peAccountsFlipped.get(nl.email));
                lc.setOwnerId(peAccountsOwner.get(peAccountsFlipped.get(nl.email)));
                lc.setDoNotCreateOpportunity(false);
                lc.setOpportunityName(nl.QuoteProductTitle__c);
                System.debug('SixthDebug>>>>'+lc);
            }    
        } else {
            // In the event an account doesn't exist
            lc.setOwnerId(nl.OwnerId);
            lc.setDoNotCreateOpportunity(false);
            lc.setOpportunityName(nl.QuoteProductTitle__c);
            System.debug('SeventhDebug>>>>'+lc);
        }
        leadConverts.add(lc);
       //System.debug('leadConverts>>>>'+leadConverts);
    }

    // Fire Payload
    System.debug('leadConverts>>>>'+leadConverts);
   if(leadConverts.size()>0) 
   {
        system.debug('lead converted ');
        Database.LeadConvertResult[] lcr = Database.convertLead(leadConverts);
   }
    else
    system.debug('lead not converted');
   // System.debug(LoggingLevel.INFO, lcr);
}

 
James LoghryJames Loghry
The error is likely coming from lines 74, 75, or 82.  That error is typically related to setting a reference such as a lookup or M/D to another object.

I would take a close look at the "peAccountsFlipped" map to make sure that is populating with the correct values (and does not return null when you call it in lines 74 or 75 in your post above.