You need to sign in to do that
Don't have an account?
AL
Apex Test Class NullPointerException
Hello, I'm trying to create a test class for inserting/updating quote products. When I try to run a test class I get the error: System.NullPointerException: Attempt to de-reference a null object. The stack trace is Class.QuoteRollupPFBU.updateQuote: line 17, column 1
Class.QLITest.RollUpQLI: line 63, column 1. I thought I initalized all of the variables, but feel free to let me know if you see any errors in my code. Thank you.
Class.QLITest.RollUpQLI: line 63, column 1. I thought I initalized all of the variables, but feel free to let me know if you see any errors in my code. Thank you.
Class public class QuoteRollupPFBU { public QuoteRollupPFBU(){ List<Quote> quotesToUpdate = new List<Quote>(); List<QuoteLineItem> quoteLineItemToUpdate; quoteLineItemToUpdate = new List<QuoteLineItem>(); } public static void updateQuote(List<SObject> quoteLineItemToUpdate){ List<Quote> quotesToUpdate = new List<Quote>(); Set<String> setQuoteProdsToRemove = new Set<String>(); List<String> listQuoteProdsToRemove = new List<String>(); Set<Id> ParentIds = new Set<Id>(); for(QuoteLineItem qProduct: (List<QuoteLineItem>)quoteLineItemToUpdate){ System.debug( 'value :' + quoteLineItemToUpdate); ParentIds.add(qProduct.QuoteId); } Map<Id, Quote> mapQuote = new Map<Id, Quote>([Select Id, ProductBusinessUnits__c, RollupofProductFamilies__c, (Select Id, QuoteId, ProductFamily__c, Product2Id, LineNumber, BusinessUnit__c from QuoteLineItems Where (LineNumber != null) ) From Quote Where Id In: ParentIds]); if(Trigger.isInsert || Trigger.isUpdate ){ for(Quote q : mapQuote.values()){ List<String> quoteLineItemProductFamList = new List<String>(); List<String> quoteLineItemBUList = new List<String>(); Set<String> qliProdFamSet = new Set<String>(); Set<String> qliBUSet = new Set<String>(); for(QuoteLineItem qli: q.quotelineitems){ if(qli.LineNumber != null ){ qliProdFamSet.add(qli.ProductFamily__c); qliBUSet.add(qli.BusinessUnit__c); } else if(qli.LineNumber == null ){ qliBUSet.remove(qli.BusinessUnit__c); qliProdFamSet.remove(qli.ProductFamily__c); } } quoteLineItemProductFamList.addAll(qliProdFamSet); quoteLineItemProductFamList.sort(); String productFamily = string.join(quoteLineItemProductFamList,', ' ); quoteLineItemBUList.addAll(qliBUSet); quoteLineItemBUList.sort(); String businessUnit = string.join(quoteLineItemBUList,', ' ); if(q.RollupofProductFamilies__c != productFamily && q.ProductBusinessUnits__c != businessUnit ){ q.RollupofProductFamilies__c = productFamily; q.ProductBusinessUnits__c = businessUnit; quotesToUpdate.add(q); } } //End of for loop above if(!quotesToUpdate.isEmpty()){ upsert quotesToUpdate; } } } } Test Class @isTest(SeeAllData = true) public class QLITest { @isTest static void RollUpQLI(){ List<QuoteLineItem> listQLI = new List<QuoteLineItem>(); List<Quote> quotesToUpdate = new List<Quote>(); Set<Id> ParentIds = new Set<Id>(); //Insert product Product2 p = new Product2(Name = 'Test Name', BusinessUnit__c = 'CDS', ProductFamily__c = 'Communication Director'); insert p; //Insert pricebook Pricebook2 standardPricebook = new Pricebook2(Id = Test.getStandardPricebookId(), IsActive = true); update standardPricebook; // Id pricebookId = Test.getStandardPricebookId(); PricebookEntry pbe1 = new PricebookEntry(Pricebook2id = standardPricebook.Id, Product2id=p.Id, isActive = true, unitPrice=100); insert pbe1; Opportunity o = new Opportunity(Pricebook2id = Test.getStandardPricebookId(), Name = 'Test Opp1', RollupofProductFamilies__c = 'Test', ProductBusinessUnits__c = 'Bed Stat', StageName ='Test Stage', CloseDate = date.today()); insert o; Quote q = new Quote(OpportunityId = o.Id, Name = 'Test Quote', Pricebook2Id = standardPricebook.Id); insert q; QuoteLineItem oli = new QuoteLineItem(PricebookEntryid = pbe1.Id, ProductFamily__c = 'Bed Stat', QuoteId = q.Id, Product2Id = p.Id, Quantity = 1, UnitPrice = 1); insert oli; listQLI.add(oli); upsert listQLI; ParentIds.add(oli.QuoteId); Map<Id, Quote> mapQuote = new Map<Id, Quote>([Select Id, BusinessUnit__c, ProductBusinessUnits__c, RollupofProductFamilies__c, (Select Id, QuoteId, Product2Id, LineNumber, ProductFamily__c, BusinessUnit__c from QuoteLineItems ) From Quote Where Id In: ParentIds]); for(Quote quote: mapQuote.values()){ List<String> qliProdFamList = new List<String>(); Set<String> qlitProdFamSet = new Set<String>(); Set<String> qlitBUset = new Set<String>(); List<String> qliBUList = new List<String>(); for(QuoteLineItem ql: quote.QuoteLineItems){ qlitProdFamSet.add(ql.ProductFamily__c); qlitBUset.add(ql.BusinessUnit__c); } qliProdFamList.addAll(qlitProdFamSet); qliProdFamList.sort(); String productFamily = string.join(qliProdFamList,', ' ); qliBUList.addAll(qlitBUset); qliBUList.sort(); String bizUnit = string.join(qliBUList,', ' ); if(quote.RollupofProductFamilies__c != productFamily && quote.BusinessUnit__c != bizUnit){ quote.RollupofProductFamilies__c = productFamily; quote.ProductBusinessUnits__c = bizUnit; quotesToUpdate.add(quote); } } upsert quotesToUpdate; test.startTest(); // if(quotesToUpdate.size()>0 && listQLI.size()>0){ QuoteRollupPFBU.updateQuote(listQLI); // } test.stopTest(); } } Trigger trigger PrimaryQliTrigger on QuoteLineItem (before insert, before update, after insert, after update, before delete, after delete) { if(Trigger.isBefore){ if(Trigger.isInsert){ } if(Trigger.isUpdate){ } if(Trigger.isDelete){ } } if(Trigger.isAfter){ if(Trigger.isInsert){ QuoteRollupPFBU.updateQuote(trigger.new); } if(Trigger.isUpdate || Trigger.isInsert ){ // QuoteRollupPFBU.updateQuote(trigger.new); } if(Trigger.isDelete){ } } }
Which is
Make it sure you write your SQOL in try catch which help you to get in that type of problem.If still problem come you can debug in catch;
Which is help you to solve it quicker.
please check this link for more help https://help.salesforce.com/articleView?id=000063739&type=1
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ankit
All Answers
Which is
Make it sure you write your SQOL in try catch which help you to get in that type of problem.If still problem come you can debug in catch;
Which is help you to solve it quicker.
please check this link for more help https://help.salesforce.com/articleView?id=000063739&type=1
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ankit