+ Start a Discussion
JamieDJamieD 

Trigger to update Account

I am having an issue with something that sounds so simple to me.  Here is the APEX trigger:

 

trigger LinkTimeCard on Timecard__c (after insert) {
    System.debug('Starting...');
    for (Timecard__c tc : Trigger.new) {
        tc.Account = tc.Case__r.Account;
    }
}

 

The Timecard custom object has a lookup relationship with Account and Master-Detail relationship with Case.  The other odd thing to me is the following:

 

trigger LinkTimeCard on Timecard__c (after insert) {
    System.debug('Starting...');
    for (Timecard__c tc : Trigger.new) {
        System.debug(tc.Case__r.CaseNumber);
    }
}

 

That shows "null" in the debug log.  I can't really find any API documentation in APEX that shows a class library like you would find for Java or .NET.  Why can't I access the object Case__r?

Best Answer chosen by Admin (Salesforce Developers) 
soofsoof

Jamie,

 

The trigger records (that you're accessing via Trigger.new) should have the data in ALL their fields.  That said, in case of lookup fields, they only carry the ID of the parent object, and NOT the reference to the whole parent record (with all fields retrieved).  I hope I'm making sense here.

 

So if you need data from the parent record, here's what you need to do:

 

trigger LinkTimeCard on Timecard__c (after insert) {

System.debug('Starting...');

Map<ID, Case> caseMap =

new Map<ID, Case>([select Id, AccountId from Case where Id in : trigger.newMap.keySet()]);

for (Timecard__c tc : Trigger.new) {

tc.Account = caseMap.get(tc.Case__c).AccountId;

}

}

 

 

trigger LinkTimeCard on Timecard__c (after insert) {

System.debug('Starting...');

Map<ID, Case> caseMap =

new Map<ID, Case>([select Id, CaseNumber from Case where Id in : trigger.newMap.keySet()]);

for (Timecard__c tc : Trigger.new) {

System.debug(caseMap.get(tc.Case__c).CaseNumber);

}

}

 

Hope this helps!

 

-soof 

All Answers

Edwin VijayEdwin Vijay
__r is used only for custom objects i believe.. Try without the __r
soofsoof

Jamie,

 

The trigger records (that you're accessing via Trigger.new) should have the data in ALL their fields.  That said, in case of lookup fields, they only carry the ID of the parent object, and NOT the reference to the whole parent record (with all fields retrieved).  I hope I'm making sense here.

 

So if you need data from the parent record, here's what you need to do:

 

trigger LinkTimeCard on Timecard__c (after insert) {

System.debug('Starting...');

Map<ID, Case> caseMap =

new Map<ID, Case>([select Id, AccountId from Case where Id in : trigger.newMap.keySet()]);

for (Timecard__c tc : Trigger.new) {

tc.Account = caseMap.get(tc.Case__c).AccountId;

}

}

 

 

trigger LinkTimeCard on Timecard__c (after insert) {

System.debug('Starting...');

Map<ID, Case> caseMap =

new Map<ID, Case>([select Id, CaseNumber from Case where Id in : trigger.newMap.keySet()]);

for (Timecard__c tc : Trigger.new) {

System.debug(caseMap.get(tc.Case__c).CaseNumber);

}

}

 

Hope this helps!

 

-soof 

This was selected as the best answer