sc_task Business rule to move non agent comments from pending to work in progress.

RBlor
Mega Guru

Hi,
I seem to be running into issues with trying to set up a business rule for sc_task. We want any non-agent replies to the sc_tasks  to move the state from pending to work in progress.  We only want this to work for 3 assignment groups. Another factor is to not work when moveworks updates.  I have the below script but it seems to not work when I comment to a ticket it seems to move the state back to work in progress.  Any tips on the script would be appreciated. thank you. 

// Before Update on sc_task

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

    // Roles considered "agents" whose replies should NOT move Pending -> WIP
    var agentRoles = ['itil', 'hr', 'facilities_inc'];

    // Check if current user has any of those roles
    var isAgent = agentRoles.some(function (r) { return gs.hasRole(r); });

    // Only move Pending -> WIP when:
    // - Additional comments updated
    // - State NOT manually changed in this update
    // - Was Pending and is still Pending
    // - Update not from MoveWorks
    // - User is NOT an agent (itil, hr, facilities_inc)
    // - Assignment group is one of the allowed groups
    if (
        current.comments.changes() &&      
        !current.state.changes() &&       
        previous.state == 5 &&            
        current.state == 5 &&             
        current.sys_updated_by != 'MoveWorks' &&
        !isAgent &&                       
        (
            current.assignment_group == 'ba0fd3c7db78e11053403978f4961920' || // Service Delivery Optimization
            current.assignment_group == 'd625dccec0a8016700a222a0f7900d06' || // IT Service Desk
            current.assignment_group == 'c75eb931dbab5700f4ee7b5b8c961971'    // Desktop Support
        )
    ) {
        current.state = 2; // Work in Progress
    }

})(current, previous);

 

1 REPLY 1

RBlor
Mega Guru

gpt cleaned it up a little but I still run into the same issue: 

// Before Update on sc_task

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

    // --- Act only on comment-only updates while Pending (5) ---
    if (!current.comments.changes()) return;               // No Additional Comments change
    if (current.state.changes()) return;                   // State was manually changed in this update
    if (previous.state != 5 || current.state != 5) return; // Was and is Pending

    // --- Only for these assignment groups (by sys_id) ---
    var allowedGroups = [
        'ba0fd3c7db78e11053403978f4961920', // Service Delivery Optimization
        'd625dccec0a8016700a222a0f7900d06', // IT Service Desk
        'c75eb931dbab5700f4ee7b5b8c961971'  // Desktop Support
    ];
    if (allowedGroups.indexOf(String(current.assignment_group)) === -1) return;

    // --- Updater checks (use session user in Before rules) ---
    var userName = gs.getUserName() || '';

    // If updated by MoveWorks, do NOT flip
    if (userName === 'MoveWorks') return;

    // If updater is an agent, do NOT flip
    var isAgent = gs.hasRole('itil') || gs.hasRole('hr') || gs.hasRole('facilities_inc');
    if (isAgent) return;

    // --- Otherwise flip Pending -> Work in Progress (2) ---
    current.state = 2;

})(current, previous);