Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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.


Please mark this post as a solution and also as helpful, if this resolves your issue or query.

Thanks,
Subhadeep Ghosh.
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
Giga Patron

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


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

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]);