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

Thanks Sandeep but it dosen't work 😞

@Kiet1 Did you clear the cache, sometime previous client scripts are cached and hence the new changes don't reflect use ctrl+R for hard refresh.

 

 

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

 

Let me know if you manage to see the alert.

This is what I see when I execute my script, it manages to fetch the department id of the requestor.

Screenshot 2023-10-21 at 12.34.59 AM.pngScreenshot 2023-10-21 at 12.35.11 AM.png

@Kiet1 Change you client script to on load and try the @Sandeep Rajput script. Then, your requirement will be fulfilled.

@Alka_Chaudhary Thanks for endorsing my reply. May be due to caching Kiet is not able to see changes made by my script.