OpportunityContactRole数据访问

时间:2013-07-10 13:58:19

标签: triggers salesforce apex-code

我试图从一个机会对象中获取一个人的名字......这可能还是完全偏离基础?这是我已有的代码。我意识到从contactRole获取contactID并没有返回一个名字......我需要在那里获得一个名字?

trigger add_primary_advisor on Opportunity(before update) {

for(Opportunity o: Trigger.new){

     if (!Trigger.oldMap.get(o.id).IsClosed) {
       OpportunityContactRole contactRole =
            [select ContactID from OpportunityContactRole where IsPrimary = true and OpportunityId = :o.id];
       if (contactRole != null) {
         o.Primary_Advisor__c=contactRole.contactID;
       }
     }
   }    
}

1 个答案:

答案 0 :(得分:0)

 //If Contact.Name doesn't works try fetching Contact.FirstName, Contact.LastName 
   List<String> names = new List<String>();
    for(OpportunityContactRole contactRole =
        [select Contact.Name from OpportunityContactRole where IsPrimary = true and OpportunityId = :o.id]){
      names.add(contactRole .Contact.name)
  }