Answered question

How do I loop a transaction the number of times as values from a File variable?

Let's assume you have a File variable, but you are not always certain the number of values in it each time. You want to run 2 or more loops using this file variable in each one, and don't want to use each value more than once. How can you automate this so it only runs the looped transaction an exact number of times that you have values in the File variable?

Keith L.
Keith L.

Keith L.

Level
2
1224 / 2000
points
Team
Keith L.
Keith L.

Keith L.

Level
2
1224 / 2000
points
Team

You need to use Javascript logical action and code to parse your File used in the File Variable to get a line count. This line count value can then be used to run your loop count an exact number of times matching your values in the file.

Here is an example of the code you would use:

=====================
//Declare file with variable. Modify to match the path to your file variable.
var fileName = "C:\\Users\\NeoloadGuru\\Desktop\\Account.csv";

//Read File and get line count
var reader = new Packages.java.io.LineNumberReader(new Packages.java.io.FileReader(fileName));
while ((reader.readLine()) != null) {}
var lineCount = reader.getLineNumber();
reader.close();

//modify value by subtracting 1 from value if file variable has a column header line, otherwise don't use.
var lineCount = lineCount-1
//Log value of line count from file
logger.debug("Line number is = "+lineCount);

//Export variable with value of line count back to Neoload script.
context.variableManager.setValue("LoopCount",lineCount);

=====================

Now, this will parse the file to get a line count of values in the file. It can subtract 1 from the total to account for a column header, and export that line count value back to Neoload to use in the Loop Count.

Please refer to attached sample project for details.

Did you find this useful ?

No (0)

Yes (0)

0% of users found this answer useful