You need to sign in to do that
Don't have an account?
SteveOrg
System.NullPointerException: Attempt to de-reference a null object:
When I attempt to pull in the Name of an Account that is related to an Opportunity:
string s = trigger.newMap.get(oppty.AccountId).name;
I get the following error:
Error:Apex trigger updateOpptyCustomerWons caused an unexpected exception, contact your administrator: updateOpptyCustomerWons: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updateOpptyCustomerWons: line 31, column 1
Any ideas what I might be doing wrong?
trigger updateOpptyCustomerWons on Opportunity (after update) { //make a set to hold opportunity ids that we need Set<Id> opptyIds = new Set<Id>(); for(Opportunity o : trigger.new) { //check to see if our field has been changed if(o.StageName != trigger.oldMap.get(o.Id).StageName){ opptyIds.add(o.Id); } } if(!opptyIds.isEmpty()){ //get any opptys associated with the accounts that have been edited //Opportunity opp //Opportunity oppty = [SELECT Id,Name, StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :opptyIds limit 1]; List<Opportunity> opptyList = [SELECT Id,StageName,Name, AccountId FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :opptyIds ]; //include Name for(Opportunity oppty: opptyList){ Opportunity opp = new Opportunity(); //string s = oppty.name; string s = trigger.newMap.get(oppty.AccountId).name; OpportunityUpdateURL.hitTheServer(s); } } }
Your trigger is on opportunity, so you must supply opportunity id to the trigger.newmap.get() method. Also you need to get the Account's name in your query. Below corrections would help:
__r is used for relationship queries. Also, note the final line. You don't need the trigger.newmap method. (Just check the relationship name AccountId__r. You can easily find this out from your schema.)
All Answers
Your trigger is on opportunity, so you must supply opportunity id to the trigger.newmap.get() method. Also you need to get the Account's name in your query. Below corrections would help:
__r is used for relationship queries. Also, note the final line. You don't need the trigger.newmap method. (Just check the relationship name AccountId__r. You can easily find this out from your schema.)
Thanks so much for your reply. Since Account was a standard relationship I just needed Account.Name instead of Account__r.Name and everything worked.
Thanks again!