You need to sign in to do that
Don't have an account?
Maciej Gunia 8
Trigger works - test fails
Hello,
I have a trigger on the campaigns level. In this post I explained the details of the solution needed - https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AbhHIAS but in few words I have the field where its value should match the custom setting and if yes, then it should bring the target value from the custom value. The trigger looks like that:
The trigger at the beining was generating the error but I amended the system this way, that it works fine and provides me with the relevant information. I've made the test class for this trigger and it fails returning me the error. Here is the test class and the error message:
TEST CLASS:
ERROR MESSAGE:
I tried to re-produce this test by myself and there is no error in Salesforce. Could anyone of you help me with that?
Regards,
Maciej
I have a trigger on the campaigns level. In this post I explained the details of the solution needed - https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AbhHIAS but in few words I have the field where its value should match the custom setting and if yes, then it should bring the target value from the custom value. The trigger looks like that:
trigger PopulateTarget on Campaign (before insert, before update) { for(Campaign campaign : Trigger.new) { if (String.isNotBlank(campaign.Apex_Calculator__c) == true) { String target = DSTargets__c.getInstance(campaign.Apex_Calculator__c).Target__c; campaign.DS_Target_Multiplier__c = Target; } } }
The trigger at the beining was generating the error but I amended the system this way, that it works fine and provides me with the relevant information. I've made the test class for this trigger and it fails returning me the error. Here is the test class and the error message:
TEST CLASS:
@isTest private class testPopulateTarget{ static testMethod void testMethod1(){ // Load the Custom Settings DSTargets__c testSetting = new DSTargets__c(Name='Africa - 10 Weeks; CW 10',Target__c='0.1538'); insert testSetting; // Create Campaign. Since it would execute trigger, put it in start and stoptests Test.startTest(); Campaign testCamp = new Campaign(); // populate all reqd. fields. testCamp.Name = 'test DS campaign'; testCamp.RecordTypeId = '012200000001b3v'; testCamp.Started_Campaign_weeks_before_Event__c = '12 Weeks'; testCamp.ParentId= '701g0000000EZRk'; insert testCamp; Test.stopTest(); testCamp = [Select ID,Apex_Calculator__c,DS_Target_Multiplier__c from Campaign where Id = :testCamp.Id]; system.assertEquals(testCamp.DS_Target_Multiplier__c,testSetting.Target__c);// assert that target is populated right } }
ERROR MESSAGE:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, PopulateTarget: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.PopulateTarget: line 7, column 1: []
I tried to re-produce this test by myself and there is no error in Salesforce. Could anyone of you help me with that?
Regards,
Maciej
Thank you for your response. However, I got the error message in the trigger you posted. It says:
How should I amend this?
What should I change?