You need to sign in to do that
Don't have an account?
Maggie 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.
Thank you,
Maggie
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
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
Set updateOLIIds doesn't contain anything that's why it is giving nullpointer exception.
Regards:
Arpit Gupta
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