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 in a few simple steps:

  1. Step 1: Add the needed Call-Back function to the ‘Advanced Scripts’
  2. Step 2: Add the relevant code to the Call-Back function in order to perform your logical script.

Step 1: Add the needed Call-Back function to the ‘Advanced Scripts’

Add the following code to your advanced script’s box:
function OnCreateIterationOrder(inChapterIndex, inIterationOrder)

{

}

This function is called, behind the scenes, in the first time a chapter is entered (while conducting a survey).
the 1st parameter, ‘inChapterIndex’ passes through the current chapters index that the surveyor is located, for example:

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].

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

Configure your function to suit your logical scripts.
Lets say for example, that we have 2 chapters and chapter 2 is a loop that iterates 5 times according to an answer scale.
We want chapter two to iterate in the order [2,1,3,4,5], if Q1 (for example, a question in chapter 1) answer is equal to 2, we can write something like this:

function OnCreateIterationOrder(inChapterIndex, inIterationOrder)
{
    var retVal = inIterationOrder;
    if(inChapterIndex==2)
    {
        if(Answer(1)==2)
        {
            retVal = [2,1,3,4,5];
        }
    }
    return retVal;
}

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

Basically, what I wrote is:
** If I am in chapter index 2 -> check if the answer of Q1 is 2
    -> if it is answer 2 -> set the iteration order to be [2,1,3,4,5];
    else -> set the iteration order to be the original one.
**

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

That’s it!

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

Comments

5 comments
  • Great insight! The only problem I see is that you have to check, whether ChapterIndex is the same before go to live. It will not be updated automatically, right?

    I guess there is a way to refer to ChapterIndex by its name?

    3
    Comment actions Permalink
  • I use that programming but have a problem if you back is not updated the inIterationOrder and stays with the first one that is generated.

    how i can do to update the inIterationOrder ?

    cuestion id 388c7d0c-6584-4801-b34d-eafa55b66b5a
    Qref(4) and chapter(2)

    regards

    0
    Comment actions Permalink
  • Hi Adolfo,

    At the moment that is how the mechanism is structured (once the random order of iterations is set it will be used for all the occasions where this loop chapter is executed).

    We have a road map development task to enable the re-ordering of the execution in different execution occasions.

    Regards,
    Daniel

    0
    Comment actions Permalink
  • And how can i show only 3 items out of 5 in below .

    function OnCreateIterationOrder(inChapterIndex, inIterationOrder)
    {
    var retVal = inIterationOrder;
    if(inChapterIndex==2)
    {
    if(Answer(1)==2)
    {
    retVal = [2,1,3,4,5];
    }
    }
    return retVal;
    }

    No need to show item 1,4 in below list.

    retVal = [2,3,5];

    Can it work ?

    0
    Comment actions Permalink
  • if you dont need 1 an 4. you can do some like this

    this method dond use the function "OnCreateIterationOrder"
    - using iteration entrance rule ( this method not rotate in specific order)
    ->InArray(IterationIndex,[2,3,5])

    --but if the loops depends fron any quest
    ->InArray(IterationIndex,[2,3,5]) && Contains(cuestion,iteratioindex)

    other method (use the function)
    if i understood well, yor loop iterate 5 times, but you cant use OnCreateIterationOrder, because you are returning 3 of 5 . in this case the function return a error and the loop iterate by default order.

    you can ()
    change this: retVal = [2,3,5] by return retVal = [2,3,5,1,4] and
    in the iteration entrance rule use

    !InArray(IterationIndex,[1,4]) //! = not to exclude

    There is another way, but it is longer to explain and my English is bad

    I hope this helps you.

    0
    Comment actions Permalink

Please sign in to leave a comment.