The CreatorCon Call for Content is officially open! Get started here.

Loop through variables in flow designer

Community Alums
Not applicable

Hi guys

 

I have a requirement where once the catalog item is submitted a catalog task should be created for each variable which are marked true(these are checkboxes)

The short description also should be variable label name

Since there are about 10 checkboxes,any idea how to do this by looping?

 

thanks

bimsari

1 ACCEPTED SOLUTION

Dnyaneshwaree
Mega Sage

Hello @Community Alums ,

You can achieve this requirement by using Flow Designer with a combination of actions and loops. Here's a step-by-step guide:

  1. Create the Flow:

    • Go to Flow Designer and create a new Flow.
    • Set the trigger to "Catalog Item Requested."
  2. Add Actions to the Flow:

    • Add a "Create Task" action.
    • Use the Script step to loop through the variables and create tasks for those that are marked true.
  3. Loop Through Variables:

    • You can use a Script step to loop through the variables.
      Here is an example script that you can use in the Flow Designer:

 

(function execute(inputs, outputs) {
    // Get the current request item (RITM)
    var gr = new GlideRecord('sc_req_item');
    gr.get(inputs.request_item_id);

    // Get the variables from the RITM
    var variables = gr.variables;

    // Loop through each variable
    for (var v in variables) {
        var variable = variables[v];

        // Check if the variable is a checkbox and is true
        if (variable.getType() === 'boolean' && variable.getValue() === 'true') {
            // Create a new catalog task
            var task = new GlideRecord('sc_task');
            task.initialize();
            task.request_item = inputs.request_item_id;
            task.short_description = variable.getQuestion().getLabel();
            task.insert();
        }
    }
})(inputs, outputs);
​

 

  1. Define Inputs and Outputs:

    • Ensure you define the necessary inputs for the script action. For example, inputs.request_item_id should be the RITM ID that you can get from the trigger or a previous action.
  2. Add Conditions (Optional):

    • If you want to add any additional conditions or actions based on the catalog item or other variables, you can do so before or after the loop.
  3. Test the Flow:

    • Test the flow by submitting a catalog item with the checkboxes and verify that tasks are created correctly.

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru

View solution in original post

2 REPLIES 2

Dnyaneshwaree
Mega Sage

Hello @Community Alums ,

You can achieve this requirement by using Flow Designer with a combination of actions and loops. Here's a step-by-step guide:

  1. Create the Flow:

    • Go to Flow Designer and create a new Flow.
    • Set the trigger to "Catalog Item Requested."
  2. Add Actions to the Flow:

    • Add a "Create Task" action.
    • Use the Script step to loop through the variables and create tasks for those that are marked true.
  3. Loop Through Variables:

    • You can use a Script step to loop through the variables.
      Here is an example script that you can use in the Flow Designer:

 

(function execute(inputs, outputs) {
    // Get the current request item (RITM)
    var gr = new GlideRecord('sc_req_item');
    gr.get(inputs.request_item_id);

    // Get the variables from the RITM
    var variables = gr.variables;

    // Loop through each variable
    for (var v in variables) {
        var variable = variables[v];

        // Check if the variable is a checkbox and is true
        if (variable.getType() === 'boolean' && variable.getValue() === 'true') {
            // Create a new catalog task
            var task = new GlideRecord('sc_task');
            task.initialize();
            task.request_item = inputs.request_item_id;
            task.short_description = variable.getQuestion().getLabel();
            task.insert();
        }
    }
})(inputs, outputs);
​

 

  1. Define Inputs and Outputs:

    • Ensure you define the necessary inputs for the script action. For example, inputs.request_item_id should be the RITM ID that you can get from the trigger or a previous action.
  2. Add Conditions (Optional):

    • If you want to add any additional conditions or actions based on the catalog item or other variables, you can do so before or after the loop.
  3. Test the Flow:

    • Test the flow by submitting a catalog item with the checkboxes and verify that tasks are created correctly.

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru

Community Alums
Not applicable

Thank you so much for your reply. This code worked!!! , I had to change 

variable.getQuestion().getType() ===