CJ Teuben
CJ Teuben

Reputation: 970

Apex trigger for new record creates number

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

I am using the above code to create a new log record when an activity is changed in salesforce.com. The Owner2_Id__c that is created from newActivity.Owner1_Id__c doesn't give me the name of the Owner1_Id__c (this is a lookup(User) field). Is there a way to get the User name that is displayed in Owner1 to Owner2?

Upvotes: 1

Views: 792

Answers (1)

Jeremy Ross
Jeremy Ross

Reputation: 11600

The right way to do it is to make one query. For this example, I'll assume by activities you mean Tasks:

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;

Upvotes: 5

Related Questions