Highlighted
Honored Contributor.
Honored Contributor.
349 views

Script to calculate the count of characters in a IM description

Hi Experts,

 

My requirement is to implement a scenario which will show a message when the incident description given by user is more than a particular limit. And the script should count both characters and new lines(\n). I am planning to implement this through format control. I have tried the following code under JavaScript tab of FC,  just to see length of description (action) field which user entered.

update : true

var len=record.action.length();

print(len);

But it throwed error while updating an IM.

Could you please suggest a way to find the length of the description that the user have just entered in  a particular IM.

 

Thanks & Regards,

Sandhu Mol

0 Likes
4 Replies
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: Script to calculate the count of characters in a IM description

How about CALCULATIONS tab and use lng function there.

Spoiler
RAD function: lng
A RAD function that returns the number of elements in an array or structure and the number of characters in a string. Remember to denull arrays to get the correct number of elements, but be careful when denulling arrays of structures.
Function
lng
Format
lng($variable)
Where $variable is the array, structure, or string.
Factors
 The $variable must be an array, structure or a character string.
 Arrays return the number of elements, NULL or otherwise.
 Strings return the number of characters.
 Structures return the number of fields.
Examples
lng("1234567890") returns 10
lng({1,2,3}) returns 3
lng({1,2,3,,,,}) returns 7
lng(denull({1,2,3,,,,})) returns 3
0 Likes
Highlighted
Super Contributor.. Super Contributor..
Super Contributor..

Re: Script to calculate the count of characters in a IM description

If you're set on using JS you could try changing your line to this

var len=record.action.toArray().toString().length;
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

Re: Script to calculate the count of characters in a IM description

Or use system functions.

var len = system.functions.lng(system.functions.denull(record.action));

0 Likes
Highlighted
Super Contributor.. Super Contributor..
Super Contributor..

Re: Script to calculate the count of characters in a IM description

Action is an array field so in this case the system.functions.lng would return the array length rather than the character count if you don't convert it to string first.

 

I tried using the RAD function to convert action to string. The length count comes out higher with that method compared to JS because it appears to be counting the curly brackets and quotes marks. Depending on the requirement that might be desirable.

 

Here's the test I ran to compare

var imRec=new SCFile("probsummary");
var ticketNumber="IM2000943"; //replace this with your ticket number
imRec.doSelect(new QueryCond("number",EQ,ticketNumber));

print("JS Func Text: "+imRec.action.toArray().toString());
print("JS Func Text Length: "+imRec.action.toArray().toString().length);
print("RAD Func Text: "+system.functions.denull(system.functions.str(imRec.action)));
print("RAD Func Text Length: "+system.functions.lng(system.functions.denull(system.functions.str(imRec.action)))); //includes curly brackets and quotes in the length count
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.