Help with client script on HR case

geannaweed
Tera Contributor

I am hiding related lists and some sections and fields on an HR case based on a role.

I need to not hide one related list (HR task) if an HR task is assigned to that user who has that role, and has to be hidden if no HR task is assigned to the user.

 

my client script on HR case table

 

function onLoad() {

if (g_user.hasRoleExactly('role')){
var getHRService = g_form.getDisplayBox('hr_service').value;
if (getHRService == "Education" || getHRService == "Payroll"){
return;
}

if (getHRService != "Education" && getHRService != "Payroll") {
g_form.setSectionDisplay('action_section', false);
g_form.setReadOnly('work_notes', true);
g_form.hideRelatedList('sn_hr_core_case.u_parent_hr_case');

g_form.hideRelatedList('sn_hr_core_task.parent');
}
}
}

 

How do i query the condition where an HR task is assigned to that user with that role and show the related list.

1 ACCEPTED SOLUTION

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

Display BR is a great option as mentioned by Arav. Alternatively, you can create a client callable script include and use GlideAjax in your client script to do the query of tasks (don't do this on client side).

 

Something like this:
Script Include (my script include is called 'hasAssignedTask', make sure you change the name in the first row to match the name of yours)

var hasAssignedTask = Class.create();
hasAssignedTask.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    checkTasks: function() {
        var caseId = this.getParameter('sysparm_parent');
        var userId = this.getParameter('sysparm_user');
        
		var hrt = new GlideRecord('sn_hr_core_task');
        hrt.addEncodedQuery('parent=' + caseId + '^assigned_to=' + userId);
        hrt.setLimit(1);
        hrt.query();
        if(hrt.next()) {
            return true
        } else {
            return false
        }
    },
    type: 'hasAssignedTask'
});

 

Client Script with GlideAjax example (onLoad):

function onLoad() {
    var hasRole = g_user.hasRoleExactly('sn_hr_core.basic');
    var taskGa = new GlideAjax('hasAssignedTask');
    taskGa.addParam('sysparm_name', 'checkTasks');
    taskGa.addParam('sysparm_parent', g_form.getUniqueValue());
    taskGa.addParam('sysparm_user', g_user.userID);
    taskGa.getXMLAnswer(relatedListControl);


    function relatedListControl(answer) {
		var hasTask = JSON.parse(answer);
        if (!hasTask || !hasRole) {
            g_form.hideRelatedList('sn_hr_core_task.parent')
        }
    }
}

Make sure you have the correct related list ID in the hideRelatedList method. You can check this by right-clicking one of the columns of the related list and choosing Configure > List Control, then take the value from the 'Related list' field exactly as is.

 

If you have a lot of sections and related lists to show/hide, you might want to consider creating different Views for the different scenarios as well and applying View Rules to "force" a particular view on the user.

View solution in original post

3 REPLIES 3

Arav
Tera Guru
Tera Guru

Hi,

 

You may want to write a display BR on HR case that checks if the logged in user is assignee of any HR tasks under the HR case. Once done, set a scratchpad variable to either true or false.

 

Use the scratchpad value in the onLoad client script to hide / show the related list.

 

If you have never used Display BR / scratchpad before, you may want to read ServiceNow's documentation.

 

Thanks,

Arav

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

Display BR is a great option as mentioned by Arav. Alternatively, you can create a client callable script include and use GlideAjax in your client script to do the query of tasks (don't do this on client side).

 

Something like this:
Script Include (my script include is called 'hasAssignedTask', make sure you change the name in the first row to match the name of yours)

var hasAssignedTask = Class.create();
hasAssignedTask.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    checkTasks: function() {
        var caseId = this.getParameter('sysparm_parent');
        var userId = this.getParameter('sysparm_user');
        
		var hrt = new GlideRecord('sn_hr_core_task');
        hrt.addEncodedQuery('parent=' + caseId + '^assigned_to=' + userId);
        hrt.setLimit(1);
        hrt.query();
        if(hrt.next()) {
            return true
        } else {
            return false
        }
    },
    type: 'hasAssignedTask'
});

 

Client Script with GlideAjax example (onLoad):

function onLoad() {
    var hasRole = g_user.hasRoleExactly('sn_hr_core.basic');
    var taskGa = new GlideAjax('hasAssignedTask');
    taskGa.addParam('sysparm_name', 'checkTasks');
    taskGa.addParam('sysparm_parent', g_form.getUniqueValue());
    taskGa.addParam('sysparm_user', g_user.userID);
    taskGa.getXMLAnswer(relatedListControl);


    function relatedListControl(answer) {
		var hasTask = JSON.parse(answer);
        if (!hasTask || !hasRole) {
            g_form.hideRelatedList('sn_hr_core_task.parent')
        }
    }
}

Make sure you have the correct related list ID in the hideRelatedList method. You can check this by right-clicking one of the columns of the related list and choosing Configure > List Control, then take the value from the 'Related list' field exactly as is.

 

If you have a lot of sections and related lists to show/hide, you might want to consider creating different Views for the different scenarios as well and applying View Rules to "force" a particular view on the user.

Nilesh Wahule
Tera Guru

Hi @geannaweed , 

Can you please clarify few questions ,

 

1. When you say user is having a role...Does it mean logged in user ?

2. If logged in person is having certain role and one HR task is assigned to him ....Do we need to show all other tasks as well or only that task in which he is assigned?

3. What is exact use case for role here ?