Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to capture the last word in the variable

chandan31
Tera Contributor

Hi All,

 

 

Can you tell me how store Last word one of the variable :

 

short Description :HR Profile GET API failure for cili

 

In this case i want to store Cili in on of the variable, Can you help me .

 

Thanks and Regards,

chandan 

1 ACCEPTED SOLUTION

You're not invoking the function correctly. Have you used JavaScript previously to ServiceNow?

Correct usage:

function getLastWord(str) {
  // Trim any whitespace from the ends of the string
  str = str.trim();

  // Split the string into an array of words
  var words = str.split(/\s+/);

  // Return the last word
  return words[words.length - 1];
}

getLastWord(current.getValue('short_description'));

View solution in original post

6 REPLIES 6

You're not invoking the function correctly. Have you used JavaScript previously to ServiceNow?

Correct usage:

function getLastWord(str) {
  // Trim any whitespace from the ends of the string
  str = str.trim();

  // Split the string into an array of words
  var words = str.split(/\s+/);

  // Return the last word
  return words[words.length - 1];
}

getLastWord(current.getValue('short_description'));

Jitendra Diwak1
Kilo Sage

Hi @chandan31

 

PPlease try this below code

 

// Assuming 'shortDescription' contains the short description provided

var shortDescription = 'HR Profile GET API failure for cili';

 

// Split the string by spaces

var words = shortDescription.split(' ');

 

// Get the last word

var lastWord = words[words.length - 1];

 

// Now 'lastWord' contains 'cili', you can store it in a variable or use it a s needed

 

 

Please accept my solution if it resolves your issue and thumps 👍 up

 

Thanks

Jitendra 

Please accept my solution if it works for and thumps up.