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
Daniel B ProbertDaniel B Probert 

Test Class for Trigger Help

Hi Guys,

 

I've written a very simple trigger for Campaigns that works well however, I want to ensure I have a test class for it and haven't written a test class before.

 

This is my trigger:

 

rigger createjournalcodes on Campaign (after insert) {

List<Journal_Codes__c> journalcodes = new List<Journal_Codes__c>();
for (Campaign camp : trigger.new) {
    
        Journal_Codes__c jc = new Journal_Codes__c();       
        jc.Campaign__c = camp.Id;
        jc.CurrencyIsoCode = 'GBP';
        jc.L1_Country_Region__c = 'UK';
        jc.L1_Country_Region_Display__c = 'United Kingdom';
        jc.L2_Activity_Codes__c = 'U';
        jc.L2_Activity_Codes_Display__c = 'Unallocated';
        jc.L3_Donor_Codes__c = 'CAMFE';
        jc.L4__c = 'UREST';
        jc.L5_Database_Code__c = 'CAM';
        journalcodes.add(jc);
    }
for (Campaign camp : trigger.new) {
    
        Journal_Codes__c jc = new Journal_Codes__c();       
        jc.Campaign__c = camp.Id;
        jc.CurrencyIsoCode = 'USD';
        jc.L1_Country_Region__c = 'US';
        jc.L1_Country_Region_Display__c = 'United States';
        jc.L2_Activity_Codes__c = 'U';
        jc.L2_Activity_Codes_Display__c = 'Unallocated';
        jc.L3_Donor_Codes__c = 'CAMFE';
        jc.L4__c = 'UREST';
        jc.L5_Database_Code__c = 'USA';
        journalcodes.add(jc);
    }
insert journalcodes;
}

 so i'm guessing I need a test class that creates a campaign but then I'm stuck on what I should be doing from there.

 

I have some required fields on the campaign so I'm guessing it needs to have them in there but then do I also need to create these related items or what?

 

confused myself a bit :)

 

cheers

dan

Best Answer chosen by Admin (Salesforce Developers) 
Devender MDevender M
Hi,

@IsTest
public class CampaignTestCLass
{
public static testmethod void CampaignTestMethod()
{
Campaign campaign = new Campaign();// mention the required fields and do insert
insert campaign;
}
}

All Answers

Devender MDevender M
Hi,

@IsTest
public class CampaignTestCLass
{
public static testmethod void CampaignTestMethod()
{
Campaign campaign = new Campaign();// mention the required fields and do insert
insert campaign;
}
}
This was selected as the best answer
Daniel B ProbertDaniel B Probert

so simple i was really over thinking it good man cheer..