Flow designer - how to increment a variable while looping

Tomko
Tera Expert

I am working on a Create User flow using flow designer with the Azure AD spoke (though this question is really for any flow that involves this kind of use case).  Given a string (person name) input, I need to create a user.  Simple enough, but I need to make the User Login ID and Email Alias unique.  To do this, I can use the Look Up User ID action.  When that action returns "User Not Found", I know I am good to create a user. 

However, if the user is found, I want to add a number to the end of the values and search again until I get a User Not Found message (e.g. John.Smith, John.Smith1, John.Smith2).  Easy enough with normal code, but I am having trouble figuring it out in Flow Designer.  

My initial approach was to add a Do Until loop in the flow, with a simple script action that increments a counter.  The problem is that I don't see a way to make an action use its own output as an input (the action is smart enough to initialize the counter to 0 if there is no input value supplied).

My second thought was to build the loop into a self-contained action (see below), but I am running into the same issue - because the script and REST steps are separate, I don't have a good way to loop.

Is there a way to call an action or an action step from within a script action step?

 

Script:

(function execute(inputs, outputs) {

     var inputString = inputs.name;
     var tempString = "";
     tempString = inputString.replace(/\s/g, ".");
     tempString = tempString.replace(/[^a-zA-Z0-9.]/g, "");
     var baseID = tempString;
     outputs.result = "failure"
     outputs.message = "Unable to create a unique User ID"

     for(var i = 0; i < 50; i++){
          var tempID = baseID

          if(i > 0){
               tempID = tempID + "." + i;
          }

          testID = tempID + "@" + inputs.domain;

          if(checkUserID(testID){
               i = 50;
               outputs.user = tempID;
               outputs.result = "success"
               outputs.message = "Unique User ID generated"
          }

     function checkUserID(ID) {
          var query = '',
          query = query.concat("userPrincipalName eq '").concat(ID).concat("' ");

          //NEED TO RUN THE LOOKUP USER REST STATEMENT HERE
          if(RESULT OF REST STATEMENT IS USER NOT FOUND){
               return true;
          }
          return false;
     }

})(inputs, outputs);

1 ACCEPTED SOLUTION

muthuramachandr
ServiceNow Employee
ServiceNow Employee

you can call the action from the script

 

try {
var inputs = {};
inputs['INPUT_VARIABLE_Name'] = ;

// Start Asynchronously: Uncomment to run in background. Code snippet will not have access to outputs.
// sn_fd.FlowAPI.startAction('ACTION_NAME', inputs);

// Execute Synchronously: Run in foreground. Code snippet has access to outputs.
var outputs = sn_fd.FlowAPI.executeAction('ACTION_NAME', inputs);

// Get Outputs:
// Note: outputs can only be retrieved when executing synchronously.
var payload = outputs['payload']; // String

} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}

View solution in original post

2 REPLIES 2

muthuramachandr
ServiceNow Employee
ServiceNow Employee

you can call the action from the script

 

try {
var inputs = {};
inputs['INPUT_VARIABLE_Name'] = ;

// Start Asynchronously: Uncomment to run in background. Code snippet will not have access to outputs.
// sn_fd.FlowAPI.startAction('ACTION_NAME', inputs);

// Execute Synchronously: Run in foreground. Code snippet has access to outputs.
var outputs = sn_fd.FlowAPI.executeAction('ACTION_NAME', inputs);

// Get Outputs:
// Note: outputs can only be retrieved when executing synchronously.
var payload = outputs['payload']; // String

} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}

Tomko
Tera Expert

Sounds like exactly what I need, thanks!