- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2024 09:46 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2024 10:48 PM - edited 07-29-2024 10:49 PM
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:
……………………………………………………………………………………………………
Please Mark it helpful 👍and Accept Solution✔️!! If this helps you!!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2024 10:10 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2024 10:16 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2024 10:48 PM - edited 07-29-2024 10:49 PM
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:
……………………………………………………………………………………………………
Please Mark it helpful 👍and Accept Solution✔️!! If this helps you!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 11:23 AM
Yes, like this:
var firstname = 'John';
var lastname = 'Doe';
gs.info('This is a message for {0} {1}', [firstname, lastname]);