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 

Test class - constructor not defined

I have a trigger which works in the sandbox. The workflow checks the field in the campaign level and compares it with the custom setting. If it matches, then it returns the target to the DS Multiplier field. The trigger looks as follows

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);
        {
            String target = targetInstance .Target__c;
             campaign.DS_Target_Multiplier__c = Target;
        }
    }
}
}

However, I had problems to write a proper test to this and asked for the help on the internet. I received the test

@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 = apexCalculatorUserId);
      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

}

}

Such test returns the error "Compile Error: Variable does not exist: apexCalculatorUserId at line 6 column 122". If I remove that ApexCalculator part System.assertEquals then the test passes. However it covers 4/6 part of the code (which is 66%). 

As I was able to find out that apexCalculatorUserId has not been defined. I was suggested to look at the constructor DSTargets__c and see what kind of ID it is expecting there. However, my knowledge doesn't allow me to amend it this way it will work. Would any of you help me to amend it as it seems I am on the last corner before the finish line and am hopeless with this test?

Regards,
Maciej
nitesh gadkarinitesh gadkari
//Is it correctly written??
SetupOwnerId = apexCalculatorUserId

///should be written like

SetupOwnerId = :apexCalculatorUserId
Regards
Nitesh
AshwaniAshwani
You have not defined "apexCalculatorUserId" in your test class. What ype is of it? Instead you can also use "UserInfo.getUserId()"
Bhawani SharmaBhawani Sharma
You are using apexCalculatorUserId in your code but you never defined that. make sure to update your code. You need to define apexCalculatorUserId variable before using it. Also don't forget to put colon(:) sign before the variable when you use it.
Maciej GuniaMaciej Gunia
Hello,

When I change SetupOwnerId = apexCalculatorUserId to SetupOwnerId = UserInfo.getUserId() I get the following error message

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: SetupOwnerId (id value of incorrect type): [SetupOwnerId]

I also tried to change SetupOwnerId = apexCalculatorUserId to SetupOwnerId = :apexCalculatorUserId but then I can't save this test class as it says

Error: Compile Error: unexpected token: ':' at line 6 column 117

I am not fluent with apex coding. Therefore, I would like to ask if some of you could help me to define the variable appropriately?

Regards,
Maciej
Bhawani SharmaBhawani Sharma
Can you try 
SetupOwnerId = UserInfo.getProfileId()
Maciej Gunia 8Maciej Gunia 8
Hi Bhawani,

I've already tried that and as I wrote above that returns the following error message while running the test:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: SetupOwnerId (id value of incorrect type): [SetupOwnerId]


Bhawani SharmaBhawani Sharma
Does it mean you have been tried UserInfo.getUserId() and UserInfo.getProfileId()?
If yes, that try this one 
SetupOwnerId = [Select Id from Profile where In != UserInfo.getProfileId() Limit 1].Id
Maciej Gunia 8Maciej Gunia 8
Which part of the code I shall place it?
Bhawani SharmaBhawani Sharma
​DSTargets__c testSetting = new DSTargets__c(Name='Africa - 10 Weeks; CW 10',Target__c='0.1538', SetupOwnerId = UserInfo.getUserId());
or
​DSTargets__c testSetting = new DSTargets__c(Name='Africa - 10 Weeks; CW 10',Target__c='0.1538', SetupOwnerId = UserInfo.getProfileId());
or
​DSTargets__c testSetting = new DSTargets__c(Name='Africa - 10 Weeks; CW 10',Target__c='0.1538', SetupOwnerId = [Select Id from Profile where Id != UserInfo.getProfileId() limit 1].Id);

Try these one by one.
Maciej Gunia 8Maciej Gunia 8
First two bring the error: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: SetupOwnerId (id value of incorrect type): [SetupOwnerId]

Third one doesn't let me to save the class returning the error: Error: Compile Error: expecting a colon, found 'UserInfo.getProfileId' at line 6 column 153. If I add the colon before UserInfo.getProfileID(), then it fails with the same error message as the two ones before
Bhawani SharmaBhawani Sharma
What is the type of your custom setting? List or hierarchy? Regards, Bhavi Sharma Certified Salesforce Consultant bhavi@simplyforce.com/bhawani.sh.sharma@gmail.com +91-9928130589 LinkedIn | Twitter | Blog | Community www.simplyforce.com
Maciej Gunia 8Maciej Gunia 8
It is a List type
Bhawani SharmaBhawani Sharma
​You don't need to set SetupOwnerId then. ​​ Regards, Bhavi Sharma Certified Salesforce Consultant bhavi@simplyforce.com/bhawani.sh.sharma@gmail.com +91-9928130589 LinkedIn | Twitter | Blog | Community www.simplyforce.com
Maciej Gunia 8Maciej Gunia 8
So how my test class should look like then? You see, this test class is created by somebody else as my apex knowledge is limited. if I removed Line 6 and 23 then the test was successful. However, it covered 66% of my trigger code. I will have to amend it this way to get 75% and I have no idea what could I do. Could you help me?
Bhawani SharmaBhawani Sharma
You need to populate Apex_Calculator__c value in your test class. Regards, Bhavi Sharma Certified Salesforce Consultant bhavi@simplyforce.com/bhawani.sh.sharma@gmail.com +91-9928130589 LinkedIn | Twitter | Blog | Community www.simplyforce.com
Maciej Gunia 8Maciej Gunia 8
How could I do this?
Shaik Baji Ahmed 1Shaik Baji Ahmed 1
Need not to set SetupOwnerId manually. Here is the code which can workfor you
DSTargets__c testSetting = new DSTargets__c.getOrgDefaults();
testSetting.Name='Africa - 10 Weeks; CW 10';
testSetting.Target__c='0.1538';
insert testSetting;