-3

What is wrong with my code?

Description: The purpose of this code is to create multiple records based on two conditions

Custom Picklist: Value = Yes (condition set in process builder)
(Number Field) Contract Term (Number of years) = 

So whatever is the value entered in the contract term custom field, those many child record would be created.

Parent Object: Opportunity Contract

Child Object: Annual Maintenance

Error in Production

This code is working fine in Developer Sandbox, after I installed in Production, this program is working as expected with the opportunity contract records which were previously created (before deploying this code)

However, this code does not work with opportunity contract records which are created after the deployment.

The error I am geting is “dereference a null object”

Where am I going wrong? Why this behaviour of working for existing records and not working with new records in Production?

Finally i have recompiled all Apex Classes in Production, but the result is the same

Any Help is highly appreciated!

Below is the code

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;
          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;
    */
  }
}


2
  • can you see if Anniversary_Date__c is null? Jun 28, 2019 at 12:09
  • Pranay, thanks let me check on this one, thanks for the suggestion Jun 28, 2019 at 12:21

1 Answer 1

0

The error you are getting means there's a specific object in your code that is null at runtime, and you are trying to get a property from that object, for example

Contact c = null;
if(c.Email.contains('a') >>> This will thrown an error because c is null

The error should include the exact line of code where this is happening, so you should look at that line and see what variable/object you are working with, and figure out why it's null.

If you cannot reproduce this in a sandbox, maybe try a full sandbox. Alternatively, once you've identified what is null (but don't know why), you can add a few system.debug statements in your code that handles that object, so that you can check the debug logs and find out why it's null.

2
  • Unfortunately error email does not state on which line variable or object is showing null value. btw code is working perfectly fine in my sandbox...not sure where the problem is Jun 28, 2019 at 12:20
  • If you have the developer console open when the error happens, you'll see the line of code. Jun 28, 2019 at 12:45

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.