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
Prashant Yadav 90Prashant Yadav 90 

Attempt to de-reference a null object (custom object)

Hi All,

Following is the code for creating multiple child records.

This code is invoked using process builder and condition to satisfy for firing process builder and creating multiple records is choosing value called "Yes" under a picklist called "Approved" on Parent Object which is "Opportunity Contract".

In sandbox this Code is working fine with all custom profiles, when i give them modify all permission for both the custom objects.

In production this code is working for systems administrator alone and not working for other profiles even with modify all permission and i getting following error and not able to save the record for creating multiple child records.

Attempt to de-reference a null object

Here is the code, any suggestions are highly appriciated

public class OppConAMInvocableClass {
    @InvocableMethod(label='Insert Child Records for Opportunity Contracts' description='Inserts the child records based on number of contract years.')
  public static void insertChildRecords(List<Opportunity_Contract__c> parentList) {
      List<Annual_Maintenance__c> childrecords = new List<Annual_Maintenance__c>();        
      for(Opportunity_Contract__c parent : parentList) {
        Integer numberYears = Integer.valueOf(parent.Contract_Years__c);
        for(Integer i=1;i<numberYears;i++) {
        //Account acct = [SELECT Id FROM Account where Id = :e.WhatId];
        Integer j = i + 1;          
        Annual_Maintenance__c child = new Annual_Maintenance__c();
            
          
            Child.Anniversary_Date__c=Parent.Annual_Renewal_Date__c.addYears(i);  
            Child.Name=Parent.Name+'-'+Child.Anniversary_Date__c.year();
            Child.Number_of_sites__c=Parent.Number_of_Sites__c;
            Child.Opportunity_Department__c =Parent.Opportunity_Department__c;
            Child.Account__c=Parent.Account_Name__c;
            Child.Primary_Contact__c=Parent.Primary_Contact__c;
            Child.Annual_Maintenance_Start_Date__c=Parent.Contract_Start_Date__c;
            Child.Annual_Maintenance_End_Date__c=Parent.Contract_End_Date__c;
            Child.Opportunity_Name__c=Parent.Opportunity_Name__c;
            
            //rama
            Child.Anniversary_Date__c=Parent.Annual_Renewal_Date__c;
            Child.Sales_Territory__c=Parent.Sales_Territory__c;
            Child.Annual_Maintenance_Amount__c=Parent.SMS_Maintenance_Amount__c;
        child.Opportunity_Contract__c=parent.Id;
        childrecords.add(child);
        }
      }    
    Database.SaveResult[] results = Database.insert(childrecords);
    /*
    List<ID> itemIds = new List<ID>();
    for (Database.SaveResult result : results) {
      if (result.isSuccess()) {
        itemIds.add(result.getId());
      }
    }
    return itemIds;
    */
  }
}
Best Answer chosen by Prashant Yadav 90
Ajay K DubediAjay K Dubedi
Hi Prashant,

I have gone through your problem and found out following point:

Try recompiling all your classes after moving all your code to production org. Setup-->Apex classes-->Compile all classes.
This has worked before hoping same for you. For more please refer: https://developer.salesforce.com/forums/?id=9060G0000005Vu1QAE

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi