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
merrick devillemerrick deville 

Trail head using future Mehods

Hello everyone I could use some help on the using future Methods trailhead. Please keep in mind I am not an Apex coder at all but I am trying to learn as much as I can.

Here is the Task

Create an Apex class with a method using the @future annotation that accepts a List of Account IDs and updates a custom field on the Account object with the number of contacts associated to the Account. Write unit tests that achieve 100% code coverage for the class.
Create a field on the Account object called 'Number_of_Contacts__c' of type Number. This field will hold the total number of Contacts for the Account.
Create an Apex class called 'AccountProcessor' that contains a 'countContacts' method that accepts a List of Account IDs. This method must use the @future annotation.
For each Account ID passed to the method, count the number of Contact records associated to it and update the 'Number_of_Contacts__c' field with this value.
Create an Apex test class called 'AccountProcessorTest'.
The unit tests must cover all lines of code included in the AccountProcessor class, resulting in 100% code coverage.
Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.

I have written an Apex class and a Test class but I think I am doing them both Wrong: because when I try to check the challenge i Get this error

Challenge Not yet complete... here's what's wrong: 
The 'AccountProcessorTest' test class doesn't appear to be calling the 'AccountProcessor.countContacts' method between Test.startTest() and Test.stopTest().


When I run the Test class I get this error :

System.QueryException: List has no rows for assignment to SObject

Here is the CLASS:
 
public class AccountProcessor {
     @future

  public static void someFutureMethod(List<id> scope) {

   Account[] updates = new Account[] {};
        for (AggregateResult ar : [
                select AccountId a, count(Id) c
                from Contact
                where AccountId in :scope
                group by AccountId
                ]) {
            updates.add(new Account(
                    Id = (Id) ar.get('a'),
                    Number_of_Contacts__c = (Decimal) ar.get('c')
                    ));
        }
        update updates;
    }

}

Here is the Test Class:
 
@IsTest
public class AccountProcessorTest {
  public static testmethod void TestAccountProcessorTest() {
 
Test.startTest();
     Account a = new Account();
        a.Name = 'Test Account';
        Insert a;
      
      Contact cont = New Contact();
      
      cont.FirstName ='Bob';
      cont.LastName ='Masters';
      cont.AccountId = a.Id;
      Insert cont;

    Test.stopTest() ;
     Contact ACC = [select AccountId from Contact where id = :a.id LIMIT 1];

 
        System.assert(Cont.AccountId != null);
        System.assertequals(cont.id, ACC.AccountId);

  }}

I have used alot or diffrent google searches to get me this far. But I have not Idea if Im even remotly close to the right answer or not.


 
Best Answer chosen by merrick deville
Amit Chaudhary 8Amit Chaudhary 8
As per task you method should be "countContacts"

Please update your Apex class like below :-
public class AccountProcessor 
{
  @future
  public static void countContacts(Set<id> setId) 
  {
      List<Account> lstAccount = [select id,Number_of_Contacts__c , (select id from contacts ) from account where id in :setId ];
      for( Account acc : lstAccount )
      {
          List<Contact> lstCont = acc.contacts ;
          
          acc.Number_of_Contacts__c = lstCont.size();
      }
      update lstAccount;
  }
}

Test class:-
@IsTest
public class AccountProcessorTest {
    public static testmethod void TestAccountProcessorTest() 
    {
        Account a = new Account();
        a.Name = 'Test Account';
        Insert a;

        Contact cont = New Contact();
        cont.FirstName ='Bob';
        cont.LastName ='Masters';
        cont.AccountId = a.Id;
        Insert cont;
        
        set<Id> setAccId = new Set<ID>();
        setAccId.add(a.id);
 
        Test.startTest();
            AccountProcessor.countContacts(setAccId);
        Test.stopTest();
        
        Account ACC = [select Number_of_Contacts__c from Account where id = :a.id LIMIT 1];
        System.assertEquals ( Integer.valueOf(ACC.Number_of_Contacts__c) ,1);
  }
  
}

Please let us know if this will help you

Thanks
Amit Chaudhary
 

All Answers

Deepak GulianDeepak Gulian
There is no method named "countContacts" in your apex class.
Amit Chaudhary 8Amit Chaudhary 8
As per task you method should be "countContacts"

