+ Start a Discussion
Prashant Yadav 90Prashant Yadav 90 

Code not working in production

What is wrong with my code?

Description: The purpose of this code is to create multiple records based on two conditions
  1. Custom Picklist: Value = Yes (condition set in process builder)
  2. (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 recomplied 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.Opportunity_Department__c =Parent.Opportunity_Department__c;
    Database.SaveResult[] results = Database.insert(childrecords);
    List<ID> itemIds = new List<ID>();
    for (Database.SaveResult result : results) {
      if (result.isSuccess()) {
    return itemIds;

Hi Prashant,

Can you check whether the new records have all the fields populated. Atleast all the fields that are populated in existing records(old ones) for those its working fine. I think you might not populating some fields in new records.

Hope this helps!