Help! - I'm struggling to auto populate department field

Kiet1
Tera Contributor

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 

function onLoad() {
   //Type appropriate comment here, and begin script below
    var user = g_form.getReference('requested_for', callBack);

    function callBack(user) {
        g_form.setValue('u_department_task', user.department);
    }
   
}
1 ACCEPTED SOLUTION

Alka_Chaudhary
Mega Sage
Mega Sage

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

View solution in original post

21 REPLIES 21

@Alka_Chaudhary it dosen't give me the option to change the table

@Kiet1 change the table from sc_task to sc_req_item highlighted in red.

Alka_Chaudhary_0-1697828659658.png

 

@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);

Prince Arora
Tera Sage
Tera Sage

@Kiet1 

 

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.

Sandeep Rajput
Tera Patron
Tera Patron

@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.