Working with arrays

We have already taken a look at different variable types, but there is a special type that we should explore a bit more—the array. Any variable type can be made into an array. An array is a container that holds multiple values of the same variable type. For example, an array of the String variable type may contain values such as "Mum", "Dad", "Sister", and "Brother", whereas an array of VC:VirtualMachine may contain multiple VMs.

Arrays in JavaScript

In JavaScript, you can use several methods to start, define, and initialize an array. The following are some of these methods:

  • By defining its content:
    var family = new Array("Mum", "Dad", "Sister", "Brother");
  • Using a more compact style:
    var family=["Mum", "Dad", "Sister", "Brother"];
  • Alternatively, if you want to initialize an empty array, just use the following:
    var family = new Array();
  • If you want to add an element to an array, you just push it in:
    family.push("Aunt");

Typically, we access the content of an array via JavaScript. To access an array, we use square brackets ([ ]) and write in it the number of the elements that you would like to access. Please note that the content count of an array starts from 0 (zero). For example, family[2] will return "Sister", and family[0] will return "Mum".

Note

Please note that before adding a value to an array, it must be defined. If you create an attribute array, you will need to either initialize it in JavaScript, or set it from the Orchestrator Client with no value.

We will work on an example in a second.

Defining and filling arrays in the workflow

Arrays are used a lot in Orchestrator, and you need to know how one can define them in the workflow as a parameter.

We will now create an array in the JavaScript example workflow that we created in the last section. We will first define an array and then fill it with content, as follows:

  1. Edit the JavaScript example workflow that you created in the last chapter.
  2. Go to the Attributes and create a new attribute called family.
  3. Click on string.
  4. Select Array Of and click on Accept.
    Defining and filling arrays in the workflow
  5. We have generated an empty array that will contain strings. Have a look at the displayed variable type.
  6. Now, we will fill it. Click on Not set under Value.
  7. Enter a New value and click on Insert value to add it to the array. Do the same with the values: Mum, Dad, Sister, and Brother.
  8. You can delete elements by clicking on the red X or change their order by using the arrows. Try it!
  9. You need to click on Accept, otherwise all the content that you put in the array will be lost.
    Defining and filling arrays in the workflow

JavaScript example

We created an array in the workflow and filled it with values. We will now use JavaScript to read it:

  1. From where we left off in the JavaScript example workflow, go to the schema.
  2. Edit the scriptable task and add the family array to it as an IN-parameter.
  3. Make sure that the myFirstValue parameter is of the type Number.
  4. Create a new OUT-parameter called arrayOut of the type String in the scriptable task.
  5. Add the following script to your Scripting section:
    arrayOut = family [myFirstValue];
    JavaScript example
  6. Run the script and check the results. Click on the little i that precedes the array to see the content of the array:
    JavaScript example

Now, we will add a value to the family array:

  1. Add a second scriptable task to the workflow directly after the existing one.
  2. Edit the scriptable task and add the family array to it as an IN- and OUT-parameter.
  3. Add the myFirstValue input parameter as an IN-parameter to the scriptable task.
  4. Add the following script:
    family = family.push(myFirstValue);
  5. Run the workflow again, and have a look at the result.

Predefined answers

So, now that we have learned about arrays, we can proceed to use an array in our workflow.

We are doing quite well in regards to the improvements to our workflow, but we still have some issues. For instance, a user can still enter 1.5 CPUs, which isn't going to work. So, we will offer the user only a list of values that they can choose from by performing the following steps:

  1. Edit the InstallFreshVM workflow.
  2. Click on Presentation and then select the Properties of the vmNbOfCpus input parameter.
  3. Click on Add Property and select Predefined answers.
  4. After the property has been added to the list, click on Not set.
  5. An array field will open up. Fill in the 1, 2, 3, and 4 values and click on Accept.
  6. Save and run the workflow.
    Predefined answers
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset