插入前没有返回lead.Id的引导触发器

时间:2012-02-28 23:05:08

标签: salesforce apex-code

我有一个简单的触发器,它从引导对象中提取数据,并在创建或更新引导时将其放入自定义的“财务”对象中。由于某种原因,Trigger.new [i] .Id在触发器中返回NULL,在插入之前触发。

我疯了,因为我看不出为什么Id会为NULL的原因......这是我的代码:

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]){
  userList.put(tmp.Id,tmp);
}

for(integer i = 0; i<Trigger.new.size(); i++){

    string leadName = null;

    if(Trigger.new[i].FirstName != ''){
        leadName = Trigger.new[i].FirstName + ' ' + Trigger.new[i].LastName;
    }else{
        leadName = Trigger.new[i].LastName;
    }

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

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

    Finance__c financeRecord = new Finance__c(

        Name = leadName,
        Lead__c = leadId,
        Lead_Email__c = Trigger.new[i].Email,
        Lead_Phone__c = Trigger.new[i].Phone,
        Lead_Owner_Name__c  = ownerInfo.Name,
        Lead_Owner_Email__c = ownerInfo.Email,
        Lead_Owner_Phone__c = ownerInfo.Phone

    );

    insert financeRecord;

    }

}

提前感谢您提供任何帮助!

约什

1 个答案:

答案 0 :(得分:9)

  

由于某种原因,Trigger.new [i] .Id在触发器中返回NULL,在插入之前触发。

SFDC记录在插入之前没有ID。插入过程会创建ID。你可能想在插入后使用。