- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2024 08:29 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2024 08:33 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2024 08:33 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2024 08:55 AM - edited 03-10-2024 08:59 AM
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
Regards,
Shyamkumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2024 09:02 AM
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' });
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); }
If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!
Thanks & Regards,
Sumanth Meda