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

mounika32
Tera Contributor
 
1 ACCEPTED SOLUTION

Please close this question by marking my response as correct solution so that it will be helpful for future users to find correct solution.

Happy Learning 🙂

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

12 REPLIES 12

Samaksh Wani
Giga Sage
Giga Sage

Hello @mounika32 

 

You can write a Client Script for the Same :-

 

suppose your multi-line field is string :-

 

function onLoad(){

var desc = g_form.getValue('description');
var arr = desc.split('\n');
var count=0;
for(var i=0;i<arr.length;i++){
count++:
}

alert("Number of Lines are" +count);



}

 

 

Plz mark my solution as Accept, If you find it helpful.

 

Regards,

Samaksh

Danish Bhairag2
Tera Sage
Tera Sage

Hi @mounika32 ,

 

// Replace 'your_table_name' and 'your_multi_line_text_field' with your actual table name and field name

var tableName = 'your_table_name';

var fieldName = 'your_multi_line_text_field';

// Get the record with the desired multi-line text field

var gr = new GlideRecord(tableName);

gr.addQuery('sys_id', 'your_record_sys_id'); // Replace with the actual sys_id of your record

gr.query();

if (gr.next()) {

    // Get the multi-line text field value

    var multiLineText = gr.getValue(fieldName);

    // Split the text into an array of lines

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

    // Get the count of lines

    var linesCount = linesArray.length;

    gs.info('Number of lines in ' + fieldName + ': ' + linesCount);

} else {

    gs.error('Record not found');

}

 

This script assumes you have the sys_id of the record you're interested in, and it fetches the multi-line text field's value using getValue. It then splits the text using the newline character (\n) and calculates the count of lines.

 

Adjust the script according to your specific use case and field names.

 

Thanks,

Danish

 

hi @Danish Bhairag2 , I have tried as below through back ground script but its not working

mounika32_1-1701928190278.png

 

 

@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