how to autopopulate first name and last name when caller changed in incident form through script inc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 03:31 AM
How to auto populate first name, and last name, when caller changed in incident form through script include in ServiceNow?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 06:26 AM
If the First name and Last name fields don't need to be editable on the incident form, then you could dot walk out the first and last name fields from the caller (which is a reference to the sys_user table) and those would show the data for first and last name of the caller without scripting.
To do this, go to the incident form, right click the header bar, then click configure, then form layout
Find Caller and select the tree button in the middle at the top of the three buttons to go into the caller reference
Find first name and last name on the left side, and click the arrow to put them on the right side.
Put them where you want to show them on the right side, next to other fields, like under Caller for example.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 06:34 AM - edited 11-13-2024 06:36 AM
Hi @MohanDegala
First create onChange client script as below
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) return;
if (newValue) {
var ga = new GlideAjax('CustomCatalogEmail');
ga.addParam('sysparm_name', 'getName');
ga.addParam('sysparm_user_id', g_form.getValue('beneficiary2'));
ga.getXMLAnswer(function(answer) {
if (answer) {
var nameObj = JSON.parse(answer); // Parse JSON response
g_form.setValue('first_name', nameObj.first_name); // Set first name
g_form.setValue('last_name', nameObj.last_name); // Set last name
g_form.setReadOnly('first_name', true); // Make fields read-only
g_form.setReadOnly('last_name', true);
} else {
g_form.clearValue('first_name');
g_form.clearValue('last_name');
}
});
} else {
g_form.clearValue('first_name');
g_form.clearValue('last_name');
}
}
Script include -
Name - CustomCatalogEmail
var CustomCatalogEmail = Class.create();
CustomCatalogEmail.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getName: function() {
var retObj = {};
var userId = this.getParameter('sysparm_user_id');
var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", userId);
gr.query();
if (gr.next()) {
retObj = {
first_name: gr.getValue("first_name"),
last_name: gr.getValue("last_name")
};
}
return JSON.stringify(retObj); // Return as JSON string
},
type: 'CustomCatalogEmail'
});
Please Mark ✅ Correct & 👍 Helpful, if applicable.