2

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 ?

1
  • 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? Commented Sep 18, 2013 at 16:36

1 Answer 1

1
if(oldOppty.Account.fieldA__c == false && newOppty.Account.fieldA__c == true){
        removeShareMap.put(newOppty.Id,newOppty);
}

You are not traversing in the trigger.new record through Account as above.

The above code also assumes that you have to query the fields on the Account using a SOQL before traversing.

Also note you will need to make sure to query for the Related fields on Account when working on the Opportunity object.

3
  • 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. Commented Sep 18, 2013 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
    Commented Sep 19, 2013 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. Commented Sep 19, 2013 at 14:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .