function "contains" in UI Builder component formula editor

Nisarg Thakur
Tera Guru

How do I build a condition in the UI Builder component formula editor to check for a condition 'contains'? Like for below, I want to check if the shortDescription contains 'time sensitive', then return 'High' else return 'Medium'

NisargThakur_1-1666628169451.png

 

I checked this article that talks about existing formula editor functions, but I could not find anything that allowed me to do that. Any workarounds/suggestion for this?

3 REPLIES 3

Claude DAmico
Kilo Sage

You could use a conditional operator:

 

//condition ? "True" : "False";
var priority = @STate.shortDescription.indexOf("time sensitive") > -1 ? "High" : "Medium";

 

Claude E. D'Amico, III - CSA

Brad Tilton
ServiceNow Employee
ServiceNow Employee

I don't think you can use JS operators in the formulas, but you could try indexOf and see if it works. Otherwise you'll probably need to script that property instead of using a formula. When you flip the property to script

BradTilton_0-1666702572427.png

 

you can do something like:

 

/**
  * @Param {params} params
  * @Param {api} params.api
  * @Param {TransformApiHelpers} params.helpers
  */
function evaluateProperty({api, helpers}) {
  var answer = "Medium";
  if (api.state.shortDescription.toString().toLowerCase().indexOf('time sensitive') > -1) {
    answer = "High"
  }
	return answer;
}

  

I am trying to do this in the data source configuration pane. I don't have an option to toggle it to a script.

NisargThakur_0-1666878989873.png

I am assuming I might have to run a page script and set the final value in a state and bind here.