Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have written below code in my Opportunity after update trigger

Map<Id,Opportunity> newOpptyMap = Trigger.newMap;
Map<Id,Opportunity> oldOpptyMap = Trigger.oldMap;
Map<Id,Opportunity> removeShareMap = new Map<Id,Opportunity>();

for(Opportunity newOppty:newOpptyMap.values()){
        Opportunity oldOppty = oldOpptyMap.get(newOppty.Id);
        if(oldOppty.Account.fieldA__c == false && newOppty.fieldA__c == true){
            removeShareMap.put(newOppty.Id,newOppty);
        }
    }

But I am getting following error Error: Compile Error: Invalid field fieldA_c for SObject Opportunity fieldA_c is a custom field in Account object in my org.What am I doing wrong here ?

share|improve this question
 
If fieldA__c is on Account, it's never going to show a difference. If you're looking for something changing during Opportunity update, it will have to be an Opportunity field that changes, right? –  Jeremy Nottingham Sep 18 '13 at 16:36
add comment

1 Answer

up vote 2 down vote accepted
if(oldOppty.Account.fieldA__c == false && newOppty.Account.fieldA__c == true){
        removeShareMap.put(newOppty.Id,newOppty);
    }

The above should fix this issue.You are not traversing in the trigger.new record through Account

share|improve this answer
 
In this example, fieldA__c is not available from the triggering record, since it is on a related object. You would need to query for that Account. But value of the field is going to be the same before and after, since the Account isn't being updated here, the Opportunity is. –  Jeremy Nottingham Sep 18 '13 at 16:35
 
@Jeremy Nottingham, that means I can't pull related object data from Trigger context variables and need to query them explicitly ? –  codebandit Sep 19 '13 at 5:31
 
That's correct; related objects are not automatically available in triggers. It also means that if you're looking for a field change on the Account object, you need to use an Account Trigger. –  Jeremy Nottingham Sep 19 '13 at 14:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.