0

I am running into an error message when attempting to bulkify my trigger. The trigger is on a custom object, Account_Status_Monitoring__c, that has a lookup field, Account__c, to the Account object. The trigger uses this field to query some data on two other custom objects, Financials__c & Risk_Factors__c. However, I am getting a "...attempt to de-reference a null object.." message.

Here is the trigger:

trigger UpdateASMfmFinancials on Account_Status_Monitoring__c (before insert) {

  // Financials
  Map<Id,Financials__c> finMap = new Map<Id,Financials__c>();
  for(Account_Status_Monitoring__c asm : Trigger.new)
  {
    finMap.put(asm.Account__c, null);       
  }

  finMap.remove(null);
  finMap.putAll([SELECT 
          Id, 
          OG_Group_12_Mth_Avg_MTD_Sales__c, 
          OG_Group_MTD_Sales_Generic__c
      FROM 
          Financials__c 
      WHERE 
          Account__c IN :finMap.keyset()]);



  // Risk Factors
  Map<Id,Risk_Factors__c> rfMap = new Map<Id,Risk_Factors__c>();
  for(Account_Status_Monitoring__c asm : Trigger.new)
  {
    rfMap.put(asm.Account__c, null);
  }

  rfMap.remove(null);
  rfMap.putAll([SELECT 
          Id, 
          Stage__c, 
          Status__c,
          OSR_Comments__c,
          Competitor__c,
          Reason__c,
          Effective_Date__c,
          RecordType.Name,
          CreatedDate,
          LastModifiedDate
      FROM 
          Risk_Factors__c 
      WHERE 
          Account__c IN :finMap.keyset()]);


  for(Account_Status_Monitoring__c asm : Trigger.new)
  {
      if(finMap.containsKey(asm.Account__c) && asm.Account__c != null)
      {
          asm.fin_Group_12_Mth_Avg_MTD_Sales__c = finMap.get(asm.Account__r.Id).OG_Group_12_Mth_Avg_MTD_Sales__c;
          asm.fin_Group_MTD_Sales_Generic__c = finMap.get(asm.Account__c).OG_Group_MTD_Sales_Generic__c;
      }

      if(rfMap.containsKey(asm.Account__c))
      {
          asm.rf_Stage__c = rfMap.get(asm.Account__c).Stage__c;
          asm.rf_Status__c = rfMap.get(asm.Account__c).Status__c;
          asm.rf_OSR_Comments__c = rfMap.get(asm.Account__c).OSR_Comments__c;
          asm.rf_Competitor__c = rfMap.get(asm.Account__c).Competitor__c;
          asm.rf_Reason__c = rfMap.get(asm.Account__c).Reason__c;
          asm.rf_Effective_Date__c = rfMap.get(asm.Account__c).Effective_date__c;
          asm.rf_Record_Type__c = rfMap.get(asm.Account__c).RecordType.Name;
          asm.rf_Risk_Factor_Created_Date__c = rfMap.get(asm.Account__c).CreatedDate;
          asm.rf_Risk_Factor_Last_Modified_Date__c = rfMap.get(asm.Account__c).LastModifiedDate;
      }     
  }}

Line 51 is "asm.fin_Group_12_Mth_Avg_MTD_Sales__c = finMap.get(asm.Account__r.Id).OG_Group_12_Mth_Avg_MTD_Sales__c;"

Any help would be greatly appreciated, thank you.

1

Normally checking containsKey will suffice to protect you from these errors. However, you subverted this protection with this loop:

for (MyObject__c asm : trigger.new)
    finMap.put(asm.Account__c, null);
finMap.putAll([/*query*/]);

Remove it and just do:

finMap = new Map<Id, Finances__c>([/*query*/]);
| improve this answer | |
  • Thank you, Adrian Larson......this got rid of my error message but it is not populating the field with any data. asm.fin_Group_12_Mth_Avg_MTD_Sales__c = finMap.get(asm.Account__r.Id).OG_Group_12_Mth_Avg_MTD_Sales__c; asm.fin_Group_MTD_Sales_Generic__c = finMap.get(asm.Account__c).OG_Group_MTD_Sales_Generic__c; – Billw Oct 6 '16 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.