-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
1Questions
-
2Replies
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, cd_OpportunityLineItemTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object
I get the above error when deploying a trigger and its associated test class in production. I believe it has to do with either my creating a record that already exists via another trigger, or something that has been left null(?). Any and all help is much appreciated! Posting my trigger and test class below:
Trigger:
Trigger:
trigger closedTrigger on Opportunity (after update) { //create a list of related accounts and account Ids list<Id> acctIds = new list<Id>(); list<Account> accts = new list<Account>(); //get the accounts and oplineitems associated with the opportunity for (opportunity o:trigger.new) { acctIds.add(o.accountId); } //create a list of Opportunity Line Items list<Id> prodOpIds = new list<Id>(); Map<Id, Set<String>> mapProducts = new Map<Id, Set<String>>(); for (opportunity op:trigger.new) { prodOpIds.add(op.Id); mapProducts.put(op.id, new Set<String>()); } for (OpportunityLineItem z: [SELECT Id, Product2.Name, OpportunityId FROM OpportunityLineItem WHERE OpportunityId IN: prodOpIds]) { mapProducts.get(z.OpportunityId).add(z.Product2.Name); } //now we can loop over the associated account(s) and update the information with which we're concerned for (account a: [SELECT Id, Curriculum_Product__c, Past_Room_Scheduling_Product__c, Room_Scheduling_Product__c, Past_Curriculum_Product__c, Past_Event_Scheduling_Product__c, Event_Scheduler_Product__c, Class_Schedule_Planning_Product__c, Past_Class_Scheduling_Product__c, Past_Catalog_Product__c, Catalog_Product__c, Account_Lifecycle_Status__c FROM account WHERE Id IN: acctIds]) { for (opportunity op:trigger.new) { Set<String> setProducts = mapProducts.get(op.Id); if (op.stageName=='Closed Won' && trigger.oldmap.get(op.Id).StageName != 'Closed Won') { //set status to Customer a.Account_Lifecycle_Status__c = 'Customer'; //class if(setProducts.contains('Class Scheduler') && a.Class_Schedule_Planning_Product__c != 'Coursedog') { a.Past_Class_Scheduling_Product__c = a.Class_Schedule_Planning_Product__c; a.Class_Schedule_Planning_Product__c = 'Coursedog'; } //catalog if(setProducts.contains('Catalog Management') && a.Catalog_Product__c != 'Coursedog') { a.Past_Catalog_Product__c = a.Catalog_Product__c; a.Catalog_Product__c = 'Coursedog'; } //events if(setProducts.contains('Event Scheduler') && a.Event_Scheduler_Product__c != 'Coursedog') { a.Past_Event_Scheduling_Product__c = a.Event_Scheduler_Product__c; a.Event_Scheduler_Product__c = 'Coursedog'; } //curriculum if(setProducts.contains('Curriculum Management') && a.Curriculum_Product__c != 'Coursedog') { a.Past_Curriculum_Product__c = a.Curriculum_Product__c; a.Curriculum_Product__c = 'Coursedog'; } accts.add(a); } if (op.stageName=='Closed Lost' && trigger.oldmap.get(op.Id).StageName != 'Closed Lost') { a.Account_Lifecycle_Status__c = 'Recycled'; accts.add(a); } } } update accts; }Test class:
@istest public class testClassForTrigger{ @istest public static void triggerTest(){ account acc = new account(); acc.name='test'; //when you create an account you need a name acc.Class_Schedule_Planning_Product__c = 'Astra Scheduler'; acc.Curriculum_Product__c = 'Courseleaf CIM'; acc.Catalog_Product__c = 'Courseleaf CAT'; acc.Event_Scheduler_Product__c = 'EMS'; insert acc; //now we have an ID Pricebook2 stdpb = new Pricebook2(Id = Test.getStandardPricebookId()); update stdpb; Product2 p = new Product2(Name = 'Catalog Management', CanUseRevenueSchedule = true, IsActive = true); insert p; Product2 p2 = new Product2(Name = 'Curriculum Management', CanUseRevenueSchedule = true, IsActive = true); insert p2; Product2 p3 = new Product2(Name = 'Class Scheduler', CanUseRevenueSchedule = true, IsActive = true); insert p3; Product2 p4 = new Product2(Name = 'Event Scheduler', CanUseRevenueSchedule = true, IsActive = true); insert p4; PricebookEntry pbe = new PricebookEntry(Product2Id = p.Id, Pricebook2Id = stdpb.Id, IsActive = true, UnitPrice = 0); insert pbe; PricebookEntry pbe2 = new PricebookEntry(Product2Id = p2.Id, Pricebook2Id = stdpb.Id, IsActive = true, UnitPrice = 0); insert pbe2; PricebookEntry pbe3 = new PricebookEntry(Product2Id = p3.Id, Pricebook2Id = stdpb.Id, IsActive = true, UnitPrice = 0); insert pbe3; PricebookEntry pbe4 = new PricebookEntry(Product2Id = p4.Id, Pricebook2Id = stdpb.Id, IsActive = true, UnitPrice = 0); insert pbe4; opportunity opp = new opportunity(); opp.name='test opp'; opp.accountId = acc.Id; opp.closedate = date.today().adddays(5); opp.stagename='Interest'; //when you create an op you need a stage, acocunt, name, and close date test.startTest(); insert opp; OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = opp.Id, PricebookEntryId = pbe.Id, Quantity = 1, UnitPrice = 50); insert oli; OpportunityLineItem oli2 = new OpportunityLineItem(OpportunityId = opp.Id, PricebookEntryId = pbe2.Id, Quantity = 1, UnitPrice = 50); insert oli2; OpportunityLineItem oli3 = new OpportunityLineItem(OpportunityId = opp.Id, PricebookEntryId = pbe3.Id, Quantity = 1, UnitPrice = 50); insert oli3; OpportunityLineItem oli4 = new OpportunityLineItem(OpportunityId = opp.Id, PricebookEntryId = pbe4.Id, Quantity = 1, UnitPrice = 50); insert oli4; opp.closedate = date.today(); opp.stageName = 'Closed Won'; update opp; account actTest = [SELECT Id, Account_Lifecycle_Status__c, Class_Schedule_Planning_Product__c, Past_Class_Scheduling_Product__c, Curriculum_Product__c, Past_Curriculum_Product__c, Catalog_Product__c, Past_Catalog_Product__c, Event_Scheduler_Product__c, Past_Event_Scheduling_Product__c FROM Account WHERE ID = :acc.Id LIMIT 1]; update actTest; system.assertequals('Customer', actTest.Account_Lifecycle_Status__c,'Opportunity update did not set account status properly'); system.assertequals('Coursedog', actTest.Curriculum_Product__c, 'Opportunity did not set curriculum product properly'); system.assertequals('Courseleaf CIM', actTest.Past_Curriculum_Product__c, 'Opportunity did not set curriculum product properly'); system.assertequals('Coursedog', actTest.Class_Schedule_Planning_Product__c, 'Opportunity did not set scheduling product properly'); system.assertequals('Astra Scheduler', actTest.Past_Class_Scheduling_Product__c, 'Opportunity did not set scheduling product properly'); system.assertequals('Coursedog', actTest.Event_Scheduler_Product__c, 'Opportunity did not set event product properly'); system.assertequals('EMS', actTest.Past_Event_Scheduling_Product__c, 'Opportunity did not set event product properly'); system.assertequals('Coursedog', actTest.Catalog_Product__c, 'Opportunity did not set catlog product properly'); system.assertequals('Courseleaf CAT', actTest.Past_Catalog_Product__c, 'Opportunity did not set catalog product properly'); test.stopTest(); } }
- Jordan Kindler
- October 24, 2020
- Like
- 0
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, cd_OpportunityLineItemTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object
I get the above error when deploying a trigger and its associated test class in production. I believe it has to do with either my creating a record that already exists via another trigger, or something that has been left null(?). Any and all help is much appreciated! Posting my trigger and test class below:
Trigger:
Trigger:
trigger closedTrigger on Opportunity (after update) { //create a list of related accounts and account Ids list<Id> acctIds = new list<Id>(); list<Account> accts = new list<Account>(); //get the accounts and oplineitems associated with the opportunity for (opportunity o:trigger.new) { acctIds.add(o.accountId); } //create a list of Opportunity Line Items list<Id> prodOpIds = new list<Id>(); Map<Id, Set<String>> mapProducts = new Map<Id, Set<String>>(); for (opportunity op:trigger.new) { prodOpIds.add(op.Id); mapProducts.put(op.id, new Set<String>()); } for (OpportunityLineItem z: [SELECT Id, Product2.Name, OpportunityId FROM OpportunityLineItem WHERE OpportunityId IN: prodOpIds]) { mapProducts.get(z.OpportunityId).add(z.Product2.Name); } //now we can loop over the associated account(s) and update the information with which we're concerned for (account a: [SELECT Id, Curriculum_Product__c, Past_Room_Scheduling_Product__c, Room_Scheduling_Product__c, Past_Curriculum_Product__c, Past_Event_Scheduling_Product__c, Event_Scheduler_Product__c, Class_Schedule_Planning_Product__c, Past_Class_Scheduling_Product__c, Past_Catalog_Product__c, Catalog_Product__c, Account_Lifecycle_Status__c FROM account WHERE Id IN: acctIds]) { for (opportunity op:trigger.new) { Set<String> setProducts = mapProducts.get(op.Id); if (op.stageName=='Closed Won' && trigger.oldmap.get(op.Id).StageName != 'Closed Won') { //set status to Customer a.Account_Lifecycle_Status__c = 'Customer'; //class if(setProducts.contains('Class Scheduler') && a.Class_Schedule_Planning_Product__c != 'Coursedog') { a.Past_Class_Scheduling_Product__c = a.Class_Schedule_Planning_Product__c; a.Class_Schedule_Planning_Product__c = 'Coursedog'; } //catalog if(setProducts.contains('Catalog Management') && a.Catalog_Product__c != 'Coursedog') { a.Past_Catalog_Product__c = a.Catalog_Product__c; a.Catalog_Product__c = 'Coursedog'; } //events if(setProducts.contains('Event Scheduler') && a.Event_Scheduler_Product__c != 'Coursedog') { a.Past_Event_Scheduling_Product__c = a.Event_Scheduler_Product__c; a.Event_Scheduler_Product__c = 'Coursedog'; } //curriculum if(setProducts.contains('Curriculum Management') && a.Curriculum_Product__c != 'Coursedog') { a.Past_Curriculum_Product__c = a.Curriculum_Product__c; a.Curriculum_Product__c = 'Coursedog'; } accts.add(a); } if (op.stageName=='Closed Lost' && trigger.oldmap.get(op.Id).StageName != 'Closed Lost') { a.Account_Lifecycle_Status__c = 'Recycled'; accts.add(a); } } } update accts; }Test class:
@istest public class testClassForTrigger{ @istest public static void triggerTest(){ account acc = new account(); acc.name='test'; //when you create an account you need a name acc.Class_Schedule_Planning_Product__c = 'Astra Scheduler'; acc.Curriculum_Product__c = 'Courseleaf CIM'; acc.Catalog_Product__c = 'Courseleaf CAT'; acc.Event_Scheduler_Product__c = 'EMS'; insert acc; //now we have an ID Pricebook2 stdpb = new Pricebook2(Id = Test.getStandardPricebookId()); update stdpb; Product2 p = new Product2(Name = 'Catalog Management', CanUseRevenueSchedule = true, IsActive = true); insert p; Product2 p2 = new Product2(Name = 'Curriculum Management', CanUseRevenueSchedule = true, IsActive = true); insert p2; Product2 p3 = new Product2(Name = 'Class Scheduler', CanUseRevenueSchedule = true, IsActive = true); insert p3; Product2 p4 = new Product2(Name = 'Event Scheduler', CanUseRevenueSchedule = true, IsActive = true); insert p4; PricebookEntry pbe = new PricebookEntry(Product2Id = p.Id, Pricebook2Id = stdpb.Id, IsActive = true, UnitPrice = 0); insert pbe; PricebookEntry pbe2 = new PricebookEntry(Product2Id = p2.Id, Pricebook2Id = stdpb.Id, IsActive = true, UnitPrice = 0); insert pbe2; PricebookEntry pbe3 = new PricebookEntry(Product2Id = p3.Id, Pricebook2Id = stdpb.Id, IsActive = true, UnitPrice = 0); insert pbe3; PricebookEntry pbe4 = new PricebookEntry(Product2Id = p4.Id, Pricebook2Id = stdpb.Id, IsActive = true, UnitPrice = 0); insert pbe4; opportunity opp = new opportunity(); opp.name='test opp'; opp.accountId = acc.Id; opp.closedate = date.today().adddays(5); opp.stagename='Interest'; //when you create an op you need a stage, acocunt, name, and close date test.startTest(); insert opp; OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = opp.Id, PricebookEntryId = pbe.Id, Quantity = 1, UnitPrice = 50); insert oli; OpportunityLineItem oli2 = new OpportunityLineItem(OpportunityId = opp.Id, PricebookEntryId = pbe2.Id, Quantity = 1, UnitPrice = 50); insert oli2; OpportunityLineItem oli3 = new OpportunityLineItem(OpportunityId = opp.Id, PricebookEntryId = pbe3.Id, Quantity = 1, UnitPrice = 50); insert oli3; OpportunityLineItem oli4 = new OpportunityLineItem(OpportunityId = opp.Id, PricebookEntryId = pbe4.Id, Quantity = 1, UnitPrice = 50); insert oli4; opp.closedate = date.today(); opp.stageName = 'Closed Won'; update opp; account actTest = [SELECT Id, Account_Lifecycle_Status__c, Class_Schedule_Planning_Product__c, Past_Class_Scheduling_Product__c, Curriculum_Product__c, Past_Curriculum_Product__c, Catalog_Product__c, Past_Catalog_Product__c, Event_Scheduler_Product__c, Past_Event_Scheduling_Product__c FROM Account WHERE ID = :acc.Id LIMIT 1]; update actTest; system.assertequals('Customer', actTest.Account_Lifecycle_Status__c,'Opportunity update did not set account status properly'); system.assertequals('Coursedog', actTest.Curriculum_Product__c, 'Opportunity did not set curriculum product properly'); system.assertequals('Courseleaf CIM', actTest.Past_Curriculum_Product__c, 'Opportunity did not set curriculum product properly'); system.assertequals('Coursedog', actTest.Class_Schedule_Planning_Product__c, 'Opportunity did not set scheduling product properly'); system.assertequals('Astra Scheduler', actTest.Past_Class_Scheduling_Product__c, 'Opportunity did not set scheduling product properly'); system.assertequals('Coursedog', actTest.Event_Scheduler_Product__c, 'Opportunity did not set event product properly'); system.assertequals('EMS', actTest.Past_Event_Scheduling_Product__c, 'Opportunity did not set event product properly'); system.assertequals('Coursedog', actTest.Catalog_Product__c, 'Opportunity did not set catlog product properly'); system.assertequals('Courseleaf CAT', actTest.Past_Catalog_Product__c, 'Opportunity did not set catalog product properly'); test.stopTest(); } }
- Jordan Kindler
- October 24, 2020
- Like
- 0