Flow: Use a flow to query field for a specific value

Desmo
Mega Guru

Hello Community,

I created a flow that contains an if statement to look up values in a string field.

 

servicenow_flow_query_contains_01.png

Factors:

  1. Value field is a string field.
  2. Value field contains comma separated values, i.e., dog,cat,bird,sheep.
  3. Short Description is a string field.
  4. Short Description can contain single or multiple values, i.e., dog or dog,cow,goat.
  5. Use Case 1: When Short Description contains a single value (i.e., dog), the flow is able to determine if value exists or does not exist in the Value field. If value does not exist, it is added into the Value field.
  6. Use Case 2: When Short Description contains multiple values (i.e., dog,pig,chicken), the flow is unable to determine if value exists in the Value field. It proceeds to add them into the Value field.

I have used business rules to perform similar functions, however, I would prefer to use a flow in this instance.

 

How to make flow look for unique values in a comma separated string?

 

Thank you in advance.

1 REPLY 1

Taha Habib
Tera Guru

Hello Desmo,

You can create a string type flow variable and then use "set flow variable" flow logic step, the script in the step will check if the value field matches to the short description if it does it will return an empty value and if it doesn't it will return the comma separated values which doesn't match, that you can insert into the value field as per your need.

In the below example I have wrote the script for the same scenario mentioned above. Note I have made value as a static variable for testing purpose.

TahaHabib_0-1685615652498.png


script in it:

/*
**Access Flow/Action data using the fd_data object. Script must return a value.
**Available options display upon pressing "." after fd_data
**example: var shortDesc = fd_data.trigger.current.short_description;
**return shortDesc;
*/
var result="";
var i;
var j;
var value="dog,cat,lion,tiger";
var itemarray= value.split(",");

if (fd_data.subflow_inputs.incident.short_description.includes(","))
{
  var item= fd_data.subflow_inputs.incident.short_description.split(",");
  for(i=0; i<item.length; i++)
    {
     var flag=0;   
     for(j=0; j<itemarray.length; j++)
      {
           if(item[i]==itemarray[j])
            {
                flag=1;
            }
      }
      if(flag==0)
      {     
          if(i!=item.length-1)
          {
            result= result+item[i]+",";
          }
          else
          {
              result= result+item[i];
          }  
      }
    }
}
else
{
    if(!(value.includes(fd_data.subflow_inputs.incident.short_description)))
     {
         result= fd_data.subflow_inputs.incident.short_description;
     }
}    

return result;


results:

TahaHabib_1-1685615751953.png

 

TahaHabib_2-1685615785069.png

I hope this helps you, please mark this solution as correct and helpful if it helps you. Thank you.