You need to sign in to do that
Don't have an account?
Sam Ward
test class attempt
Hi,
So I have the below code and I know it works okay
I have also added in 4 schedules which trigger this:
I've then tried to write a test class by following a video and changing the fields that I need instead of what the video was using and it doesn't work can someone help please?
Test Class:
Thanks in advance
So I have the below code and I know it works okay
global class MarketingClicks implements Schedulable { global void execute(SchedulableContext ctx) { List<Lead> Leadlist = [SELECT Id, Campaign_Name__c FROM Lead WHERE Campaign_Name__c != '']; if(!LeadList.isEmpty()){ for(Lead L : LeadList) { //Create Marketing Click L.Link_Type__c = 'TEST'; Pardot_Campaign__c PC = new Pardot_Campaign__c(); PC.Lead__c = L.Id; PC.Name = L.Campaign_Name__c; insert PC; L.Campaign_Name__c= ''; Update L; } } System.debug(Leadlist); } }
I have also added in 4 schedules which trigger this:
System.schedule('Marketing Clicks Job 1', '0 0 * * * ?', new MarketingClicks ()); System.schedule('Marketing Clicks Job 2', '0 15 * * * ?', new MarketingClicks ()); System.schedule('Marketing Clicks Job 3', '0 30 * * * ?', new MarketingClicks ()); System.schedule('Marketing Clicks Job 4', '0 45 * * * ?', new MarketingClicks ());Again this works fine from what i'm seeing.
I've then tried to write a test class by following a video and changing the fields that I need instead of what the video was using and it doesn't work can someone help please?
Test Class:
@isTest Public Class MarketingClicksTest { @testSetup static void setup() { List<Lead> Leadlist = new List<Lead>(); for(Integer i=1;i<=10;i++) { Lead Id = new Lead(Company='Test',Lastname='sam',Status='Open',UK_Canada__c='UK',Phone='000000',Online_Offline__c='Offline',Enquiry_Source__c='Cold Call',Method__c='N/A',Position__c='UNASSIGNED',Industry_Sector__c='Miscellaneous',Campaign_Name__c='Test'); } insert LeadList; } static testmethod void testMarketingClicksScheduleJob() { string sch='0 5 12 10 2 ?'; Test.startTest(); string jobId=system.schedule('scheduleApexTest',sch,new MarketingClicks()); List<Lead> LeadList=[SELECT Id, Campaign_Name__c FROM Lead WHERE Campaign_Name__c != '']; system.assertEquals(10, Leadlist.size()); Test.stopTest(); } }
Thanks in advance
A side note of execute method, Salesforce suggests not to use DML in loop. You may need to enhance execute method.