how to get the count of lines from multi line text string

mounika32
Tera Contributor

how to get the count of lines from multi line text string, I tried below but its returing undeined instead of 2

mounika32_0-1701927088649.png

 

1 ACCEPTED SOLUTION

User might have entered extra 'ENTER' so it is adding extra line at the end.

You can use logic like below:

var current = new GlideRecord('change_request');
current.get('fdbce3f72f323110ae17debcf699b65c');
var itar = current.variables.existing_itar_objects.toString().trim(); // use toString() to change it to string
var lineCount = itar.split('\n').length;
gs.info(lineCount);
Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

15 REPLIES 15

Harsh_Deep
Giga Sage
Giga Sage

Hello @mounika32 

 

Please use this script -

var itar = current.variables.existingg_itar_object;
var linesArray = itar.split('\n');
var lineCount = linesArray.length;
gs.info('Number of lines in the multi-line text: ' + lineCount);

 

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

Hello @Harsh_Deep  I am getting undefined i nthe count 

mounika32_0-1701929898358.png

 

Hello @mounika32 

 

Please use this-

var gr = new GlideRecord('sc_req_item');
gr.get('3fc5485497433110cf05f8971153af79');
var itar = gr.variables.software_requirements.toString();
var linesArray = itar.split('\n');
var lineCount = linesArray.length;
gs.info('Number of lines in the multi-line text: ' + lineCount);

 

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

 

Rahul Talreja
Mega Sage
Mega Sage

Hi @mounika32 ,

Try Below script - 

var chatHistory = "Line 1\nLine 2\nLine 3\nLine 4";
var lines = chatHistory.split('\n');
var lineCount = lines.length;
gs.info(lineCount);
Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul
Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul