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
SimmoSimmo 

Advanced Apex Specialist - Step 4

I keep getting this on the 'Check Challenge' for Step 4 of the Adavanced Apex Specialist:

Ensure constructAccounts returns a list of size cnt of uniquely named Account records with all of the required fields populated.

This makes no sense to me as my method does create the correct number of Accounts and places them in a List? They are also uniquely named?

     /**
     * @name CreateAccounts
     * @description Constructs a list of Account records for unit tests
    **/
    public static List<Account> ConstructAccounts(Integer cnt) {
    
        // Ensure this method returns a list of size cnt of uniquely named Account records
        // with all of the required fields populated.
        
        List<Account> accounts = new List<Account>();
        
        // Create the number of Accounts that was passed in
        for (Integer i=0; i < cnt; i++ ) {
            
            Account oAccount = new Account();
            
            oAccount.Name = 'TestAccountLtd_' + cnt;
            
            accounts.add(oAccount);
            
        }
        
        return accounts;
    }

Any ideas why it is failing this?

 
SimmoSimmo
Sorry here is a more readble sample of the code:
 
/**
     * @name CreateAccounts
     * @description Constructs a list of Account records for unit tests
    **/
    public static List<Account> ConstructAccounts(Integer cnt) {
    
        // Ensure this method returns a list of size cnt of uniquely named Account records
        // with all of the required fields populated.
        
        List<Account> accounts = new List<Account>();
        
        // Create the number of Accounts that was passed in
        for (Integer i=0; i < cnt; i++ ) {
            
            Account oAccount = new Account();
            
            oAccount.Name = 'TestAccountLtd_' + cnt;

            
            accounts.add(oAccount);
            
        }
        
        return accounts;
    }

 
SimmoSimmo
Ok, I see it now with a fresh pair of eyes!!

The Account name has the cnt variable being concatenated to the end and not the i variable, so all the Account names are the same. Doh!!