- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 02:05 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 02:36 AM
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:
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 07:13 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 09:59 PM
Hi @chandan86patra check below script I test in pdi
if my answer helps you mark helpful and accept solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 02:36 AM
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:
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 04:45 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 07:13 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 09:59 PM
Hi @chandan86patra check below script I test in pdi
if my answer helps you mark helpful and accept solution