+ Start a Discussion
Rey ChangRey Chang 

Help with Test Class - Schedule Apex

Hello All, 

   Newbie to Salesforce and i need help writing a test class - schedule apex + consuming csv file. Below is the code - 
=======================
global class FilePOC_ScheduleClass implements Schedulable {
 
     global Blob csvFileBody;
     global string csvAsString;
    
   global void execute(SchedulableContext sc)
   
    {   
        
        try{
            
            ContentVersion cv=[SELECT id,ContentDocumentId,versiondata
                                    FROM Contentversion
                                   WHERE Id='0990o00000B9TYOA3' ];
        
            csvFileBody =cv.VersionData;
            csvAsString= csvFileBody.toString();
        
            // Logic to split the file contents 
            List<String> csvFileLines= csvAsString.split('\n');
            List<String> newList = new List<String>();
    
            // Logic to remove the fist line - the column name
            for(Integer i=1;i<csvFileLines.size();i++){
                newList.add(csvFileLines[i]);
            }
    
            List<Account> accList = new List<Account>();
            
            for(integer i=0;i<newList.size();i++){
                
                List<String> values = newList[i].split(',');
                
                // Prepare the Account fields for insert
                Account accObj = new Account();
                accObj.Name = values[0];
                accObj.accountnumber = values[1];
                accObj.Type = values[2];
                accObj.AccountSource = values[3];
                accObj.Industry = values[4];
                
                acclist.add(accObj);
            }
            
            // Insert the account collection
            insert acclist;
        }
        catch (Exception e)
        {
            
        }
    }
}
Please help !! 
Thanks 
Rey....
 
Best Answer chosen by Rey Chang
Derrick AbbeyDerrick Abbey
Hi Rey,

Have you seen this page in the Apex developer guide?  There is an example of a test class for schedulable Apex here  (toward the bottom).  
In short, you'll need to use the Test.StartTest() method and System.schedule method to create your tests.  I'd suggest uploading a csv file as a static resource and using that as your test file for the unit tests.

On a side note, what is this class meant to accomplish?  Since the contentVersion Id is hard-coded, it will only load that data once, and then it would just reload the same data each time. Also, if you're just trying to be able to upload records via a CSV file, is there a reason you don't just use Dataloader, the Rest API or the bulk API?