Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Cancel HR Task if the subject person of the parent case is not a manager

Simran321
Tera Expert

Hi There,

 

I have a lifecycle case that has a task that gets created. I need to cancel that HR task if the subject person for the Parent case is not a manager. How can I do this

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@Simran321 You need to write an onBefore insert business rule on your HR Task table as follows.

 

Screenshot 2024-01-23 at 1.27.59 PM.pngScreenshot 2024-01-23 at 1.28.50 PM.png

Here is the script for your reference.

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var glideUser = new GlideAggregate('sys_user');
    glideUser.addQuery('manager', gs.getUserID());
    glideUser.addAggregate('COUNT');
    glideUser.query();
    var managerCount = 0;
    if (glideUser.next()) {
        managerCount = glideUser.getAggregate('COUNT');
    }

    if (managerCount == 0) {
        current.setValue('state', '7'); //set the state to cancelled.
    }

})(current, previous);

 

Hope this helps.

Simran321
Tera Expert

@Sandeep Rajput - Thanks for your response. The HR Task is the child task of Lifecycle case. I need to find id parent case subject person is the manager. So cannot use gs.getuserID()

Here is the updated script for the subject person.

 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var glideUser = new GlideAggregate('sys_user');
    glideUser.addQuery('manager', current.parent.subject_person);
    glideUser.addAggregate('COUNT');
    glideUser.query();
    var managerCount = 0;
    if (glideUser.next()) {
        managerCount = glideUser.getAggregate('COUNT');
    }

    if (managerCount == 0) {
        current.setValue('state', '7'); //set the state to cancelled.
    }

})(current, previous);