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
Colleen Cleveland 6Colleen Cleveland 6 

Include only certain file types

I have developed a code that pulls records into a custom object from a specified directory that has been setup so our clients can view training videos we have hosted on a server outside of salesforce. The code works as it should (it deletes all records and then compiles all of the records in memory and when it completes its search inserts a new copy of each record), but it pulls in every file located in the subfolders and not just the video files i want.
This is my first time working with coding so am asking for assistance in how to write a line to include only .mp4, .swf, .html as the file types that are pulled. The code is based off of one we created to pull in document records the same way.



// Remove existing Training Video records which will remove Training Video Records records
        delete [SELECT Id FROM Training_Video__c] ;

        videoRecordsToCreate = new List<Training_Video_Record__c>();
        trainingVideosToCreate = new List<Training_Video__c>();

        // Interate through the file to build records   
        for (Integer i=1;i<fileLines.size();i++) {

            system.debug('Looping through fileLines: ' + i + ' Length: ' + fileLines[i].length() + ' ' + fileLines[i]);

            // Nothing to see here, move along
            if (fileLines[i].contains('Directory of') != true){continue;}

            // Find and Training Video Category
            String[] inputValues = new String[]{};
            inputValues = fileLines[i].split('\\\\');
           
            // Look for Sub Directory and Get Training Video Directory and Training Video Records
            if (inputValues.size()==3) {               

                system.debug('Found two path qualifiers in fileLines: ' + i + ' ' + fileLines[i]);            
                   
                Training_Video__c tv = new Training_Video__c();
                tv.Name = inputValues[2]; //<--Workflow rules will change the name
                insert tv;                  

                // Read File Names and Create Video Records
                eoFiles = false;
                i = i+4;
                while (eoFiles != true) {                               

                    // Check for end of Files
                    if (fileLines[i].contains('File(s)') == true) {
                        eoFiles = true;
                        continue;
                        }
                     
                    if (fileLines[i].substring(31).length() > 0) {

                        system.debug('fileLines: ' + i +' ' + fileLines[i]);
              
                        Training_Video_Record__c vr = new Training_Video_Record__c();
               
                        vr.Name = fileLines[i].substring(40);
                        vr.Video__c = tv.Id;
                        videoRecordsToCreate.add(vr);
               
                        }
Best Answer chosen by Colleen Cleveland 6
ShaneMcLaughlinShaneMcLaughlin
if (fileLines[i].substring(31).length() > 0) {

  system.debug('fileLines: ' + i +' ' + fileLines[i]);

//new if section starts here-------------------
  if (
   fileLines[i].contains('.mp4') ||
   fileLines[i].contains('.swf') ||  
   fileLines[i].contains('.html')
  ){
     Training_Video_Record__c vr = new Training_Video_Record__c();
               
      vr.Name = fileLines[i].substring(40);
      vr.Video__c = tv.Id;
      videoRecordsToCreate.add(vr);
  } 
//end of the new stuff              
}
note: this won't handle .MP4, .SwF or other variations--it's case sensitive.  Thanks for joining the Houston Dev Group!