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
Maggie Farley 20Maggie Farley 20 

Help with NullPointerException

Hello,

Can someone help point out where the NullPointerException occurs? This class is invocable from a process that gets the Opportunity Line Item ID for created and updated records where Product End Date = null. 
 
public with sharing class invocable_productEndDate 
{
  
  /*Params to be passed from different process builder processes from varying objects. 
  None required, because each process will execute slightly different logic and use different params.*/
  public class productVariables 
  {
    @InvocableVariable
    public Id opptyLineItemId;
  }

  @InvocableMethod
  public static void productEndDate(List<productVariables>  Vars)
  {
    List<Id> oppLineItemIds = new List<Id>();
    List<OpportunityLineItem> oppLineItems = new List<OpportunityLineItem>();
    Map<Id, OpportunityLineItem> oliIdandOLI = new Map<Id, OpportunityLineItem>();
    Map<Id, Date> oliAndProductEndDate = new Map<Id, Date>();
    Set<Id> updateOLIIds = new Set<Id>();
    List<OpportunityLineItem> olisToUpdate = new List<OpportunityLineItem>();


    /*Loop through variable objects passed from process builder and assign Ids to lists. Because of SOQL
    governors, don't want to query inside loop, so list assignment works around that*/
    for (productVariables var : Vars)
    {
      if (var.opptyLineItemId != null)
      {
        oppLineItemIds.add(var.opptyLineItemId);
        system.debug('oppLineItemIds = ' + oppLineItemIds);
      }
    }

    oppLineItems = [SELECT Id, ServiceDate, Product_End_Date__c, PricebookEntry.Product2.FY16_Revenue_Type__c
          FROM OpportunityLineItem 
          WHERE Id IN : oppLineItemIds
          AND PricebookEntry.Product2.FY16_Revenue_Type__c = 'Recurring'];

    system.debug('oppLineItems = ' + oppLineItems);

    /*Product Term Months may be null, because Product End Date is not required on Salesforce ui when adding a product. Assume 12 months from ServiceDate and calculate months between.
    Default Product Date based on these assumptions - updates will re-calculate accordingly.*/
    for (OpportunityLineItem oli : oppLineItems)
    {
      oliIdandOLI.put(oli.Id, oli);
      Date tempProductEndDate = oli.ServiceDate.addYears(1).addDays(-1);
      oliAndProductEndDate.put(oli.Id, tempProductEndDate);
    }

    /*Line Item field values to update go in maps in this block*/
    if (!oliIdandOLI.isEmpty())
    {
      for (Id oliID : oliIdandOLI.keySet())
      {
        if (!oliAndProductEndDate.isEmpty())
        {
          oliIdandOLI.get(oliId).Product_End_Date__c = oliAndProductEndDate.get(oliId);
        }
      }
    }

    /*Don't want duplicates in list of line items to update, so loop through ordered set of Ids and add to update list only if the record is not already there.*/
    for (Id oliId : oliIdandOLI.keySet())
    {
      if (!updateOLIIds.contains(oliId))
      {
        olisToUpdate.add(oliIdandOLI.get(oliId));
        system.debug('olisToUpdate =  ' + olisToUpdate);
      }
      else
      {
        updateOLIIds.add(oliId);
        system.debug('updateOLIIds = ' + updateOLIIds);
      }
    }



Thank you,

Maggie
Best Answer chosen by Maggie Farley 20
Srinivasa Chary TaduriSrinivasa Chary Taduri
Below line should be checked null condition.

Date tempProductEndDate = oli.ServiceDate.addYears(1).addDays(-1);

like below


Date tempProductEndDate;
if(oli.ServiceDate != null)
{
   tempProductEndDate = oli.ServiceDate.addYears(1).addDays(-1);
}


Please mark it as correct

All Answers

Arpit Gupta 37Arpit Gupta 37
Hi Maggie ,

Set updateOLIIds doesn't contain anything that's why it is giving nullpointer exception.

Regards:
Arpit Gupta
Srinivasa Chary TaduriSrinivasa Chary Taduri
Below line should be checked null condition.

Date tempProductEndDate = oli.ServiceDate.addYears(1).addDays(-1);

like below


Date tempProductEndDate;
if(oli.ServiceDate != null)
{
   tempProductEndDate = oli.ServiceDate.addYears(1).addDays(-1);
}


Please mark it as correct
This was selected as the best answer