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
Mega Sage
Mega Sage

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

Hi Alka,

 

I’ve tried this and it has not worked

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:-

Alka_Chaudhary_1-1697825547295.png

 

Alka_Chaudhary_0-1697825499368.png

 

Please Mark my answers Helpful & Accepted if I have answered your questions.

Thanks,

Alka

 

Screenshot#4.pngScreenshot#3.pngScreenshot#2.pngScreenshot#1.png