question on incident form impact choices

Mani60
Tera Contributor
Hi All,

i have one requirement, if user's department is 'IES' then he should not see impact choices and if user's department is not 'IES' then he should see impact choices. So for this I have written one onchange client script, but this client script not work properly, Select any user, it is going in the else condition only if condition not working in this script So can anyone help me on this where is the mistake on my code. 
Please find the below code :

function
onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
   
    var user = new GlideRecord('sys_user');
    user.addQuery('caller_id.department', 'LIKE', 'IES');
    user.query();
   
    if (user.next()) {
       
        alert(user.getValue('name')); 
        alert(user.getValue('department'));
        g_form.removeOption('impact', '7');
        g_form.removeOption('impact', '5');
        g_form.removeOption('impact', '6');
    } else {
       
        alert("Department is not 'IES'");
        g_form.addOption('impact', '5','Entire');
        g_form.addOption('impact', '6','Team');
        g_form.addOption('impact', '7','Single');
       
    }
}
3 REPLIES 3

Amitoj Wadhera
Kilo Sage

Hi @Mani60 ,

 

Corrected Script:

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

    // Get the department of the current user
    var user = g_user.userID;
    var userGR = new GlideRecord('sys_user');
    if (userGR.get(user)) {
        var department = userGR.getValue('department');
        var deptGR = new GlideRecord('cmn_department');
        if (deptGR.get(department)) {
            var deptName = deptGR.getValue('name');
            
            if (deptName === 'IES') {
                alert(userGR.getValue('name')); 
                alert(deptName);
                g_form.removeOption('impact', '7');
                g_form.removeOption('impact', '5');
                g_form.removeOption('impact', '6');
            } else {
                alert("Department is not 'IES'");
                g_form.addOption('impact', '5', 'Entire');
                g_form.addOption('impact', '6', 'Team');
                g_form.addOption('impact', '7', 'Single');
            }
        }
    }
}

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera

dgarad
Giga Sage

Hi @Mani60 

refer to the below link.

https://www.servicenow.com/community/hrsd-forum/how-to-hide-certain-choices-based-on-logged-in-users...

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

HrishabhKumar
Kilo Sage

Hi @Mani60 ,

You can achieve this using getReference function. I have added the demo code, you can modify it according to your requirement. I have left placeholders for you to fill in the details like "sys_is of IES department" and in if and else condition.

 

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

    g_form.getReference('caller_id',myfunc);
	function myfunc(val){
		if(val.department == 'sys_id of IES Department'){
			g_form.addInfoMessage('in if');
			//Add you if logic here
		}
		else{
			g_form.addInfoMessage('in else');
			//Add you else logic here
		}
	}
    

}

 

 

Hope this helps you.