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

Can you post your existing script include and onchange script? Sounds like you just need to make a minor update.

John1243
Tera Contributor

Hi @Joe S1 , please find the codes

 

Script include:

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

userDetailsArnotts: function() {
var id = this.getParameter('sysparm_id');
//var answer = [];
var gr = new GlideRecord('sys_user');
var aObj = {};
if (gr.get(id)) {
aObj.name = [{"sys_id": gr.getValue('sys_id'), "display_value" : gr.name.getDisplayValue()}];
aObj.location = [{"sys_id": gr.getValue('location'), "display_value" : gr.location.getDisplayValue()}];
aObj.employee_number = gr.employee_number.getDisplayValue();
aObj.department = [{"sys_id": gr.getValue('department'), "display_value" : gr.department.getDisplayValue()}];
aObj.first_name = gr.first_name.getDisplayValue();
//aObj.manager=gr.manager.getDisplayValue();
aObj.last_name = gr.last_name.getDisplayValue();
aObj.manager = [{"sys_id": gr.getValue('manager'), "display_value" : gr.manager.getDisplayValue()}];
aObj.mobile_phone = gr.mobile_phone.getDisplayValue();
aObj.email = gr.email.getDisplayValue();
aObj.user_name = gr.user_name.getDisplayValue();
//aObj.department = gr.department.getDisplayValue();
}
var output = JSON.stringify(aObj);
return output;
},

type: 'userData'

 

onChange Script:

 

function onChange(control, oldValue, newValue, isLoading) {
/*if (newValue == '') {
return;
}*/
if (newValue == '') {
g_form.setValue('user_id', '');
g_form.setValue('phone', '');
g_form.setValue('department', '');
g_form.setValue('location', '');
g_form.setValue('email_address', '');
g_form.setValue('manager', '');

return;
}
if (newValue != '') {
var ga1 = new GlideAjax('userData');
ga1.addParam('sysparm_name', 'userDetailsArnotts');
ga1.addParam('sysparm_id', newValue);
ga1.getXML(setValues);
}
}

function setValues(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
// alert(answer);
var Obj = JSON.parse(answer);

g_form.setValue('user_id', Obj.user_id);
g_form.setValue('phone', Obj.mobile_phone);

g_form.setValue('location', Obj.location[0].sys_id, Obj.location[0].display_value);
g_form.setValue('email_address', Obj.email);
g_form.setValue('manager', Obj.manager[0].sys_id,Obj.manager[0].display_value);

g_form.setValue('department', Obj.department[0].sys_id,Obj.department[0].display_value);

}

 

 

But this script is working on & written on variable set.

 

How this can be useful for conditions on variables/

 

Any Suggestions.

@John1243 , you can just create a new on change catalog client script which I mentioned above and use the same script include by adding the getDepartment function, something like this:

 

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

userDetailsArnotts: function() {
var id = this.getParameter('sysparm_id');
//var answer = [];
var gr = new GlideRecord('sys_user');
var aObj = {};
if (gr.get(id)) {
aObj.name = [{"sys_id": gr.getValue('sys_id'), "display_value" : gr.name.getDisplayValue()}];
aObj.location = [{"sys_id": gr.getValue('location'), "display_value" : gr.location.getDisplayValue()}];
aObj.employee_number = gr.employee_number.getDisplayValue();
aObj.department = [{"sys_id": gr.getValue('department'), "display_value" : gr.department.getDisplayValue()}];
aObj.first_name = gr.first_name.getDisplayValue();
//aObj.manager=gr.manager.getDisplayValue();
aObj.last_name = gr.last_name.getDisplayValue();
aObj.manager = [{"sys_id": gr.getValue('manager'), "display_value" : gr.manager.getDisplayValue()}];
aObj.mobile_phone = gr.mobile_phone.getDisplayValue();
aObj.email = gr.email.getDisplayValue();
aObj.user_name = gr.user_name.getDisplayValue();
//aObj.department = gr.department.getDisplayValue();
}
var output = JSON.stringify(aObj);
return output;
},

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: 'userData'
});