1

Still a beginner here learning to use SOQL and I am not sure why this is returning nothing. I start off with using a txt file as a static resource and reading its contents which in my case are report names, one in each line. I then use the line variable in my query line by binding it in order to retrieve report ID and folder name.

I run it through the loop and I can confirm that the line variable is indeed getting each individual line in the txt file. But when I check the output, all it returns is () each iteration.

// sr is where I store the contents of a txt file that has report names
StaticResource sr = [select body from StaticResource Where Name = 'test'];
String contents = sr.body.toString();

for(String line : contents.split('\n')) {
    
    // Get the report ID and folderName
    List <Report> reportInfo = [SELECT ID, FolderName FROM Report where Name = :line];
    
    System.debug(reportInfo);
    
}

Any help is appreciated. Thanks.

2
  • A good first step here would be to print the value of line you're working with in each iteration (via System.debug()) to see if there's anything obviously wrong. It does appear that body is base64 encoded, so it's possible (likely, even) that not decoding from base64 could be the issue.
    – Derek F
    Jan 17 at 19:58
  • @DerekF They're using toString, seems fine.
    – sfdcfox
    Jan 17 at 20:00

1 Answer 1

3

If I had to guess, you may have Windows-based line endings in your file; commonly called CRLF, written as \r\n. Consider normalizing your line endings first. You can also take this opportunity to query all the reports at once using a bind variable of a list of strings:

StaticResource sr = [select body from StaticResource Where Name = 'test'];
List<String> reportNames = sr.body.toString().replaceAll('\r\n','\n').replaceAll('\r','\n').split('\n');
Report[] reports = [SELECT Name, FolderName FROM Report WHERE Name = :reportNames];

Notice how we normalize CRLF and CR to just LF in two separate replaceAll calls. We need to do this because files from different OSes and software might have line endings that are CR, LF, or CRLF, so we need to be able to handle each of those situations separately.

3
  • Ok, I see. But now I need to iterate over each item in Reports by its ID and folderName as I will be collecting information from each report one by one. How would I go about doing that using this method?
    – apexdev
    Jan 17 at 21:38
  • @apexdev you use a for loop over the results of the query: for(Report reportRecord: reports) { ... }. You can use the methods in the ReportManager class to get additional information from the report.
    – sfdcfox
    Jan 17 at 22:27
  • thanks for your help.
    – apexdev
    Jan 18 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.