You need to sign in to do that
Don't have an account?
Trigger Error - NullPointerException
I am using a trigger to update an associated record whenever the first record is modified. I am only testing this on one field at the moment.
On the first record, I make a change to the data, then when I click save, I expect it to update the related record (but not necessarily inform me that it did so).
I'm getting this error:
Error:Apex trigger Updated_Associated_Project caused an unexpected exception, contact your administrator: Updated_Associated_Project: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Updated_Associated_Project: line 49, column 43
Any suggestions as to the cause?
Here's my code:
trigger Updated_Associated_Project on Opportunity (after update) { // The purpose of this trigger is to pass values from the Opportunity to the Project so that // users don't have to manually update both when changes are made. // This trigger will pass all related values to the associated Project. // Future idea: Inform user of number of fields that were updated? // Should this just mass update all of them, or review each field to see if it's different // and then update? // ----------------------------------------------------------------------- // Locate project that has this Opportunity as its parent // ------------ code recycled from sf code cookbook------------ // The map allows us to keep track of the Opportunities that have // changes to update the Projects with Map<Id, Opportunity> OppsThatHaveBeenUpdated = new Map<Id, Opportunity>(); // Trigger.new is a list of the Opportunities that will be updated // This loop iterates over the list, and adds any that have had // changes to the OppsThatHaveBeenUpdated map. for (Integer i = 0; i < Trigger.new.size(); i++) { if ( (Trigger.old[i].Acquisition_Specifications__c != Trigger.new[i].Acquisition_Specifications__c)) { OppsThatHaveBeenUpdated.put(Trigger.old[i].id, Trigger.new[i]); } } List<Summary__c> updatedProjects = new List<Summary__c>(); for (Summary__c s : [SELECT Id, Acquisition_Specifications__c FROM Summary__c WHERE Opportunity__c in :OppsThatHaveBeenUpdated.keySet()]) { Opportunity parentOpportunity = OppsThatHaveBeenUpdated.get(s.Id); s.Acquisition_Specifications__c = parentOpportunity.Acquisition_Specifications__c; // Add projects to list and bulk update updatedProjects.add(s); } update updatedProjects; }
It appeasrs you built your map using the opportunity id as the key, but it appears you are trying to "get" values from the map using the Summary__c id.
I think you would want your second loop to look more like this:
List<Summary__c> updatedProjects = new List<Summary__c>(); for (Summary__c s : [SELECT Id,Opportunity__c, Acquisition_Specifications__c FROM Summary__c WHERE Opportunity__c in :OppsThatHaveBeenUpdated.keySet()]) { Opportunity parentOpportunity = OppsThatHaveBeenUpdated.get(s.Opportunity__c); s.Acquisition_Specifications__c = parentOpportunity.Acquisition_Specifications__c; // Add projects to list and bulk update updatedProjects.add(s); } update updatedProjects; }
All Answers
It appeasrs you built your map using the opportunity id as the key, but it appears you are trying to "get" values from the map using the Summary__c id.
I think you would want your second loop to look more like this:
List<Summary__c> updatedProjects = new List<Summary__c>(); for (Summary__c s : [SELECT Id,Opportunity__c, Acquisition_Specifications__c FROM Summary__c WHERE Opportunity__c in :OppsThatHaveBeenUpdated.keySet()]) { Opportunity parentOpportunity = OppsThatHaveBeenUpdated.get(s.Opportunity__c); s.Acquisition_Specifications__c = parentOpportunity.Acquisition_Specifications__c; // Add projects to list and bulk update updatedProjects.add(s); } update updatedProjects; }
Jim:
Thank you, thank you, thank you.
Your modifications worked perfectly and now I have my first successfully running trigger!
Jenna