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
Maciej Gunia 8Maciej 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:

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
AshwaniAshwani
You need to provide setup owner id equal to "campaign.Apex_Calculator__c" if you are getting instance by passing userId. Aslo in you main class check for null.


trigger PopulateTarget on Campaign (before insert, before update) 
{
    for(Campaign campaign : Trigger.new)
    {
        if (String.isNotBlank(campaign.Apex_Calculator__c) == true)
        {
            DSTargets__c targetInstance = DSTargets__c.getInstance(campaign.Apex_Calculator__c);
            if(targetInstance != null && targetInstance.id != null) {
                String target = targetInstance .Target__c;
                 campaign.DS_Target_Multiplier__c = Target;
            }
        }
    }
}




@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', SetupOwnerId = apexCalculaterUserId);
          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

}

}



Maciej Gunia 8Maciej Gunia 8
Hi Avilion,

Thank you for your response. However, I got the error message in the trigger you posted. It says:
Error: Compile Error: Variable does not exist: apexCalculaterUserId at line 6 column 122

How should I amend this?

Maciej Gunia 8Maciej Gunia 8
The error is in the test class...
Maciej Gunia 8Maciej Gunia 8
If I remove SetupOwnerId = apexCalculaterUserId, then I am able to save it but while testing it fails and gives the error

System.AssertException: Assertion Failed: Expected: null, Actual: 0.1538 Class.testPopulateTarget.testMethod1: line 23, column

What should I change?