script is not working

chandan86patra
Tera Contributor

Hi All, 

This script is not working properly , if the last name is dot "." then only first name and middle name should display and  if the last name is not dot than first name and last name should display .output is not display properly 

 

Case1. rakesh Kumar

Case rakesh .

 

Script 

var user_name = 'chandan kumar';
var short_name;
var gr = new GlideRecord('sys_user');
gr.addQuery('name', user_name);
gr.query();

if (gr.next()) {
    // Check if the last_name is missing, a dot, or contains only special characters
    if ( gr.last_name == '.' ) {
        short_name = gr.first_name + ' ' + gr.middle_name; // Combine first and middle names
    } else {
        short_name = gr.first_name + ' ' + gr.last_name; // Combine first and last names
    }
    gs.print(short_name);
}
3 ACCEPTED SOLUTIONS

Juhi Poddar
Kilo Patron

Hello @chandan86patra 

Here is script to meet your requirement:

var user_name = 'Peter .';
var short_name = '';
var gr = new GlideRecord('sys_user');
gr.addQuery('name', user_name);
gr.query();

if (gr.next()) {
    // Regular expression to check if last_name contains only alphabetic characters
    var alphabeticRegex = /^[a-zA-Z]+$/;

    if (!gr.last_name || !alphabeticRegex.test(gr.last_name)) {
        short_name = gr.first_name;
        if (gr.middle_name) {
            short_name += ' ' + gr.middle_name; // Add middle name if available
        }
    } else {
        short_name = gr.first_name + ' ' + gr.last_name; // Use first and last name
    }
    gs.print(short_name);
}

Result:

JuhiPoddar_0-1736246120784.png

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

@chandan86patra 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

Harish Bainsla
Kilo Patron
Kilo Patron

Hi @chandan86patra check below script I test in pdi

Screenshot 2025-01-08 at 11.28.17 AM.png

if my answer helps you mark helpful and accept solution

View solution in original post

4 REPLIES 4

Juhi Poddar
Kilo Patron

Hello @chandan86patra 

Here is script to meet your requirement:

var user_name = 'Peter .';
var short_name = '';
var gr = new GlideRecord('sys_user');
gr.addQuery('name', user_name);
gr.query();

if (gr.next()) {
    // Regular expression to check if last_name contains only alphabetic characters
    var alphabeticRegex = /^[a-zA-Z]+$/;

    if (!gr.last_name || !alphabeticRegex.test(gr.last_name)) {
        short_name = gr.first_name;
        if (gr.middle_name) {
            short_name += ' ' + gr.middle_name; // Add middle name if available
        }
    } else {
        short_name = gr.first_name + ' ' + gr.last_name; // Use first and last name
    }
    gs.print(short_name);
}

Result:

JuhiPoddar_0-1736246120784.png

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Ankur Bawiskar
Tera Patron
Tera Patron

@chandan86patra 

try this

var user_name = 'chandan kumar';
var short_name;
var gr = new GlideRecord('sys_user');
gr.addQuery('name', user_name);
gr.query();

if (gr.next()) {
    // Check if the last_name is a dot
    if (gr.last_name == '.') {
        short_name = gr.first_name + ' ' + (gr.middle_name ? gr.middle_name : ''); // Combine first and middle names if middle name exists
    } else {
        short_name = gr.first_name + ' ' + gr.last_name; // Combine first and last names
    }
    gs.print(short_name);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@chandan86patra 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Harish Bainsla
Kilo Patron
Kilo Patron

Hi @chandan86patra check below script I test in pdi

Screenshot 2025-01-08 at 11.28.17 AM.png

if my answer helps you mark helpful and accept solution