+ Start a Discussion
tengeltengel 

Please help: Attempt to de-reference a null object

Hello. The trigger below updates a custom Activity field for Event called "New_Business_Opportunity__c" to true if the Related To field of the event is an Opportunity with a certain record type. My org uses LinkPoint360 to sync Outlook calendar items to Salesforce, and a number of users have been experiencing issues when LinkPoint attempts to create Salesforce events using Outlook calendar items. Salesforce items do not get created, and the following error is captured in some event logs:

 

Apex script unhandled trigger exception by user/organization: 00560000001SP8S/00D60000000JyqG

updateOppEventCheckbox: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.updateOppEventCheckbox: line 8, column 1

 Here is the trigger code:

trigger updateOppEventCheckbox on Event(before insert, before update) {
  map<id,opportunity> opportunities = new map<id,opportunity>();
  for(event record:trigger.new)
    if(record.whatid!=null&&record.whatid.getsobjecttype()==opportunity.sobjecttype)
    opportunities.put(record.whatid,null);
  opportunities.putAll([select id,recordtype.developername from opportunity where id in :opportunities.keyset()]);
  for(event record:trigger.new)
    record.New_Business_Opportunity__c = opportunities.containskey(record.whatid) && opportunities.get(record.whatid).recordtype.developername.contains('Standard');
}

 

Can anyone tell me if there is a way to optimize this code so that the error clears? Some users are losing Outlook calendar data, so this is a potentially big problem :(

 

Thank you!

bob_buzzardbob_buzzard

In this line:

 

record.New_Business_Opportunity__c = opportunities.containskey(record.whatid) && opportunities.get(record.whatid).recordtype.developername.contains('Standard');

 you are assuming that there will always be a recordtype for the opportunity - is that the case?  

tengeltengel
Hi Bob. Yes, all Opportunities created in our org will have a record type at the time of creation.
souvik9086souvik9086

Please check NULL for record.whatId before the following line

 

record.New_Business_Opportunity__c = opportunities.containskey(record.whatid) && opportunities.get(record.whatid).recordtype.developername.contains('Standard');

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

tengeltengel
Souvik, thank you for the reply. I sort of lifted this code from another developer and am not really good with Apex. Can you clarify what I need to put before that line?
souvik9086souvik9086

Try like this

 

for(event record:trigger.new){

if(record.whatid != NULL){
record.New_Business_Opportunity__c = opportunities.containskey(record.whatid) && opportunities.get(record.whatid).recordtype.developername.contains('Standard');
}
}


If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks