Concatenate certain values from two or more string fields into one

gauravmahendru
ServiceNow Employee
ServiceNow Employee

Hi,

 

I am trying to achieve the following:

 

Field A (Country): INDIA

Field B (Location): Pune

Field C (Asset Tag): GM110022

Concatenated Field: IN_Pune_GM110022

 

I know the calculated field type can give me "INDIA_Pune_GM110022", however, I want only the first two characters from Field A.

 

Guidance appreciated.

5 REPLIES 5

Sankar N
Kilo Guru

Please try with below script in business rule, 

 

 

(function executeRule(current, previous /*null when async*/) {

  // Get the values of the three fields
  var country = current.getValue('Country');
  var location = current.getValue('Location');
  var assetTag = current.getValue('Asset Tag');

  // Concatenate the values with underscores
  var concatenated = country.substring(0,2) + '_' + location + '_' + assetTag;

  // Set the value of the Concatenated Field
  current.setValue('Concatenated Field', concatenated);

})(current, previous);

 

 

n the script above, we're using the substring() function to get the first two characters of the Country field. The getValue() function is used to retrieve the values of the fields, and setValue() is used to set the value of the Concatenated Field.

Make sure to replace the field names with the actual names of the fields in your ServiceNow instance. Also, you can adjust the separator used between the fields by changing the underscore character in the concatenated variable.

Thanks this worked for me. I used a Before business rule, insert and update selected

Nitin Panchal
Tera Guru

Hi @gauravmahendru , 

 

You can use following script . 

(function calculatedFieldValue(current) {

var fullString = current.country.toString().substring(0, 2) + '_' + current.location.toString()+'_'+current.asset_tag.toString();
return fullString;

})(current);

 

If this helped you please mark it helpful / mark it correct answer. 

 

Thank you

Nitin

Nitin Panchal
Tera Guru

Hi, @gauravmahendru . Thank you for marking my response as helpful. Please consider marking it as the answer if my reply helped you resolve the issue. This will help the community. 

Thank you

Nitin