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

Danish Bhairag2
Tera Sage
Tera Sage

@mounika32 ,

 

Try the .length in a new line then check if it returns proper value or not

var linesArray = itar.split('\n');

    // Get the count of lines

    var linesCount = linesArray.length;

 

Thanks,

Danish

 

 

Anil Lande
Kilo Patron

Hi,

Can you please explain what type of variable it is?

And can you please show the record screenshot storing this value?

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

Hi @Anil Lande  and @Danish Bhairag2 , its a multi line text varaible

 

 

Danish Bhairag2
Tera Sage
Tera Sage

Hi @mounika32 ,

 

Please share the snip of the variable in which data is stored please. Need to check how data is stored in that variable

 

Thanks,

Danish

 

Anil Lande
Kilo Patron

Please try below:

var current = new GlideRecord('change_request');
current.get('fdbce3f72f323110ae17debcf699b65c');
var itar = current.variables.existing_itar_objects.toString(); // 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