Join us Dec. 16 for The Community Hour, our first global virtual User Group event!

How can we use Text or String formatting in ServiceNow?

subhadeep1618
Tera Guru

Hello Everyone,

Do we have any nice way to format strings in ServiceNow scripts?

I am looking for something like the following:

var field1 = 'xyz';

var field2 = 'abc';

var output = "Welcome {0} to {1} instance".format(field1, field2);

OR

var output = "Welcome ${field1} to ${field2}";

 

Any help will be much appreciated.

1 ACCEPTED SOLUTION

Satishkumar B
Giga Sage

Hi @subhadeep1618 

you can format strings using custom methods. You can extend the String prototype to include a format method for indexed placeholders:
check the below code:

SatishkumarB_0-1722318313432.png

 

……………………………………………………………………………………………………

Please Mark it helpful 👍and Accept Solution✔️!! If this helps you!!

View solution in original post

4 REPLIES 4

Mark Manders
Tera Patron

Yes, you can. Question is where you want to show this, but you can script the format of a string field.

Satishkumar B
Giga Sage

Hi @subhadeep1618 

you can format strings using custom methods. You can extend the String prototype to include a format method for indexed placeholders:
check the below code:

SatishkumarB_0-1722318313432.png

 

……………………………………………………………………………………………………

Please Mark it helpful 👍and Accept Solution✔️!! If this helps you!!

Yes, like this:

 

var firstname = 'John';
var lastname = 'Doe';
gs.info('This is a message for {0} {1}', [firstname, lastname]);