I want to fetch last word dynamically
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 07:13 PM
Hi All ,
I want to fetch the last word from the short description and store in the one of the variable :
var gr= current.short_description;
var words = s.trim().split(gr);
var lengthOfLastWord = words[words.length - 1];
gs.print(lengthOfLastWord);
var ab=lengthOfLastWord;
This script is not fetching the last word .Can anyone help me .
Thanks and Regards,
Chandan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 09:00 PM
Hi @chandan86patra ,
Please try the below:
var sentence = "The world works with ServiceNow";
var words = sentence.split(" ");
var lastWord = words[words.length - 1];
gs.log("The last word of sentence is: " + lastWord);
Output:
*** Script: The last word of sentence is: ServiceNow
You can replace the string to your short description attribute as below:
var sentence = current.short_description;
var words = sentence.split(" ");
var lastWord = words[words.length - 1];
gs.log("The last word of sentence is: " + lastWord);
Please hit like and mark my response as correct if that helps.
Mark this as Helpful / Accept the Solution if this helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 09:03 PM
@chandan86patra Please try the following script. It should work.
var gr= current.short_description;
var words = gr.trim().split(' ');
var lengthOfLastWord = words[words.length - 1];
gs.print(lengthOfLastWord);
var ab=lengthOfLastWord;
Hope this helps.