Please update your Apex class like below :-
public class AccountProcessor 
{
  @future
  public static void countContacts(Set<id> setId) 
  {
      List<Account> lstAccount = [select id,Number_of_Contacts__c , (select id from contacts ) from account where id in :setId ];
      for( Account acc : lstAccount )
      {
          List<Contact> lstCont = acc.contacts ;
          
          acc.Number_of_Contacts__c = lstCont.size();
      }
      update lstAccount;
  }
}

Test class:-
@IsTest
public class AccountProcessorTest {
    public static testmethod void TestAccountProcessorTest() 
    {
        Account a = new Account();
        a.Name = 'Test Account';
        Insert a;

        Contact cont = New Contact();
        cont.FirstName ='Bob';
        cont.LastName ='Masters';
        cont.AccountId = a.Id;
        Insert cont;
        
        set<Id> setAccId = new Set<ID>();
        setAccId.add(a.id);
 
        Test.startTest();
            AccountProcessor.countContacts(setAccId);
        Test.stopTest();
        
        Account ACC = [select Number_of_Contacts__c from Account where id = :a.id LIMIT 1];
        System.assertEquals ( Integer.valueOf(ACC.Number_of_Contacts__c) ,1);
  }
  
}

Please let us know if this will help you

Thanks
Amit Chaudhary
 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
If your issue is resolved then please close this thread after marking best answer do that is if some has issue that post can help other
Bhanu ErraguntlaBhanu Erraguntla
Thanks Amit.  This helped.  I have used a list instead of set and that worked!

-Bhanu
merrick devillemerrick deville
Amit,

Thanks for the help!
Swathi MiryalaSwathi Miryala
Thank you Amit for the help
Naveen ChoudharyNaveen Choudhary
Thanks Amit!!!
 
Fnu SumitFnu Sumit
Amit i use your code and befor check to challange i use run all. but it wont cover 100% code.
SHISHIR BANSALSHISHIR BANSAL
Methods defined as TestMethod do not support Web service callouts  


this error is coming while using your code 
Harsh GosarHarsh Gosar
If you get this error : Methods defined as TestMethod do not support Web service callouts
Try testing  AccountProcessor -> class and test class in new org with only @future or @future(callout = false) annotation 
Amidou CisseAmidou Cisse
Thank You Amit !
Carlos FrancoCarlos Franco

Hello Amit... I would like to know how I can set a specific ID in AccountProcessorTest... I tried

@IsTest
public class AccountProcessorTest {
    public static testmethod void TestAccountProcessorTest() {
        Test.startTest();
            AccountProcessor.countContacts('0015000001I2JpXAAV');
        Test.stopTest();
  }
}

And got this error --> Method does not exist or incorrect signature: AccountProcessor.countContacts(String)

 

Thanks in advance

Sindhura ManthapuriSindhura Manthapuri
@carlos Franco: This is because "AccountProcessor" apex class is not saved. Please save "AccountProcessor" class and this will remove the error.
Neeraj Kumar 108Neeraj Kumar 108
I am getting Error: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Number_Of_Contacts__c]: [Number_Of_Contacts__c]
Cezary ZawadzkiCezary Zawadzki
@Neeraj Kumar 108 when creating field Number_Of_Contacts_cc you probably marked it as required and when creating Account for test you don't provide value for this field.
ChubbyChubby
I am getting Variable does not exist:Contacts error. Can someone please help?
Ben RowleyBen Rowley
Hi, 
Please try this:

Create this class:
public class AccountProcessor {
   
    @Future
    public static void countContacts(List<Id> accountIds){
        Map<Id,List<Contact>> accContacts = new Map<Id,List<Contact>>();
        List<Account> accsForUpdate = new List<Account>();
        if(accountIds != null){
            For(Account acc : [SELECT id,(SELECT id,Name FROM Contacts)FROM Account where id in: accountIds]){
                  accContacts.put(acc.Id,acc.contacts);
             }
            
            for(Id key : accContacts.keySet()){
                Account a = New Account(id = key);
                a.Number_of_Contacts__c = accContacts.get(key).size();
                accsForUpdate.add(a);
            }
        update accsForUpdate;
        }
    }
}
Now create this Test class. 
 
@isTest
public class AccountProcessorTest {
 
    @testSetup 
    static void setupAccount(){

    List<Account> accounts = RandomAccountContactFactory.generateRandomAccounts(1);
    insert accounts;
    
    
    List<Contact> contacts = RandomAccountContactFactory.generateRandomContacts(3, 'TestAP' , accounts.get(0).id);
    insert contacts;
  	
    }
    
