- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 10:28 AM
Hi,
I need help with auto populating the department field on the sc_task form.
I've created a new departmeent field (u_department_task) but would like it to be pulling the data from the Requested For field (requested_for)
Client script
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 12:49 PM
Hello @Kiet1 ,
Please Replace your script with below script:-
Client Script:-
function onLoad() {
//Type appropriate comment here, and begin script below
var req= g_form.getValue('request_item');
var ga = new GlideAjax('departmentDetail');
ga.addParam('sysparm_name', 'getDetails');
ga.addParam('sysparm_req', req); //newValue will requester for
ga.getXML(callback);
function callback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_department_task', answer); // enter correct variable name here
}
}
Script include:-
var departmentDetail = Class.create();
departmentDetail.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDetails: function() {
var req = this.getParameter('sysparm_req');
var gr = new GlideRecord('sc_req_item');
gr.addQuery('sys_id', req);
gr.query();
if (gr.next()) {
return gr.request.requested_for.department.toString();
}
},
type: 'departmentDetail'
});
Please Mark my answers Helpful & Accepted if I have answered your questions.
Thanks,
Alka
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 10:32 AM - edited 10-20-2023 10:35 AM
Hello @Kiet1 ,
You can use onChange client script, when 'requested for' variables changes then auto populate the department.
You can use below script:-
g_form.setValue("u_department_task", g_form.getReference("requested_for").department);
Please Mark my answers Helpful & Accepted if I have answered your questions.
Thanks,
Alka
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 11:02 AM
Hi Alka,
I’ve tried this and it has not worked
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 11:12 AM - edited 10-20-2023 11:14 AM
Hello @Kiet1 ,
You can use Ajax call to get the Department.
You need to create script include and call it in client script using ajax call.
Please refer to below script:-
Onchange Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('departmentDetail');
ga.addParam('sysparm_name', 'getDetails');
ga.addParam('sysparm_req_for', newValue); //newValue will requester for
ga.getXML(callback);
function callback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_department_task', answer); // enter correct variable name here
}
}
Create a script include with name 'departmentDetail' and make it client callable. Add the below script in script :-
var departmentDetail = Class.create();
departmentDetail.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDetails: function() {
var req_user = this.getParameter('sysparm_req_for');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', req_user);
gr.query();
if (gr.next()) {
return gr.department.toString();
}
},
type: 'departmentDetail'
});
Please refer to below screenshots:-
Please Mark my answers Helpful & Accepted if I have answered your questions.
Thanks,
Alka
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 11:26 AM