Service Catalog - if Statement for a text variable that contains "string"

Chase Stevenson
Mega Guru

I have a workflow set up for a catalog item and I want to include an IF Statement in the workflow that will check whether a single text field contains/starts with the following strings: "10.", "161.", "172."

To summarize, the script is supposed to check if the entered IP address in the variable begins with a 10. or a 161. or a 172. address.

Variable Name: source_ip

I tried scripting it out but I have no idea where to start as I want it to specifically check that the string BEGINS with the above numbers. I cannot use a CONTAINS condition because an IP of x.x.10.x would still trigger the statement.

Here's a picture of the proposed script that I've wrote up, can anybody provide any recommendations?

find_real_file.png

1 ACCEPTED SOLUTION

What type is the "source_ip" catalog variable?

You can force your script variable "sourceIP" to a string with the following:

var sourceIP = current.variables.source_ip.toString();

 

View solution in original post

10 REPLIES 10

Ashok Katam
Mega Guru

Hi

You can use something like this

function ifScript(){
var sourceIP = current.variables.source_ip;
   if(sourceIP.indexOf("10.") == 0 || sourceIP.indexOf("161.") == 0 ||    sourceIP.indexOf("172.") == 0) {
       return 'yes';
   }
return 'no';
}

Thank you for the suggestion. I tried the indexOf() function and get a workflow error: "Cannot find function indexOf in object":

find_real_file.png

Code looks like the following:

find_real_file.png

Can you try to add 

var sourceIP = "";
sourceIP = current.variables.source_ip;

What type is the "source_ip" catalog variable?

You can force your script variable "sourceIP" to a string with the following:

var sourceIP = current.variables.source_ip.toString();

 

Thanks for your response - this was the missing piece that I needed.

It is a single line text variable and I would have guessed it defaulted to a string format, but the .toString() got it working.

 

For reference, the working code looks like:

find_real_file.png