	 @isTest
     static void testAccountProcessor(){
     
     List<id> accIds = new List<id>();
		for(Account a : [select id from Account]){
            accIds.add(a.id);
        }
         Test.startTest();
         AccountProcessor.countContacts(accIds);
         Test.stopTest();
     
     }
}

And this class to be called from the test class to create test Account with 3 Contacts (to be counted). 
public class RandomAccountContactFactory {
    
    public static List<Contact> generateRandomContacts (Integer numContacts, String lastName,Id accId){
        List<Contact> contacts = new List<Contact>();
        
        for(integer i = 0; i<numContacts; i++){
            Contact c = new Contact();
            c.FirstName = 'Trail' + i;
            c.LastName = lastName + i;
            c.AccountId = accId;	
			contacts.add(c);
        }
        return contacts;
    }
    

    public static List<Account> generateRandomAccounts (Integer numAccounts){
        List<Account> accounts = new List<Account>();

        for(integer i = 0; i<numAccounts; i++){
            Account a = new Account();
            a.Name = 'Test' + i;
            accounts.add(a);
        }
        return accounts;
    }
    
}


    
    
KapavariVenkatramanaKapavariVenkatramana
@ Thank you Amit for the help
Jagdeep MankotiaJagdeep Mankotia
Must create RandomAccountContactFactory class. Thanks Ben your code is working fine for me.
Ali Ali 28Ali Ali 28
Must create RandomAccountContactFactory class. Download Ludo star apk from here (https://theludoapks.com/)
Christian Francisco 04Christian Francisco 04
@Amit Chaudhary and Devs

It has been a while where this has been closed with a solution as best answer below. 

Question is, how do you call this anonymous using Open Execute Anonymous Window (Ctrl + E) in the developer console?

For example I want to execute this with Id (0016g000008zrVOAAY).
When I run anonymous: AccountProcessor.countContacts(0016g000008zrVOAAY);
I am getting error of: Unexpected Token '('.

Class used from Best Answer:
public class AccountProcessor { @future public static void countContacts(Set<id> setId) { List<Account> lstAccount = [select id,Number_of_Contacts__c , (select id from contacts ) from account where id in :setId ]; for( Account acc : lstAccount ) { List<Contact> lstCont = acc.contacts ; acc.Number_of_Contacts__c = lstCont.size(); } update lstAccount; } }

Thank you in advance.
Christian Francisco 04Christian Francisco 04
Update: Incase somebody want to run their own class using record id, I found a solution here:
https://developer.salesforce.com/forums/?id=9062I000000IE9QQAW

Basically assigning the id to a variable using Set<Id> then calling the class method using the idSet variable. This works for me:

Set<Id> idSet = new Set<Id>{'0016g000008zrVOAAY'};
AccountProcessor.countContacts(idSet);
Siddhi SalvekarSiddhi Salvekar
AccountProcessor Class
 
public class AccountProcessor {
    @future
    public static void countContacts(List<Id> listAccountIds){
        Map<Id,List<Contact>> accToContactsMap = new Map<Id, List<Contact>>();
        List<Account> updateAccounts = new List<Account>();
        if(listAccountIds != null){
            for(Account acc : [SELECT Id,
                               						(Select Id from Contacts)
                               			 FROM Account
                               			 WHERE id in: listAccountIds]){
                accToContactsMap.put(acc.id, acc.Contacts);
            }
        }
        for(Id accountId : accToContactsMap.KeySet()){
            Account accUpdate = new Account(id = accountId);
            accUpdate.Number_of_Contacts__c = accToContactsMap.get(accountId).size();
            updateAccounts.add(accUpdate);
        }
        update updateAccounts;
    }
    
    
    public static List<Contact> generateRandomContacts (Integer numContacts, String lastName,Id accId){
        List<Contact> contacts = new List<Contact>();
        
        for(integer i = 0; i<numContacts; i++){
            Contact c = new Contact();
            c.FirstName = 'Trail' + i;
            c.LastName = lastName + i;
            c.AccountId = accId;	
			contacts.add(c);
        }
        return contacts;
    }
    

    public static List<Account> generateRandomAccounts (Integer numAccounts){
        List<Account> accounts = new List<Account>();

        for(integer i = 0; i<numAccounts; i++){
            Account a = new Account();
            a.Name = 'Test' + i;
            accounts.add(a);
        }
        return accounts;
    }
}

AccountProcessorTest Class
 
@isTest
public class AccountProcessorTest {
    
    @testSetup public static void SetupTestData(){
        
        List<Account> listofAccounts = AccountProcessor.generateRandomAccounts(1);
        insert listofAccounts;
        List<Contact> listofContacts = AccountProcessor.generateRandomContacts(5, 'Test', listofAccounts.get(0).id);
        insert listofContacts;
        
    }
    @isTest
    public static void accountProcessing()
    {
        
        List<id> accIds = new List<id>();
        for(Account a : [select id from Account]){
            accIds.add(a.id);
        }
        Test.startTest();
        AccountProcessor.countContacts(accIds);
        Test.stopTest();
        Account accountAssert = [select Number_of_Contacts__c from Account where id IN :accIds LIMIT 1];
        system.assertEquals(5, Integer.valueOf(accountAssert.Number_of_Contacts__c));
    }
}

 
rahul soni 20rahul soni 20

Hi, 

public class AccountProcessor {
    @future
    
    public static void countContacts(List<ID> accountId){
        //this list we have created to bulkified the trigger
        List<Account> accountToUpdate = new  List<Account>();
        
        //here we have added the account and associacted account to a list via soql
        List<Account > accounts = [SELECT id, name, (SELECT ID FROM contacts) from Account where id in :accountId];
        //Now ittrate all the account one by one
        for(Account acc : accounts){
            // contacts from the above list will be here in this list
            List<Contact> contactlist = acc.contacts; 
            //size of the list in this filed 
            acc.Number_of_Contacts__c = contactList.size();
            // added to the list = accountToUpdate 
            accountToUpdate.add(acc);
        }
        update accountToUpdate;
    }
    
}
Test class 
@isTest
public class AccountProcessorTest {
    @isTest
    private static void testContactCount(){
        
        Account newAccount = new Account(Name = 'Test account');
        insert newAccount;
        
        Contact newContact = new Contact();
        newContact.FirstName    = 'Rahul';
        newContact.LastName     =  'Soni';
        newContact.Description  =  'Salesforce challanges';
        newContact.AccountId    =  newAccount.id;
        insert newContact;
        
        Contact newContact2 = new Contact();
        newContact2.FirstName    = 'Gagan';
        newContact2.LastName     =  'Soni';
        newContact2.Description  =  'Salesforce challanges';
        newContact2.AccountId    =  newAccount.id;
        insert newContact2;
        
        
        List<Id> accountids = new List<Id>();
        accountids.add(newAccount.Id);
        
        
        Test.startTest();
        AccountProcessor.countContacts(accountids);
        Test.stopTest();
    }
    
}

I hope this will be not futile.
Caleb Kuester 27Caleb Kuester 27
I think that there should be more rigorous requirements for the test class on this trail. Just to say it needs 100% coverage runs contrary to the point of having a test class.

This is how I achieved 100% coverage:
@isTest
public class AccountProcessorTest {
    public static testMethod void asdf(){
        Account a = new Account(Name='asdf');
        insert a;
        Test.startTest();
        AccountProcessor.countContacts(new List<Id>{a.Id});
        Test.stopTest();
    }
}
As I didn't actually need to add a Contact record in order to reach every line of code, I got by without having actually tested the functionality of the class.

The class itself was just as bad. As a developer, it's almost literally painful to see database queries and DML within a loop, but I was allowed to get away with it.
public class AccountProcessor {
	@future
    public static void countContacts(List<Id> accountIds){
        List<Contact> contactList;
        Account currentAccount;
        for(Id accountId :accountIds){
			contactList = [SELECT Id FROM Contact WHERE AccountId = :accountId];
            currentAccount = [SELECT Id FROM Account WHERE Id = :accountId];
            currentAccount.Number_of_Contacts__c = contactList.size();
            update currentAccount;
        }
    }
}
Just here to provide an example of how "I passed" still can mean there's more to learn. I'm not perfect by any means, but this will not pass any effective code review.

rahul soni 20 (above) has a great example of how to achieve this without having DML and SOQL in a loop.
 
PRAVEEN JAVVAJI 2PRAVEEN JAVVAJI 2
It worked , thanks.
PadyPady
Hi folks, this worked successfully for me with 100% code coverage and I solved the multiple Account Id, included the null check as well.

Tested the field update using Anonymous run.
 
public class AccountProcessor 
{
    
  @future
  public static void countContacts(set<Id> AcnIds) 
  {
      List<Account> Acclst = new List<Account>();
      List<Account> Acclst2 = new List<Account>();
       if(AcnIds != null)
	{

    Acclst = [Select Id, Number_of_Contacts__c , (select id from contacts) from Account Where Id IN :AcnIds];
      for(Account acc : Acclst)
      {
          List<Contact> Contlst = acc.contacts ;
			acc.Number_of_Contacts__c = Contlst.size();
          	Acclst2.add(acc);
      }
      		
  	}
      update Acclst2;
  }
}

Test Class used:
 
@isTest
public class AccountProcessorTest {
    @isTest
    private static void testContactCount()
    {
        
        Account newAccount = new Account(Name = 'Test account new');
        insert newAccount;
        
        Contact newContact = new Contact();
        newContact.FirstName    = 'Pady';
        newContact.LastName     =  'Test';
        newContact.Description  =  'Salesforce challanges';
        newContact.AccountId    =  newAccount.id;
        insert newContact;
        
        Contact newContact2 = new Contact();
        newContact2.FirstName    = 'Test';
        newContact2.LastName     =  '10';
        newContact2.Description  =  'Salesforce challanges';
        newContact2.AccountId    =  newAccount.id;
        insert newContact2;
        
        
        Set<Id> accountids = new Set<Id>();
        accountids.add(newAccount.Id);
        
        
        Test.startTest();
        AccountProcessor.countContacts(accountids);
        Test.stopTest();
    }
    
}

Anonymous code execution to validate the account updates:
 
Set<Id> idSet = new Set<Id>{'Place your acc Id here'};

AccountProcessor.countContacts(idSet);

Please use this as guidance only.

Do let me know if you have any issues.
Aaditya Srivastava 8Aaditya Srivastava 8

@Amit Chaudhary 8 Thanks for the answer. It is very helpful. Just a thought, we can optimize the two lines:

List<Contact> lstCont = acc.contacts ; acc.Number_of_Contacts__c = lstCont.size();

as

acc.Number_of_Contacts__c = (acc.contacts).size();

I am new to this and would appreciate suggestions anyone has on optimizing code.

 

ani ghoani gho
Jist with the help i received from above ......tested verified....Thanks
public class AccountProcessor {
    
      @future
    public static void countContacts(List<Id> recordIds){
           List<Account> accounts = [Select Id, Name from Account Where Id IN :recordIds];
           List<Contact> contacts = [Select Id, Name from Contact Where Id IN :recordIds];
          
        for(Account account:accounts){
            account.Number_of_Contacts__c = contacts.size();
        }
           update accounts;
    }

}



@IsTest
public class AccountProcessorTest {
    
    @testSetup 
    static void setupAccount(){

    List<Account> accounts = RandomContactAccountFactory.generateRandomAccounts(3);
    insert accounts;
    
    
    List<Contact> contacts = RandomContactAccountFactory.generateRandomContacts(3, 'TestAP', accounts.get(0).id);
    insert contacts;
  	
    }
    
	 @isTest
     static void testAccountProcessor(){
     
     List<id> accountIds = new List<id>();
		for(Account account : [select id from Account]){
            accountIds.add(account.id);
        }
         Test.startTest();
         AccountProcessor.countContacts(accountIds);
         Test.stopTest();
     
     }

}


//reused below one from earlier tutorial.....

public class RandomContactAccountFactory{
	public static List<Contact> generateRandomContacts( Integer maxNumberOfContacts, String lName, id accId ){
        
      
        List<Contact> contacts = new List<Contact>();        
		Integer counter=1;
        
		while(counter<=maxNumberOfContacts){
            Contact con = new Contact(LastName = lName );
			con.FirstName=('contactFirstName '+counter);
            con.AccountId=accId;
			contacts.add(con);
            counter++;
		}		
		return contacts;
	}
    
    public static List<Account> generateRandomAccounts (Integer numAccounts){
        List<Account> accounts = new List<Account>();

        for(integer counter = 0; counter<numAccounts; counter++){
            Account a = new Account();
            a.Name = 'accountName ' + counter;
            accounts.add(a);
        }
        return accounts;
    }
}



 
Rohan TelangRohan Telang
Thank you Amit for the help....
 
Ritik RajRitik Raj

According to problem, We have to acheive lump sum 100% Test Coverage.

I've achieved 100% Test Coverage via doing this. Hope, This will help you too.

AccountProcessor Class.
 

public class AccountProcessor {
  @future
    public static void countContacts(List<Id> accIds){
        List<Account> acc = [select id, 
                             Number_Of_Contacts__c, (select id from contacts)
                             from account where id in: accIds];
        for(Account a:acc){
            List<Contact> updContact = a.contacts;
            a.Number_Of_Contacts__c = updContact.size();
        }
        update acc;
    }
}
Test Class
@IsTest
public class AccountProcessorTest {
	
    public static testmethod void TestAccountProcessorTest(){
        Account a = new Account();
        a.Name = 'Test Account';
        Insert a;
        
        Contact cont   = New Contact();
        cont.FirstName = 'Bob';
        cont.LastName  = 'Masters';
        cont.AccountId = a.Id;
        Insert cont;
        
        List<Id> accIds = new List<Id>();
        accIds.add(a.Id);
        
        Test.startTest();
        AccountProcessor.countContacts(accIds);
        Test.stopTest();
        
        Account Acc = [select Number_Of_Contacts__c from Account where id = :a.Id LIMIT 1];
        System.assertEquals(Integer.valueOf(Acc.Number_Of_Contacts__c), 1);
    }
    
}

 
Aryan Gupta 11Aryan Gupta 11
Dis ich dam eji. I tlk lik fiji. Go lnk folow mi kno lern foto app etidor. https://blogseva.com/photo-par-gana-lagane-wala-apps-download/
Richard BruceRichard Bruce
According to problem, We have to acheive lump sum 100% Test Coverage.
I've achieved 100% Test Coverage via doing this. Hope, This will help you too.
AccountProcessor Class. for electric griddles reviews (https://smokelesscooking.com/electric-griddles-reviews/)
Bella John 10Bella John 10
Based on the situation it is necessary to attain the amount of 100% Test Coverage. I've been able to achieve 100% Test coverage by taking this approach. I hope that this can aid you in your quest too.
Class of Account Processor. For review  Click here (https://musikringtone.com/download/jack-sparrow-bgm-ringtone-download-2020/)
Shahid Hussain 18Shahid Hussain 18
Faith GreenwellFaith Greenwell
In view of the circumstance achieving how much 100 percent Test Coverage is important. I've had the option to accomplish 100 percent Test inclusion by adopting this strategy. I trust that this can help you in your mission as well.
Class of Record Processor. For audit Click here (https://ringtonezip.com/download/jack-sparrow-bgm-ringtone/)
Universal CelebritiesUniversal Celebrities
Emily Ratajkowski (https://universalcelebs.com/emily-ratajkowski/) has been out and about on the streets of New York City recently. Which isn’t exactly surprising in and of itself, except for the fact that’s she been doing so with a new hairstyle. Last week, the model unveiled a red dye job in a certain fiery shade that has been all the rage amongst celebrities recently.
 
CHEN YANTAOCHEN YANTAO
User-added image

---------------------------------------------------------------------------------------------------------------------------------------------
public class DailyLeadProcessor implements Schedulable {

    public void execute(SchedulableContext sc) {
        // Find the first 200 Lead records with a blank LeadSource field
        List<Lead> leadsToUpdate = [SELECT Id, LeadSource FROM Lead WHERE LeadSource = null LIMIT 200];

        // Update them with the LeadSource value of 'Dreamforce'
        for (Lead leadRecord : leadsToUpdate) {
            leadRecord.LeadSource = 'Dreamforce';
        }
        update leadsToUpdate;
    }
}

------------------------------------------------------------------------------------------------------------------------------------------------

@IsTest

public class DailyLeadProcessorTest {

    @IsTest

    static void testDailyLeadProcessor() {

        // Create 200 Lead records with blank LeadSource field

        List<Lead> leadsToInsert = new List<Lead>();

        for (Integer i = 0; i < 200; i++) {

            Lead lead = new Lead(FirstName = 'Test Lead ' + i, LastName = 'TestLastName ' + i, Company = 'TestCompany');

            leadsToInsert.add(lead);

        }

        insert leadsToInsert;

        // Schedule the DailyLeadProcessor to run

        String cronExp = '0 0 1 * * ?';

        Test.startTest();

        System.schedule('Daily Lead Processor Test', cronExp, new DailyLeadProcessor());

        Test.stopTest();

        // Query the Lead records to verify that they were updated correctly

        List<Lead> updatedLeads = [SELECT Id, LeadSource FROM Lead WHERE Id IN :leadsToInsert];

        // Assert that all Lead records were updated with the LeadSource 'Dreamforce'

        for (Lead lead : updatedLeads) {

            System.assertEquals('Dreamforce', lead.LeadSource);

        }

    }

}