Get Account name from contact object

1.6k views Asked by At

Please help I want to get the Account.Name from the Account object from the Contact (after iupdate) Trigger. Below my trigger.but currently its giving only null for c.Account.name..

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

There are 1 answers

0
Raul On

Parent fields values are not accessible from Trigger.new, you need to retrieve them from SOQL. Check the trigger below:

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);

            }
        }
    }
}

Try above code, and one more thing you should never hardcode a record id in apex.

(Edit: For some reason it doesn't allow me to post the trigger as Code, So added it as a HTML to the comment.)