我有一个类似的问题,但希望所有当前和未来的用户字段都可用。由于 User 的自定义查找字段不受公式字段的限制,因此我在 Opportunity 和 Account 对象上创建了一个名为 OwnerLookup 的字段,然后使用触发器在创建或编辑时填充它。例如,机会触发器是这样的:
trigger OpportunityTrigger on Opportunity (before insert, after insert, before update, after update) {
if(trigger.isBefore && trigger.isInsert) {
OpportunityTriggerHandler.newOpportunity(Trigger.old, Trigger.new);
}
else if(trigger.isAfter && trigger.isInsert){
//OpportunityTriggerHandler.futureUse(Trigger.new);
}
else if(trigger.isBefore && trigger.isUpdate){
OpportunityTriggerHandler.updateOpportunity(Trigger.new, Trigger.oldMap);
}
else if(trigger.isAfter && trigger.isUpdate){
//OpportunityTriggerHandler.futureUse(Trigger.new, Trigger.oldMap);
}
}
OpportunityTriggerHandler 类(Apex 代码)是:
public with sharing class OpportunityTriggerHandler {
public static void newOpportunity( List<Opportunity> oldOpportunitys, List<Opportunity> newOpportunitys ) {
for (Opportunity opp: newOpportunitys) {
updateOwnerData( opp );
}
}
public static void updateOpportunity( List<Opportunity> oldOpportunitys, Map<Id, Opportunity> newOpportunitys ) {
for (Opportunity opp: oldOpportunitys) {
updateOwnerData( opp );
}
}
public static void updateOwnerData( Opportunity opp ) {
opp.OwnerLookup__c = opp.OwnerId;
}
}
然后,我在 Opportunity/Account 对象上创建公式字段以访问任何所有者(用户)对象字段,例如 Oppty Owner Name 公式字段:
OwnerLookup__r.FirstName & " " & OwnerLookup__r.LastName