show the Caller details in the following format on Description First Name, Last Name, Deportment.

Subhashini C
Tera Contributor
 
1 REPLY 1

HrishabhKumar
Kilo Sage

Hi @Subhashini C ,

If you want to populate caller First Name, Last Name, Department in the description section. Then you can use getReference, g_scratchpad and GlideAjax. The best and the ServiceNow recommended way is to use GlideAjax.

I've provided you the steps and script for the particular solution:

Step 1: Create the Script Include

  1. Navigate to System Definition > Script Includes.
  2. Click on New to create a new Script Include.
  3. Fill in the details and add the script as follows:

Name: CallerDetails

API Name: CallerDetails

Client Callable: Checked

 

 

var CallerDetails = Class.create();
CallerDetails.prototype = {
    initialize: function() {},

    getCallerDetails: function(callerId) {
        var result = {};
        
        if (!callerId) {
            return result;
        }
        
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(callerId)) {
            result.firstName = userGR.getValue('first_name');
            result.lastName = userGR.getValue('last_name');
            var departmentId = userGR.getValue('department');

            var deptGR = new GlideRecord('cmn_department');
            if (deptGR.get(departmentId)) {
                result.department = deptGR.getValue('name');
            } else {
                result.department = '';
            }
        }

        return result;
    },

    type: 'CallerDetails'
};

 

 

 

Step 2: Create the onLoad Client Script

  1. Navigate to System Definition > Client Scripts.
  2. Click on New to create a new Client Script.
  3. Fill in the details and add the script as follows:

Name: Populate Caller Details in Description

Table: Incident

Type: onLoad

UI Type: All

 

 

(function() {
    // Ensure the script only runs on new records
    if (g_form.isNewRecord()) {
        var callerId = g_form.getValue('caller_id');

        if (callerId) {
            // Call the Script Include to get caller details
            var ga = new GlideAjax('CallerDetails');
            ga.addParam('sysparm_name', 'getCallerDetails');
            ga.addParam('callerId', callerId);
            ga.getXMLAnswer(function(response) {
                var answer = response.responseXML.documentElement.getAttribute("answer");
                if (answer) {
                    var details = JSON.parse(answer);
                    var description = details.firstName + ' ' + details.lastName + ', ' + details.department;
                    g_form.setValue('description', description);
                }
            });
        }
    }
})();

 

 

 

Thanks,

Hope this helps.

If my response proves helpful please mark it helpful and accept it as solution to close this thread.