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
GoForceGoGoForceGo 

Where is the bug, if any?

I have a master object and it's detail.When this code executes, it is crashing at mo.Contact != null complaining about null pointer reference - i.e master object is null.

I fail to see why this would be, given that I got the master object through a query to database and put it in a map. I am also assuming that salesforce would never try to insert/update a detail object unless the master object field was non-null.

 

Additional info:

 

I  am UPSERTING the detail object using DATA LOADER. So I have an external id on detail object. I also have an external id on the parent object and I am matching the parent based on external id of the parent. When the parent object has been deleted, I usually get an external id not found error.

 

trigger updateDetailObject on Detail_Object__c (before insert, before update) {
     
    Detail_Object__c[] DetailObjects = Trigger.New;
    
    Set<Id> MasterObjectIds = new Set<Id> ();

    for (Detail_Object__c do:DetailObjects ) {
       MasterObjectIds.add(do.Master_Object__c);
    }

   Map<Id, Master_Object__c> MasterObjectMap = new Map<Id, Master_Object__c> ([select Contact__c from Master_Object__c where id in :MasterObjectIds]);
 
   for (Detail_Object__c do:DetailObjects) {    
       Master_Object__c mo = MasterObjectMap.get(do.Master_Object__c);
       if (mo.Contact__c != null) { //crashes here. why would master object map not have the entry in it? 
          ...
       }
   }
}

 

 

*werewolf**werewolf*

What is the variable called ol?  Your code says

 

if (ol.Contact__c != null)

 

But there's no evidence that any variable called ol was initialized.

GoForceGoGoForceGo

 I had edited the code and changed it in most places, but forgot that spot. My original code had "ol". I changed it to mo for the posting.

 

The problem still there...I just tried inserting a dummy detail record without the master set...seems like salesforce let's you do it and I do hit this trigger...

 

I could have sworn that it could not be done and salesforce run time would catch it before any trigger is hit..did salesforce change something recently ?

 

 

GoForceGoGoForceGo

I am still puzzled on what happened. Most of my customers over last 2 years frequently delete the master record and they have never seen the crash. I saw the crash for the first time yesterday.

 

As I said, I am using the data loader. I upsert the detail records and the set the external id for the master record (i.e the field Master_Object__External_ID__c). In past, when the master record is deleted, I get the following failed message:

 

Upsert failed. First exception on row 0; first error: INVALID_FIELD, Foreign key external ID: junk21324211 not found for field Master_Object__External_ID__c in entity Master_Object__c.

 

The before insert/before upsert trigger NEVER executes.

 

Somehow it executed this time with Master_Record__c field set to null!!!

 

 

*werewolf**werewolf*

What happens if you tweak around the metadata a little to set it back to version 22 or 21 or something?  If there's a change in Apex or how objects are treated, that should catch it.

GoForceGoGoForceGo

I am using a fairly over version of data loader - which means probably API version of somewhere in the 12-16 - I can't remember.

 

I am also looking at object security issues - perhaps this customer had security settings where master object or master object fields were not visible the dataloader user.