从联系对象获取帐户名称

时间:2015-06-14 10:38:24

标签: triggers salesforce apex-code

请帮忙 我想从Contact(在iupdate之后)触发器的Account对象中获取Account.Name。在我的触发器下面。但是目前它只为c.Account.name ..

提供null
trigger Contactcallout on Contact (after update) {

Map<Id, String> m = new Map<Id, String>();

for (contact c : Trigger.new) {
if(c.RecordTypeId == '012D0000000BaFA'){
    contact old = Trigger.oldMap.get(c.Id);   
if (c.Email !=               old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.ph    one||c.Title__c!=old.Title__c||c.Account.name!=old.Account.name) {
    WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c            
.Title__c,c.Account.name);

}
}
}
}

1 个答案:

答案 0 :(得分:0)

无法从Trigger.new访问父字段值,您需要从SOQL中检索它们。检查下面的触发器:

trigger Contactcallout on Contact (after update) {
    
    Map<Id, String> m = new Map<Id, String>();

    for (Contact c : [SELECT Id, Email, FirstName, LastName, Phone, Title__c, Account.Name 
        FROM Account Where Id IN: Trigger.new]) {
        if(c.RecordTypeId == '012D0000000BaFA'){
            Contact old = Trigger.oldMap.get(c.Id);   
            if (c.Email != old.Email || c.FirstName != old.FirstName ||
                c.LastName != old.LastName || c.Phone != old.Phone ||
                c.Title__c != old.Title__c || c.Account.Name != old.Account.Name) {
                WebServiceCallout.sendNotification(c.Id, c.Email, 
                    c.FirstName, c.LastName, c.Phone, c.Title__c, c.Account.Name);

            }
        }
    }
}

尝试上面的代码,还有一件事你永远不应该硬编码顶点的记录ID。

(编辑:由于某种原因,它不允许我将触发器作为代码发布,因此将其作为HTML添加到评论中。)