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
visuvisu 

Null Pointer Exception On Before Insert Trigger?

The below trigger is throwing "System.NullPointer Exception.Attempt to de-reference a null object",  when there are no products associated with opportunity! When we add Opp to a custom object(detail side) , this trigger will fetch all the Products on this Opp to a field on this custom object,if there are no products sholud leave this field empty but on saving it is throwing the above error.Kindly help out with a solution for this. Thanks....

trigger productnames on cobj__c(before insert,before update) {
List<Id> oppIds = new List<Id>();
for(cobj__c co : Trigger.new)          //cobj__c is custom object
{
oppIds.add(co.Opportunity__c);    //Opportunity__C is the Opportunity master field on this object.
}
Map<Id,List<String>> oppIdToProdNameMap = new Map<Id,List<String>>();
for(OpportunityLineItem p2: [Select Id, Product2.Name,OpportunityId from OpportunityLineItem where OpportunityId in :oppIds])
{
   
     if(oppIdToProdNameMap.containsKey(p2.OpportunityId ))
      {
        List<String> l1 = oppIdToProdNameMap.get(p2.OpportunityId);
        l1.add(p2.Product2.Name);
        oppIdToProdNameMap.put(p2.OpportunityId,l1);
      }
     else
      {
        List<String> l1 = new List<String>();
        l1.add(p2.Product2.Name);
        oppIdToProdNameMap.put(p2.OpportunityId,l1);
      }
}
for(cobj__c co : Trigger.new)
{
 
    String s = '';
    boolean first = true;
    for(String p : oppIdToProdNameMap.get(co.Opportunity__c))
    {
    
        if(!first)
            s+=',';
        first = false;
        s += p;
      
    }
    co.Product_Names__c = s;  //field to populate
}
}
Best Answer chosen by visu
Pavan Kumar KajaPavan Kumar Kaja
Hi Visu,

You could write better code following best practices. I just added if condition before getting into the loop Opportunity lineitems are available or not.

Let me know if you have any questions.
trigger productnames on cobj__c(before insert,before update) {
	List<Id> oppIds = new List<Id>();
	for(cobj__c co : Trigger.new)          //cobj__c is custom object{
		oppIds.add(co.Opportunity__c);    //Opportunity__C is the Opportunity master field on this object.
	}

	Map<Id,List<String>> oppIdToProdNameMap = new Map<Id,List<String>>();
	
	for(OpportunityLineItem p2: [Select Id, Product2.Name,OpportunityId from OpportunityLineItem where OpportunityId in :oppIds]){
   
		if(oppIdToProdNameMap.containsKey(p2.OpportunityId )){
			List<String> l1 = oppIdToProdNameMap.get(p2.OpportunityId);
			l1.add(p2.Product2.Name);
			oppIdToProdNameMap.put(p2.OpportunityId,l1);
		}
		else{
			List<String> l1 = new List<String>();
			l1.add(p2.Product2.Name);
			oppIdToProdNameMap.put(p2.OpportunityId,l1);
		}			
	}

	if(!oppIdToProdNameMap.isEMpty()){
		for(cobj__c co : Trigger.new){
			String s = '';
			boolean first = true;
			for(String p : oppIdToProdNameMap.get(co.Opportunity__c)){
				if(!first)
					s+=',';
				first = false;
				s += p;
			}
			co.Product_Names__c = s;  //field to populate
		}
	}	
}



All Answers

Pavan Kumar KajaPavan Kumar Kaja
Hi Visu,

You could write better code following best practices. I just added if condition before getting into the loop Opportunity lineitems are available or not.

Let me know if you have any questions.
trigger productnames on cobj__c(before insert,before update) {
	List<Id> oppIds = new List<Id>();
	for(cobj__c co : Trigger.new)          //cobj__c is custom object{
		oppIds.add(co.Opportunity__c);    //Opportunity__C is the Opportunity master field on this object.
	}

	Map<Id,List<String>> oppIdToProdNameMap = new Map<Id,List<String>>();
	
	for(OpportunityLineItem p2: [Select Id, Product2.Name,OpportunityId from OpportunityLineItem where OpportunityId in :oppIds]){
   
		if(oppIdToProdNameMap.containsKey(p2.OpportunityId )){
			List<String> l1 = oppIdToProdNameMap.get(p2.OpportunityId);
			l1.add(p2.Product2.Name);
			oppIdToProdNameMap.put(p2.OpportunityId,l1);
		}
		else{
			List<String> l1 = new List<String>();
			l1.add(p2.Product2.Name);
			oppIdToProdNameMap.put(p2.OpportunityId,l1);
		}			
	}

	if(!oppIdToProdNameMap.isEMpty()){
		for(cobj__c co : Trigger.new){
			String s = '';
			boolean first = true;
			for(String p : oppIdToProdNameMap.get(co.Opportunity__c)){
				if(!first)
					s+=',';
				first = false;
				s += p;
			}
			co.Product_Names__c = s;  //field to populate
		}
	}	
}



This was selected as the best answer
visuvisu
Hi Ashi,

This worked for me! thanks a lot for the help. If possible please advise me with best practices to implement in this trigger. Thank you.