新记录的顶点触发器会创建数字

时间:2012-02-23 21:14:01

标签: salesforce apex-code

Log.add(new Cx_Trans_Log__c(
               Object__c = 'Activity',
               Object_Id__c = newActivity.Name,
               Owner2_Id__c = newActivity.Owner1_Id__c,

我在salesforce.com中更改活动时使用上面的代码创建新的日志记录。从newActivity.Owner1_Id__c创建的Owner2_Id__c没有给出Owner1_Id__c的名称(这是查找(用户)字段)。有没有办法获取Owner1中显示的用户名到Owner2?

1 个答案:

答案 0 :(得分:5)

正确的方法是进行一次查询。对于这个例子,我假设您的活动意味着任务:

Set<Id> userIds = new Set<Id>();
for (Task t : Trigger.new) {
    userIds.add(t.OwnerId);
}
Map<Id, User> users = new Map<Id, User>([SELECT Name FROM User WHERE Id IN :userIds]);

// then you could plug something like this into your existing code
String userName = users.get(newActivity.Onwer1_Id__c).Name;