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

Mahendra RC
Mega Sage

Hi LDN1622,

If you want to check if string starts with "10.", "161.", "172."

Please use the below code and let me know if you face any issue (since I didn't actually tried it myself):

var sourceIP = current.variables.source_ip;

if(sourceIP.startsWith("10.") || sourceIP.startsWith("161.") || sourceIP.startsWith("172.")) {

  return 'yes';

}

return 'no';

 

if you want to check if string field contains 10.", "161." or "172."

 

var sourceIP = current.variables.source_ip;

if(sourceIP.indexOf("10.") >= 0 || sourceIP.indexOf("161.") >= 0 || sourceIP.indexOf("172.") >= 0) {

  return 'yes';

}

return 'no';

 

Please let me know if this is not what you are looking for.

 

Please mark this correct, if it resolves you problem.

Thanks

Hello, thank you for the suggestion.

I tried the startsWith() function that you suggested and get the following result when the workflow is used:

find_real_file.png

Script looks like the following:

find_real_file.png

Use current.variables.source_ip.toString(); and try ?

Working now, thank you for the assistance.