- 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 12:02 PM
@Alka_Chaudhary it dosen't give me the option to change the table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 12:05 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 12:01 PM - edited 10-20-2023 12:02 PM
@Kiet1 , change the client script table to sc_req_item table i.e requested item table and in field name select the requested for and use this script. It will work.
g_form.setValue("u_department_task", g_form.getReference("requested_for").department);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 11:13 AM
Could you please try same script in onChange client script instead of onLoad as while loading the form "Requested for" field must be empty which results the empty department
-> Create an onChange Script
-> Copy paste same code
If my answer solved your issue, please mark my answer as ✅ Correct & 👍Helpful based on the Impact.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 11:52 AM
@Kiet1 Requested for is a dot walked field from Requested Item (sc_req_item) table hence you need to update your script as follows.
function onLoad() {
//Type appropriate comment here, and begin script below
var user = g_form.getReference('request_item.requested_for', callBack);
function callBack(user) {
g_form.setValue('u_department_task', user.department);
}
}
I have tested this on my PDI and it works perfectly.
Please mark my answer correct and helpful if it manages to address your issue.