Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Get the mobile number and location of the caller details on my incident form.

dhineshkumar
Tera Guru

Hi Team 

I have the requirement on my incident form. When I select the caller it should fetch the mobile number (string) and location (reference) fields on my incident form using script include and glideajax method, How to achieve this task ?

 

Thank you.

1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@dhineshkumar 

You can use below client and script include to achieve your requirement, just cross check correct field name in the code

Client script: 

 

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

    var callerID = g_form.getValue('caller_id'); // Assuming the field name is "caller_id"
    var ga = new GlideAjax('CallerInformationHelper');
    ga.addParam('sysparm_name', 'getCallerInformation');
    ga.addParam('callerID', callerID);
    ga.getXMLAnswer(function(response) {
        var info = JSON.parse(response);
        if (info) {
            g_form.setValue('mobile_phone', info.mobileNumber); // Set the mobile number field
            g_form.setValue('location', info.location); // Set the location field
        }
    });
}

 

Script Include:

 

var CallerInformationHelper = Class.create();
CallerInformationHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getCallerInformation: function(callerID) {
        var result = {};
        var grCaller = new GlideRecord('sys_user');
        if (grCaller.get(callerID)) {
            result.mobileNumber = grCaller.mobile_phone.toString();
            result.location = grCaller.location.toString(); // Assuming location is a reference field
        }
        return result;
    },

    type: 'CallerInformationHelper'
});

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

 

 

View solution in original post

3 REPLIES 3

Maddysunil
Kilo Sage

@dhineshkumar 

You can use below client and script include to achieve your requirement, just cross check correct field name in the code

Client script: 

 

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

    var callerID = g_form.getValue('caller_id'); // Assuming the field name is "caller_id"
    var ga = new GlideAjax('CallerInformationHelper');
    ga.addParam('sysparm_name', 'getCallerInformation');
    ga.addParam('callerID', callerID);
    ga.getXMLAnswer(function(response) {
        var info = JSON.parse(response);
        if (info) {
            g_form.setValue('mobile_phone', info.mobileNumber); // Set the mobile number field
            g_form.setValue('location', info.location); // Set the location field
        }
    });
}

 

Script Include:

 

var CallerInformationHelper = Class.create();
CallerInformationHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getCallerInformation: function(callerID) {
        var result = {};
        var grCaller = new GlideRecord('sys_user');
        if (grCaller.get(callerID)) {
            result.mobileNumber = grCaller.mobile_phone.toString();
            result.location = grCaller.location.toString(); // Assuming location is a reference field
        }
        return result;
    },

    type: 'CallerInformationHelper'
});

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

 

 

shyamkumar VK
Kilo Patron

@dhineshkumar ,

You can do this using Client Script using get Reference with Callback Function 

On change Client Script - Field On change of Caller

 

var caller = g_form.getReference('caller_id', callBackFunc);
}
function callBackFunc(caller)
{

g_form.setValue('mobile_phone',caller.mobile_phone);
g_form.setValue('location',caller.location);
}

 

Approach 2 :

In the Default Value of Location & Phone Number Fields add this Below lines 

 

Location :

 

javascript:gs.getUser().getLocation();

 

 

Phone Number 

 

javascript:gs.getUser().getmobile_phone(); // Replace the field name 

 

 

Regards,

Shyamkumar

 

Please mark this as helpful and accept as a solution if this resolves your Ask.
Regards,

Shyamkumar

Sumanth16
Kilo Patron

Hi @dhineshkumar ,

 

 

1.Script Include Creation

Name: userDetailsUtil

API Name: global.userDetailsUtil (Auto populated by system)

Client Callable: Checked(True)

Active: Checked(True)

var userDetailsUtil = Class.create();
userDetailsUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getEmployeeDetails: function() {
        var userName = this.getParameter('sysparm_user');
        var user = new GlideRecord('sys_user');
        var result = { //Create an object to store the User data
            firstName: "",
            lastName: "",
            userID: "",
            location: "",
            manager: ""
        };
        if (user.get(userName)) {
            result.firstName = user.first_name.toString(); // toString is different from getDisplayValue
            result.lastName = user.last_name.toString(); // toString is different from getDisplayValue
            result.userID = user.user_name.toString(); // toString is different from getDisplayValue
            result.location = user.location.getDisplayValue(); //use getDisplayValue() value for reference fields
            result.manager = user.manager.getDisplayValue(); //use getDisplayValue() value for reference fields
        }
        return JSON.stringify(result);

    },
    type: 'userDetailsUtil'
});

Sumanth16_0-1710086528884.png

 

2.Catalog Client Script creation

Name: Return User Details

Active: Checked (True)

UI Type: ALL

Type: OnChange

Variable Name: Logged in user Variable.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	var user_id = g_form.getValue('requested_for');
	var ga = new GlideAjax('userDetailsUtil');//name of script include
	ga.addParam('sysparm_name', 'getEmployeeDetails');//name of function on script include
	ga.addParam('sysparm_user', user_id);//name of field on form triggering call
	ga.getXML(EmployeeDetailsLookup); // Always try to use asynchronous call
}

// Callback function to process the response returned from the server
function EmployeeDetailsLookup(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	var result = JSON.parse(answer);
	g_form.setValue('first_name',result.firstName);
	g_form.setValue('last_name',result.lastName);
	g_form.setValue('user_name',result.userID);
	g_form.setValue('location',result.location);
	g_form.setValue('manager',result.manager);
}

Sumanth16_1-1710086528816.png

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda