+ Start a Discussion
pcmca_2007@yahoo.co.inpcmca_2007@yahoo.co.in 

Opportunity Trigger Error

Hi All,

          I have created a lead and tried to convert it into opportunity.I have a trigger on opportunity that throws an error

          "Attempt to de-reference a null object".I want to know why it is throwing error and is "Attempt to de-reference a null  object" and "Null pointer exception of java is same.I have java backgrount and just started working on salesforce.

 

Code for my Trigger is as below:

 

 

    trigger CompOldNew on Opportunity (before insert,before update) {
    Map<Id,String> oppIDs=new Map<Id,String>();
   
    for(Opportunity opp : Trigger.new){
                    //Create an old and new map so that we can compare values
                                   
                     Opportunity oldOpp= Trigger.oldMap.get(opp.ID);
                     Opportunity newOpp = Trigger.newMap.get(opp.ID);
            System.debug('oldOppoldOpp&&&&&&'+oldOpp+'newOppnewOpp$$$$$'+newOpp);
                    //Retrieve the old and new Reseller Email Field           
                    string oldResellerEmail = oldOpp.Reseller_Email__c;
                    string newResellerEmail = newOpp.Reseller_Email__c;
   System.debug('oldResellerEmail==='+oldResellerEmail+'newResellerEmail==='+newResellerEmail);           
                    //If the fields are different, the email has changed
                    if(oldResellerEmail != newResellerEmail){
                        oppIDs.put(opp.Id,opp.Reseller_Email__c);
                          
                    }
                    System.debug('MAP VALUE======'+oppIDs);
                   
}

List<Opportunity> ls=new List<Opportunity>([select name,Reseller_Email__c from Opportunity where id in :Trigger.oldMap.keySet()]);
System.debug('XXXXXXXXXXXXXXXXXXXXXX'+ls);


}

 

 

With Regards

Prabhash Mishra

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Your trigger is declared as before insert as well as before update.  However, insert triggers have no concept of previous values as the record is new to the database.  Therefore I'd imagine its your use of trigger.old that is the problem.  

 

How to fix this depends on your use case - if a new record indicates that the email has "changed", you can simply exclude new records from the tests:

 

 trigger CompOldNew on Opportunity (before insert,before update) {
    Map<Id,String> oppIDs=new Map<Id,String>();
   
    if (trigger.isUpdate)
    {
       for(Opportunity opp : Trigger.new){
                    //Create an old and new map so that we can compare values
                                   
                     Opportunity oldOpp= Trigger.oldMap.get(opp.ID);
                     Opportunity newOpp = Trigger.newMap.get(opp.ID);
            System.debug('oldOppoldOpp&&&&&&'+oldOpp+'newOppnewOpp$$$$$'+newOpp);
                    //Retrieve the old and new Reseller Email Field           
                    string oldResellerEmail = oldOpp.Reseller_Email__c;
                    string newResellerEmail = newOpp.Reseller_Email__c;
   System.debug('oldResellerEmail==='+oldResellerEmail+'newResellerEmail==='+newResellerEmail);           
                    //If the fields are different, the email has changed
                    if(oldResellerEmail != newResellerEmail){
                        oppIDs.put(opp.Id,opp.Reseller_Email__c);
                          
                    }
System.debug('MAP VALUE======'+oppIDs);
} } else { // new records - add all oppIds.addAll(trigger.new); } List<Opportunity> ls=new List<Opportunity>([select name,Reseller_Email__c from Opportunity where id in :Trigger.oldMap.keySet()]); System.debug('XXXXXXXXXXXXXXXXXXXXXX'+ls); }

 

 

 

All Answers

Ankit AroraAnkit Arora

Can you please tell in which line you are facing error?

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

bob_buzzardbob_buzzard

Your trigger is declared as before insert as well as before update.  However, insert triggers have no concept of previous values as the record is new to the database.  Therefore I'd imagine its your use of trigger.old that is the problem.  

 

How to fix this depends on your use case - if a new record indicates that the email has "changed", you can simply exclude new records from the tests:

 

 trigger CompOldNew on Opportunity (before insert,before update) {
    Map<Id,String> oppIDs=new Map<Id,String>();
   
    if (trigger.isUpdate)
    {
       for(Opportunity opp : Trigger.new){
                    //Create an old and new map so that we can compare values
                                   
                     Opportunity oldOpp= Trigger.oldMap.get(opp.ID);
                     Opportunity newOpp = Trigger.newMap.get(opp.ID);
            System.debug('oldOppoldOpp&&&&&&'+oldOpp+'newOppnewOpp$$$$$'+newOpp);
                    //Retrieve the old and new Reseller Email Field           
                    string oldResellerEmail = oldOpp.Reseller_Email__c;
                    string newResellerEmail = newOpp.Reseller_Email__c;
   System.debug('oldResellerEmail==='+oldResellerEmail+'newResellerEmail==='+newResellerEmail);           
                    //If the fields are different, the email has changed
                    if(oldResellerEmail != newResellerEmail){
                        oppIDs.put(opp.Id,opp.Reseller_Email__c);
                          
                    }
System.debug('MAP VALUE======'+oppIDs);
} } else { // new records - add all oppIds.addAll(trigger.new); } List<Opportunity> ls=new List<Opportunity>([select name,Reseller_Email__c from Opportunity where id in :Trigger.oldMap.keySet()]); System.debug('XXXXXXXXXXXXXXXXXXXXXX'+ls); }

 

 

 

This was selected as the best answer
pcmca_2007@yahoo.co.inpcmca_2007@yahoo.co.in

Hi,

    Thanks for your help.Your suggested code has solved my problem.

 

 

With Regards

Prabhash Mishra