The CreatorCon Call for Content is officially open! Get started here.

Need to Print the FirstName and Last Name in Short Description

sayanghosh0
Tera Contributor

Hello Team,


I am trying to put the FirstName and LastName in Short Description when the Caller changes.
Script Include:

sayanghosh0_0-1737152526302.png

 


On Change Client Script:
sayanghosh0_1-1737152552965.png

 

Getting the Below Output:
sayanghosh0_0-1737152288240.png

 



Best Regards



1 ACCEPTED SOLUTION

Siddhesh Jadhav
Kilo Sage
Kilo Sage

Hi @sayanghosh0 ,

 

Suggested Solution:

Your current issue is caused by how you are handling the response from the Script Include. Since the Script Include returns an array (even if it contains just one object), you need to access the first index [0] of the array to retrieve the caller's information.

Here’s a step-by-step guide to resolve the issue:

 

  1. Update Your Client Script
    Modify the client script to properly handle the array returned from the Script Include:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}

	var glideAjax = new GlideAjax('IncidentUtils');
	glideAjax.addParam('sysparm_name', 'callerInfo');
	glideAjax.addParam('sysparm_caller', newValue);
	glideAjax.getXMLAnswer(pop);

	function pop(response) {
		if (response) {
			var answer = JSON.parse(response);
			alert('answer' + answer);
			var sd = 'First Name : ' + answer[0].firstName + ' Last Name : ' + answer[0].lastName;
			g_form.setValue('short_description', sd);
		} else {
			alert('error');
		}
	}
}

 

  • Understanding the Script Include
    Since your Script Include is returning an array, even if it contains only one object, ensure the client script processes the array correctly.

  • Recommendation
    In my opinion, there is no need to return an array from the Script Include when you are dealing with just one object. Instead, you can directly return the object.

 

var IncidentUtils = Class.create();
IncidentUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    callerInfo: function () {
        var callerSysId = this.getParameter('sysparm_caller');
        var userRecord = new GlideRecord('sys_user');
        userRecord.addQuery('sys_id', callerSysId);
        userRecord.query();

        if (userRecord.next()) {
            var userObj = {
                firstName: userRecord.first_name.toString(),
                lastName: userRecord.last_name.toString()
            };
            return JSON.stringify(userObj); // Return an object instead of an array
        }
        return null;
    },
    type: 'IncidentUtils'
});

 

 

If this answer helps you solve your query, please mark it as accepted and helpful.

 

Best Regards,
Siddhesh Jadhav

View solution in original post

7 REPLIES 7

Siddhesh Jadhav
Kilo Sage
Kilo Sage

Hi @sayanghosh0 ,

 

Suggested Solution:

Your current issue is caused by how you are handling the response from the Script Include. Since the Script Include returns an array (even if it contains just one object), you need to access the first index [0] of the array to retrieve the caller's information.

Here’s a step-by-step guide to resolve the issue:

 

  1. Update Your Client Script
    Modify the client script to properly handle the array returned from the Script Include:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}

	var glideAjax = new GlideAjax('IncidentUtils');
	glideAjax.addParam('sysparm_name', 'callerInfo');
	glideAjax.addParam('sysparm_caller', newValue);
	glideAjax.getXMLAnswer(pop);

	function pop(response) {
		if (response) {
			var answer = JSON.parse(response);
			alert('answer' + answer);
			var sd = 'First Name : ' + answer[0].firstName + ' Last Name : ' + answer[0].lastName;
			g_form.setValue('short_description', sd);
		} else {
			alert('error');
		}
	}
}

 

  • Understanding the Script Include
    Since your Script Include is returning an array, even if it contains only one object, ensure the client script processes the array correctly.

  • Recommendation
    In my opinion, there is no need to return an array from the Script Include when you are dealing with just one object. Instead, you can directly return the object.

 

var IncidentUtils = Class.create();
IncidentUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    callerInfo: function () {
        var callerSysId = this.getParameter('sysparm_caller');
        var userRecord = new GlideRecord('sys_user');
        userRecord.addQuery('sys_id', callerSysId);
        userRecord.query();

        if (userRecord.next()) {
            var userObj = {
                firstName: userRecord.first_name.toString(),
                lastName: userRecord.last_name.toString()
            };
            return JSON.stringify(userObj); // Return an object instead of an array
        }
        return null;
    },
    type: 'IncidentUtils'
});

 

 

If this answer helps you solve your query, please mark it as accepted and helpful.

 

Best Regards,
Siddhesh Jadhav

@Siddhesh Jadhav 

Thanks , it works!! 😃 

Ankur Bawiskar
Tera Patron
Tera Patron

@sayanghosh0 

 no need of GlideAjax here

you can also use getReference() with callback method

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading) {
		return;
	}

	if(newValue == ''){
		g_form.clearValue('short_description');
	}

	var ref = g_form.getReference('caller_id', callBackMethod);
}

function callBackMethod(ref){
	if(ref.first_name && ref.last_name)
		g_form.setValue('short_description', 'First name is ' + ref.first_name + ' Last name is ' + ref.last_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