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
DbjensenDbjensen 

How to add an Id to newly created record

Hello - I'm having issues adding an Id to another record. In a nutshell, I have a custom object called Lead Submission. When a Lead Submission is created, a standard Lead record is also created. How can I add the Id of the Lead Submission to the standard Lead record?

All of the code resides in a class and is called by a "before trigger". The code is a bit long but happy to post if it helps.

Any help would be greatly appreciated. 

 
NadavNadav
Seeing the code would help explain the flow of what you to. 
Generally, if you have a field in Lead that references the Lead Submission, you probably want to do something like this:

LeadSubmission__c ls = new LeadSubmission__c();
ls.Name='whatever'   <-- all the fields that you want to create
...
insert ls;
Lead l = new Lead();
l.LeadSubmission__c = ls.Id;

I've made some assumptions regarding the names of the custom objects and fields, but I've done something similar with a custom object that contains the Id of another object.

If that doesn't help, paste the code over.
DbjensenDbjensen
Hi Nadav - Thanks for the response. Yes, I have a lookup field on the standard lead record called "Lead Submission". Here is the code. I'm not sure how to add line numbers so I bolded the lines that you suggested. (Just scroll down a ways.)  After the lead is inserted, the "Lead Submission" field remains blank. 

Trigger:
 if (Trigger.isBefore) {
            if (Trigger.isInsert) {
             DuplicateLead.potentialDupes(Trigger.new);
 } 

Class:
public class DuplicateLeadSubmissionProcess {
    
    public static void dupeLeadSubmission (List<Lead_Submission__c> newLS) {
        
        Set<String> LastNameStreetAndZipCode = new Set<String>();
        Set<String> leadStatus = new Set<String>();
        List<Lead> leadsToInsert = new List<Lead>();
        Set<String> leadSourcesList = new Set<String>();
        List<ID> userId = new List<ID>();
        List<String> groupSalesCodeList = new List<String>();
        List<Account> accountsToInsert = new List<Account>();
        List<Contact> contactsToInsert = new List<Contact>();
        List<Lead_Submission__c> existingLeadSubmissions = new List<Lead_Submission__c>();
        List<Id> leadSubmissionIds = new List<Id>();
        String statusSubStatusCode = 'NC';
        DateTime statusDate = datetime.now();
        Date submittedDate = date.Today();
        
        for(Lead_Submission__c newLeadSubmission : newLS) {
            LastNameStreetAndZipCode.add(newLeadSubmission.LastNameStreetAndZipCode__c);
            leadStatus.add(newLeadSubmission.Lead_Status_For_TESTING__c);
            leadSourcesList.add(newLeadSubmission.Lead_Source_Text__c);
            userId.add(newLeadSubmission.OwnerId);
            groupSalesCodeList.add(newLeadSubmission.Group_Text__c);
            leadSubmissionIds.add(newLeadSubmission.Id);
        }
        
        System.debug('Lead Submission ' + LastNameStreetAndZipCode);
        System.debug('Lead Submission Status ' + leadStatus);
        System.debug('Lead Submission lead source ' + leadSourcesList);
        System.debug('Lead Submission User Id ' + userId);
        System.debug('Lead Submission Group Sales code ' + groupSalesCodeList);
        
        //Search for existing lead submission
        List<Lead_Submission__c> existingLS = [Select Id, LastNameStreetAndZipCode__c
                                               FROM Lead_Submission__c
                                               WHERE LastNameStreetAndZipCode__c =:LastNameStreetAndZipCode
                                               AND Lead_Status_For_TESTING__c =:leadStatus];
        
        System.debug('Existing lead submission ' + existingLS);
        
        Map<String, Lead_Submission__c> mapOfLeadSub = new Map<String, Lead_Submission__c>();
        for(Lead_Submission__c leadSubmissionFound : existingLS) {
            mapOfLeadSub.put(leadSubmissionFound.LastNameStreetAndZipCode__c, leadSubmissionFound);
        }
        
        //Search for lead source
        List<Lead_Source__c> leadSource = [SELECT ID, Name 
                                           FROM Lead_Source__c
                                           WHERE Name =:leadSourcesList];
        
        System.debug('lead source found ' + leadSource);
        
        Map<String, Lead_Source__c> mapOfLeadSources = new Map<String, Lead_Source__c>();
        for(Lead_Source__c leadSources : leadSource) {
            mapOfLeadSources.put(leadSources.Name, leadSources);
        }
        
        //Search for agent ID and SOB
        List<Agent__c> agent = [SELECT ID, User__c, SOB_Code__c 
                                FROM Agent__c
                                WHERE User__c =:userId];
        
        System.debug('agent found ' + agent);
        
        Map<String, Agent__c> mapOfagents = new Map<String, Agent__c>();
        for(Agent__c agentName : agent) {
            mapOfagents.put(agentName.User__c, agentName);
        }
        
        Map<String, String> mapOfagentsSOB = new Map<String, String>();
        for(Agent__c agentSOB : agent) {
            mapOfagentsSOB.put(agentSOB.User__c, agentSOB.SOB_Code__c);
        }
        
        //Search for sales code
        List<Sales_Code__c> salesCode = [SELECT ID, Name
                                         FROM Sales_Code__c
                                         WHERE Name =:groupSalesCodeList];
        
        System.debug('sales code found ' + salesCode);
        
        Map<String, Sales_Code__c> mapOfSOBs = new Map<String, Sales_Code__c>();
        for(Sales_Code__c groupSalesCode : salesCode) {
            mapOfSOBs.put(groupSalesCode.Name, groupSalesCode);
        }
        
        //Search for existing account
        List<Account> exsistingAccount = [Select Id, LastNameStreetAndZipCode__c FROM Account
                                          WHERE LastNameStreetAndZipCode__c IN : LastNameStreetAndZipCode];
        
        System.debug('account found ' + exsistingAccount);
        
        Map<String, Account> mapOfAccounts = new Map<String, Account>();
        for(Account acct : exsistingAccount) {
            mapOfAccounts.put(acct.LastNameStreetAndZipCode__c, acct);
        }
        
        //Update duplicate LS or Create new Lead and/or account and contact
        for(Lead_Submission__c newLeadSub : newLS) {
            try{
                if(mapOfLeadSub.containsKey(newLeadSub.LastNameStreetAndZipCode__c)) {
                    Lead_Submission__c retrievedId = mapOfLeadSub.get(newLeadSub.LastNameStreetAndZipCode__c);
                    newLeadSub.Duplicate__c = True;
                    newLeadSub.Duplicate_Lead_Submission__c = retrievedId.Id;
                } else {
                    Agent__c retrievedAgentId = mapOfagents.get(newLeadSub.OwnerId);
                    String retrieveAgentSOB = mapOfagentsSOB.get(newLeadSub.OwnerId);
                    Sales_Code__c retrievedSalesCodeId = mapOfSOBs.get(newLeadSub.Group_Text__c);
                    Lead_Source__c retrievedLeadSource = mapOfLeadSources.get(newLeadSub.Lead_Source_Text__c);
                    Lead createNewLead = new Lead();
                    createNewLead.Alt_City__c = newLeadSub.City__c;
                    createNewLead.Alt_Email__c = newLeadSub.Email__c;
                    createNewLead.Alt_First_Name__c = newLeadSub.First_Name__c;
                    createNewLead.Alt_Home_Phone__c = newLeadSub.Home_Phone__c;
                    createNewLead.Alt_Last_Name__c = newLeadSub.Last_Name__c;
                    createNewLead.Alt_State__c = newLeadSub.State_1__c;
                    createNewLead.Alt_Street__c = newLeadSub.Street__c;
                    createNewLead.Alt_Zip__c = newLeadSub.Postal_Code__c;
                    createNewLead.CHP_Graduation_Date__c = newLeadSub.CHP_Graduation_Date__c;
                    createNewLead.Cell_Phone__c = newLeadSub.Cell_Phone__c;
                    createNewLead.City = newLeadSub.City__c;
                    createNewLead.Company = newLeadSub.Last_Name__c;
                    createNewLead.Consultant_Notes__c = newLeadSub.Comments__c;
                    createNewLead.Current_Insurance_Company__c = newLeadSub.Current_Insurance_Company_1__c;
                    createNewLead.Effective_Timestamp__c = statusDate;
                    createNewLead.Email = newLeadSub.Email__c;
                    createNewLead.Expiration_Date__c = newLeadSub.Expiration__c;
                    createNewLead.Facility_Name__c = newLeadSub.Facility_Name__c;
                    createNewLead.Facility_State__c = newLeadSub.Facility_State_1__c;
                    createNewLead.Facility_Zip_Code__c = newLeadSub.Facility_Postal_Code__c;
                    createNewLead.Field_Agent_SOB__c = retrieveAgentSOB;
                    createNewLead.Field_Agent__c = retrievedAgentId.Id;
                    createNewLead.FirstName = newLeadSub.First_Name__c;
                    createNewLead.Group_Sales_Code__c = newLeadSub.Group_Text__c;
                    createNewLead.Group_Sales_Code_2__c = retrievedSalesCodeId.Id;
                    createNewLead.Home_Phone__c = newLeadSub.Home_Phone__c;
                    createNewLead.LastName = newLeadSub.Last_Name__c;
                    createNewLead.Lead_Source_Text__c = newLeadSub.Lead_Source_Text__c;
                    createNewLead.Lead_Source__c = retrievedLeadSource.Id;
                    createNewLead.Lead_Submission__c = newLeadSub.Id;
                    createNewLead.Lead_Type__c = newLeadSub.Lead_Type_Text__c;
                    createNewLead.Meeting_Date__c = newLeadSub.Meeting_Date__c;
                    createNewLead.Meeting_Type__c = newLeadSub.Meeting_Type_Code__c;
                    createNewLead.PostalCode = newLeadSub.Postal_Code__c;
                    createNewLead.State = newLeadSub.State__c;
                    createNewLead.Status_Code__c = statusSubStatusCode;
                    createNewLead.Street = newLeadSub.Street__c;
                    createNewLead.Submitted_Date__c = submittedDate;
                    createNewLead.Substatus_Code__c = statusSubStatusCode;
                    createNewLead.Urgent_New__c = newLeadSub.Urgent_1__c;
                    createNewLead.VIP_New__c = newLeadSub.Executive_VIP_1__c;
                    createNewLead.Work_Extension__c = newLeadSub.Work_Extension__c;
                    createNewLead.Work_Phone__c = newLeadSub.Work_Phone__c;
                    leadsToInsert.add(createNewLead);    
                    
                    if(!mapOfAccounts.containsKey(newLeadSub.LastNameStreetAndZipCode__c)) {
                        Account createNewAccount = new Account();
                        createNewAccount.BillingCity = newLeadSub.City__c;
                        createNewAccount.BillingPostalCode = newLeadSub.Postal_Code__c;
                        createNewAccount.BillingState = newLeadSub.State_1__c;
                        createNewAccount.BillingStreet = newLeadSub.Street__c;
                        createNewAccount.Home_Phone__c = newLeadSub.Home_Phone__c;
                        createNewAccount.Name = newLeadSub.Last_Name__c;
                        accountsToInsert.add(createNewAccount);
                        
                        Contact createNewContact = new Contact();
                        createNewContact.AccountId = createNewAccount.Id;
                        createNewContact.Best_Time_to_Call__c = newLeadSub.Best_Time_to_Call__c;
                        createNewContact.Cell_Phone__c = newLeadSub.Cell_Phone__c;
                        createNewContact.Email = newLeadSub.Email__c;
                        createNewContact.FirstName = newLeadSub.First_Name__c;
                        createNewContact.Gender__c = newLeadSub.Gender__c;
                        createNewContact.LastName = newLeadSub.Last_Name__c;
                        createNewContact.MailingCity = newLeadSub.City__c;
                        createNewContact.MailingPostalCode = newLeadSub.Postal_Code__c;
                        createNewContact.MailingState = newLeadSub.Postal_Code__c;
                        createNewContact.MailingStreet = newLeadSub.Street__c;
                        createNewContact.Primary_Contact__c = true;
                        contactsToInsert.add(createNewContact);
                    }
                }
            } catch (Exception e){
                System.debug('ERROR:' + e);
            }      
        }
        
        try{
            insert accountsToInsert;
            insert contactsToInsert;
            insert leadsToInsert;
        } catch (DMLException e){
            for(Account acct : accountsToInsert) {
                acct.addError('There was a problem inserting accounts');
            }
            for(Contact ct : contactsToInsert) {
                ct.addError('There was a problem inserting contacts');
            }
            for(Lead dl : leadsToInsert) {
                dl.addError('There was a problem inserting leads');
            }
        } 
        
        //Update existing lead submission
        for(Lead_Submission__c resubmitLS : existingLS) {
            if(!existingLS.isEmpty()) {
                try {
                    resubmitLS.Resubmitted__c = True;
                    existingLeadSubmissions.add(resubmitLS);   
                } catch (Exception e){
                    System.debug('Error updating lead submission ' + e);
                }      
            }
        }
        update existingLeadSubmissions;
    }
}