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

@Kiet1 

 

Mistake1: You haven't selected the field name in your onChange Client script, select field name as "requested_for" and try again please, if it will not work you can go ahead with Alka's solution

@Prince Arora I don't have the option "requested_for"

Hello @Kiet1 ,

Please try this solution.

 

I've tried to create the catalog client script but the dropdown for Variable name is empty 

@Kiet1 , change the table to sc_req_item i.e requested item table. Then you can see the field.