# How To Set The Order Of Iterations Within a Loop

Overview

This How-To will explain and show how you can set the order of iterations within a given loop chapter.

function OnCreateIterationOrder(inChapterIndex, inIterationOrder)

{

}

This function is a callback so is called by the runtime engine when a loop chapter is entered (while conducting a survey).
the 1st parameter, ‘inChapterIndex’ passes through the current chapters index that the surveyor is located, for example:

Note: The index is 4 in this case.

And the 2nd parameter, ‘inIterationOrder’ passes through the original iteration order of the loop, for example:
If chapter 1 is a loop that iterates 5 times, it will pass through: [1,2,3,4,5].

Add the relevant code to the Call-Back function in order to perform your logical script.

In the above example (screenshot) chapter 4 (Q11) iterates 5 times based on an answer scale.

Lets also assume that I have int he survey a prior question that is with variable name Q1 and has 5 answers (uses the same scale) and that I have set it to randomize the answers.
We want chapter to iterate in the same order as the answers of Q1 were we can write the call back to be:

`function OnCreateIterationOrder(inChapterIndex, inIterationOrder){    var retVal = inIterationOrder;    if(inChapterIndex==4)    {        retVal = GetQuestionAnswersRandomOrder(Q1);    }    return retVal;}`

* this should be written in the function we wrote in Step 1, within the advanced scripts box, as following:

You can write any script that you wish in order set different loop iterations order with this method.

Tip: As Chapter indexes may move while you script (add additional chapters etc.) you can build a function that returns the chapter index from its name:

`function GetCahpterIndexFromName(inName){  var retVal = -1;  var chapters = GetAllChapters();  for (var i=0; i<chapters.length; i++)  {    if (chapters[i].Name == inName)    {      retVal = chapters[i].Idx;    }  }  return retVal;}`

That’s it!

4 out of 5 found this helpful
Have more questions? Submit a request

1 comment
• Dear yevhen,
Thanks for your mail but i in urgent situation and I can not read guide. Can you say me should I add that codes to where?

function OnCreateIterationOrder(inChapterIndex, inIterationOrder)
{
var retVal = inIterationOrder;
if (inChapterIndex == 6)
{