Explore Courses Blog Tutorials Interview Questions
0 votes
in Salesforce by (11.9k points)

I have a simple trigger which pulls data from the lead object and places it into a custom "Finance" object when the lead is created or updated. For some reason, the[i].Id is returning NULL in the trigger which fires before insert.

I'm going crazy because I can't see any reason why the Id would be NULL... Here's my code:

trigger FinanceNew on Lead (before insert) {

Map<String,User> userList = new Map<String,User>{};

// Places each returned user into a map with their sfID as the key

for(User tmp : [SELECT Id,Name,Email,Phone FROM User]){



for(integer i = 0; i<; i++){

    string leadName = null;

    if([i].FirstName != ''){

        leadName =[i].FirstName + ' ' +[i].LastName;


        leadName =[i].LastName;


    User ownerInfo = userList.get([i].OwnerId);

    ID leadId =[i].Id; // THIS IS RETURNING NULL!!

    Finance__c financeRecord = new Finance__c(

        Name = leadName,

        Lead__c = leadId,

        Lead_Email__c =[i].Email,

        Lead_Phone__c =[i].Phone,

        Lead_Owner_Name__c  = ownerInfo.Name,

        Lead_Owner_Email__c = ownerInfo.Email,

        Lead_Owner_Phone__c = ownerInfo.Phone


    insert financeRecord;



Thank you in advance for any help given!

1 Answer

0 votes
by (32.1k points)
Well, since you are talking about the[i].Id being returned as NULL in the trigger which actually fires before insert, the main reason here is that the Salesforce records don't have ID's before they are inserted. When the process of insertion is done, an ID is created. You would want to probably use this after insert.

Browse Categories