Retrive requested_for department value to set a value on the ssp form

John1243
Tera Contributor

Hi All, 

 

On the form there is one multiple choice field with options 'yes'/'no'

if that is 'yes' then retrieve the requested_for - department value based on that department value need to populate a default value on a field 'Lic'.

 

tried onChange script not helpful.

 

function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;

}
var dept = g_form.getValue('requested_for.department.name');
var rf = g_form.getValu('is_this_for_a_rf_user');
if (dept == "Manfacturing – ANZ" && rf == "yes") {
g_form.setValue('lic', 'Development LIC');
alert('test');

}
}

 

Any suggestions.

7 REPLIES 7

Joe S1
Kilo Sage

Hi John,

 

You need to either do a getReference for the department or use a glideAjax and call a script include to get the value. Something like this:

	var rf = g_form.getValue('is_this_for_a_rf_user');
	var reqFor = g_form.getReference('requested_for', getDeptName);

	function getDeptName(reqFor){
		if (reqFor.department.name == 'Manfacturing – ANZ' && rf == 'yes'){
			g_form.setValue('lic', 'Development LIC');
		}
	}

Karan Chhabra6
Mega Sage
Mega Sage

Hi @John1243 ,

 

You need to use glideAjax and script include for this, if you use getReference(), it's not gonna work because you'll only be able to dot walk till level 1.

Please use this client script and script include below

Client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (newValue == '') {
        return;

    }

    var userGA = new GlideAjax('getUserDetails');
    userGA.addParam('sysparm_name', 'getDepartment');
    userGA.addParam('sysparm_user', g_form.getValue('requested_for'));
    userGA.getXMLAnswer(_handleResponse);

    function _handleResponse(answer) {

        var rf = g_form.getValue('is_tis_for_a_rf_user');
        if (answer == "Manfacturing – ANZ" && rf == "yes") {
            g_form.setValue('lic', 'Development LIC');


        }

    }
}

 

Script Include:

var getUserDetails = Class.create();
getUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getDepartment : function(){

var user = this.getParameter('sysparm_user');
var userGR = new GlideRecord('sys_user');
userGR.get(user);
var dept = userGR.department.name;
return dept;

},

    type: 'getUserDetails'
});

 

If my answer has helped with your question, please mark it as helpful and accepted solution.

 

Thanks,

Karan

hi @Karan Chhabra6 

 

the above scripts are not working in my case.

we are having a variable set in that we are having all the requested for related fields like manager, email, department , etc and we are auto populating all the details through script include & onchange script

we are not displaying all the fields on the ssp form only requested_for field will be displayed and remaining all fields will be only be displayed on the RITM.

 

so in my current requirement will this work of retrieving the department and adding condition to it.

 

Any suggestions.

@John1243  - the above script should work fine, i tested it on my PDI as well, please check the variable and choice backend names.