how to autopopulate first name and last name when caller changed in incident form through script inc

MohanDegala
Tera Contributor

How to auto populate first name, and last name, when caller changed in incident form through script include in ServiceNow?

2 REPLIES 2

Zach Koch
Giga Sage
Giga Sage

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

ZachKoch_0-1731507849106.png

Find Caller and select the tree button in the middle at the top of the three buttons to go into the caller reference

ZachKoch_1-1731507917435.png

Find first name and last name on the left side, and click the arrow to put them on the right side.

ZachKoch_2-1731507971685.png

Put them where you want to show them on the right side, next to other fields, like under Caller for example.

If this information helped resolve your issue, please remember to mark response correct and thumbs up to help future community members on this information, thanks!

Community Alums
Not applicable

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

 - checked

 

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.