function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VaderVader 

Trigger Error: System.NullPointerException: Attempt to de-reference a null object

Our opportunity model utilizes parent and child opportunities that are stored on the same object with a self lookup of the parent opp on the child opp record.  There is a one to many relationship between parent (the project) and child (the products) opportunites.

 

I am trying to create a trigger that will take values from the Parent Opp and propagate them down to the child opp and am getting an error and can't quite figure it out.  Code below:

 

 

 

trigger updateChildOppParentValues on Opportunity (before update) {

Map<id,Opportunity> mapOpp = new map<id,Opportunity>([select id, StageName, CloseDate, AccountId, Cellular_Chipset__c, Configuration__c, Chipset_Provider__c,
Design_House__c, EAU__c, Lifetime_Volume_units__c, Market_Segment__c, Mass_Production_Date__c,
Probability, Program_Lifetime_mos__c, Region_Carrier__c, Amount, Wifi_Chipset__c
from Opportunity where id =: trigger.new]);

List<Opportunity> toUpdate = new list<Opportunity>();

For(Opportunity opp :[select id from Opportunity where Project_Opportunity__c =:trigger.new]) {
Error occurring on this line:  toUpdate.add(new Opportunity(id=opp.Id, Project_Cellular_Chipset__c = mapOpp.get(opp.id).Cellular_Chipset__c,Project_Chipset_Configuration__c=mapOpp.get(opp.id).Configuration__c,Project_Chipset_Provider__c=mapOpp.get(opp.id).Chipset_Provider__c,Project_Customer_Parent_Location__c=mapOpp.get(opp.id).Design_House__c,Project_Design_Win_Target_Date__c=mapOpp.get(opp.id).CloseDate,Project_EAU__c=mapOpp.get(opp.id).EAU__c,Project_Lifetime_Volume_units__c=mapOpp.get(opp.id).Lifetime_Volume_units__c,Project_Market_Segment__c=mapOpp.get(opp.id).Market_Segment__c,Project_Mass_Production_Date__c=mapOpp.get(opp.id).Mass_Production_Date__c,Project_Probability__c=mapOpp.get(opp.id).Probability,Project_Program_Lifetime_mos__c=mapOpp.get(opp.id).Program_Lifetime_mos__c,Project_Region_Carrier__c=mapOpp.get(opp.id).Region_Carrier__c,Project_Stage__c=mapOpp.get(opp.id).StageName,Project_Total_Value__c=mapOpp.get(opp.id).Amount,Project_WiFi_Configuration__c=mapOpp.get(opp.id).Wifi_Chipset__c));

}

If (toUpdate.size()>0) update toUpdate;

}

 

 

It's probably something really silly but any help would be appreciated!

Bhawani SharmaBhawani Sharma
Can you add following line before adding element in list
if(mapOpp.containsKey(opp.id) && mapOpp.get(opp.id) != null) {
toUpdate.add(.......);
}
VaderVader

Found the solution via a friends advice...  

 

I needed to be mapping on the Project_Opportunity__c value, so I added it to the query and changed the get(opp.id) to get(opp.Project_Opportunity__c) since I wanted the data from the project opp to be put into the child opp.

 

Here's the working code...

 

trigger updateChildOppParentValues on Opportunity (after update) {	
	
	Map<id,Opportunity> mapOpp = new map<id,Opportunity>([select id, StageName, CloseDate, AccountId, Cellular_Chipset__c, Configuration__c, Chipset_Provider__c,Design_House__c, EAU__c, Lifetime_Volume_units__c, Market_Segment__c, Mass_Production_Date__c,Probability, Program_Lifetime_mos__c, Region_Carrier__c, Amount, Wifi_Chipset__c, Project_Opportunity__c													from Opportunity where id =: trigger.new]);
	
	List<Opportunity> toUpdate = new list<Opportunity>();	
	
	For(Opportunity opp :[select id, Project_Opportunity__c from Opportunity where Project_Opportunity__c =:trigger.new]) {		
			
		toUpdate.add(new Opportunity(id=opp.Id, Project_Cellular_Chipset__c = mapOpp.get(opp.Project_Opportunity__c).Cellular_Chipset__c,
												Project_Chipset_Configuration__c=mapOpp.get(opp.Project_Opportunity__c).Configuration__c,                      						Project_Chipset_Provider__c=mapOpp.get(opp.Project_Opportunity__c).Chipset_Provider__c,                        						Project_Customer_Parent_Location__c=mapOpp.get(opp.Project_Opportunity__c).Design_House__c,                        						Project_Design_Win_Target_Date__c=mapOpp.get(opp.Project_Opportunity__c).CloseDate,                        						Project_EAU__c=mapOpp.get(opp.Project_Opportunity__c).EAU__c,                       						Project_Lifetime_Volume_units__c=mapOpp.get(opp.Project_Opportunity__c).Lifetime_Volume_units__c,                        						Project_Market_Segment__c=mapOpp.get(opp.Project_Opportunity__c).Market_Segment__c,                        						Project_Mass_Production_Date__c=mapOpp.get(opp.Project_Opportunity__c).Mass_Production_Date__c,                        						Project_Probability__c=mapOpp.get(opp.Project_Opportunity__c).Probability,                        						Project_Program_Lifetime_mos__c=mapOpp.get(opp.Project_Opportunity__c).Program_Lifetime_mos__c,                        						Project_Region_Carrier__c=mapOpp.get(opp.Project_Opportunity__c).Region_Carrier__c,                        						Project_Stage__c=mapOpp.get(opp.Project_Opportunity__c).StageName,                        						Project_Total_Value__c=mapOpp.get(opp.Project_Opportunity__c).Amount,                        						Project_WiFi_Configuration__c=mapOpp.get(opp.Project_Opportunity__c).Wifi_Chipset__c));

	}
	
	If (toUpdate.size()>0) update toUpdate;
	
}