1

I am using :

trigger.oldMap.get(op.ID).Owner.Name;

and it is returning me null. Is there a way I can get both old and new opportunity owner name. Using query I am able to get only the new owner name.

Map<ID,Opportunity> allOpp1 = new Map<ID,Opportunity>([Select ID, Account.ID, Opportunity.Owner.Name, Opportunity.Owner.Email, Opportunity.Owner.FIrstName, Owner.ID from Opportunity]);    
for(Opportunity op : trigger.new){

        String body = 'Dear '+allOpp1.get(op.Id).Owner.FirstName+'\n Opportunity: '+op.Name+' has be moved to the new Owner '+trigger.oldMap.get(op.ID).Owner.Name;               
}        

Thanks in advance.

2
  • Can you please provide more context? Trigger.oldMap is meant for this use case. Post your whole trigger Commented Oct 16, 2015 at 13:22
  • @BrianMansfield - I have added my trigger lines above.
    – xyz sfdc
    Commented Oct 16, 2015 at 14:22

1 Answer 1

3

You cannot get the parent or child information from trigger.new or trigger.old.

The trigger.new(Map)/trigger.old(Map) variable only has access to the fields in current object and no access to child object or lookup fields.

You will have to create a formula field (Lets say OwnerName__c) on the object (I guess your trigger is on opportunity so you'll have to create a formula field on opportunity) which gets the owner name from opportunity.

Then you can perform comparison

if(trigger.oldMap.get(op.ID).OwnerName__c != op.OwnerName__c ){
    //Perform something
}

Note this won't work for a before insert trigger.

I prefer a formula because it will take into account older opportunities also.

2
  • Thanks for your reply. is there no other way ?
    – xyz sfdc
    Commented Oct 16, 2015 at 14:23
  • Not that I can recall. If you transfer an opportunity through UI or transfer an account, you have an option to send notification automatically. Perhaps if it is sufficient for your use case, you can look into it.
    – manjit5190
    Commented Oct 16, 2015 at 15:10

You must log in to answer this question.

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