Need Help on code

Harshal Patil1
Tera Contributor

Problem ticket cannot be closed if there are open Incident Tasks anyone please help with coding.

1 ACCEPTED SOLUTION

Martin Friedel
Mega Sage

Hello, I am not sure if the requirement makes sense. There is a relation between Problem record and Incident record (via problem_id field on Incident). However here is the solution for your question:

 

  1. Create Business Rule on Problem table
    • Advanced: True
    • When: before
    • Run on: Update
    • Filter Conditions: State is Closed
      BR_problem.JPG

  2. In the Advanced tab, paste this code:

 

 

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

    var problemSysID = current.getUniqueValue();
    var incidentArr = [];
    var incTaskArr = [];

    // Get active Incident records in relation with current Problem record
    var incidentGR = new GlideRecord('incident');
    incidentGR.addQuery('problem_id', problemSysID);
    incidentGR.addActiveQuery();
    incidentGR.query();

    while (incidentGR.next()) {
        incidentArr.push(incidentGR.getUniqueValue());
    }

    if (incidentArr.length === 0) {
        // No related active Incident record found, no need to check for Incident Tasks
        return;
    }

    // Get active Incident tasks of found Incident records
    var incTaskGR = new GlideRecord('incident_task');
    incTaskGR.addQuery('incident', 'IN', incidentArr);
    incTaskGR.addActiveQuery();
    incTaskGR.query();

    while (incTaskGR.next()) {
        incTaskArr.push(incTaskGR.getValue('number'));
    }

    // If there is an active Incident Task, abort action
    if (incTaskArr.length > 0) {
        current.setAbortAction(true);
        gs.addErrorMessage('Problem task still has an active incident task: ' + incTaskArr.toString());
    }

})(current, previous);​

 

 

 

Modify the code as you need. For example you can change the error message to include clickable link to which redirects user to the Incident Task record.

 

If my answer helped you, please mark it as correct and helpful, thank you 👍
Martin

View solution in original post

2 REPLIES 2

Martin96
Tera Contributor

Hello, I am not sure if the requirement makes a sense. There is a relation between Problem record and Incident record (via field 'problem_id' on Incident). However here is the solution:

  1. Create a Business Rule on Problem table [problem]
    • Advanced: True
    • When: before
    • Run on Update
    • Filter Conditions: State is Closed
      BR_problem.JPG

  2. Add below code to Advanced field

 

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

    var problemSysID = current.getUniqueValue();
    var incidentArr = [];
    var incTaskArr = [];

    // Get active Incident records in relation with current Problem record
    var incidentGR = new GlideRecord('incident');
    incidentGR.addQuery('problem_id', problemSysID);
    incidentGR.addActiveQuery();
    incidentGR.query();

    while (incidentGR.next()) {
        incidentArr.push(incidentGR.getUniqueValue());
    }

    if (incidentArr.length === 0) {
        // No related active Incident record found, no need to check for Incident Tasks
        return;
    }

    // Get active Incident tasks of found Incident records
    var incTaskGR = new GlideRecord('incident_task');
    incTaskGR.addQuery('incident', 'IN', incidentArr);
    incTaskGR.addActiveQuery();
    incTaskGR.query();

    while (incTaskGR.next()) {
        incTaskArr.push(incTaskGR.getValue('number'));
    }

    // If there is an active Incident Task, abort action
    if (incTaskArr.length > 0) {
        current.setAbortAction(true);
        gs.addErrorMessage('Problem task still has an active incident task: ' + incTaskArr.toString());
    }

})(current, previous);​

 

Modify code as you need, especially error message (for example you can enhance it by providing clickable link that redirects user to the record).

 

Hope it helps 👍
Martin

Martin Friedel
Mega Sage

Hello, I am not sure if the requirement makes sense. There is a relation between Problem record and Incident record (via problem_id field on Incident). However here is the solution for your question:

 

  1. Create Business Rule on Problem table
    • Advanced: True
    • When: before
    • Run on: Update
    • Filter Conditions: State is Closed
      BR_problem.JPG

  2. In the Advanced tab, paste this code:

 

 

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

    var problemSysID = current.getUniqueValue();
    var incidentArr = [];
    var incTaskArr = [];

    // Get active Incident records in relation with current Problem record
    var incidentGR = new GlideRecord('incident');
    incidentGR.addQuery('problem_id', problemSysID);
    incidentGR.addActiveQuery();
    incidentGR.query();

    while (incidentGR.next()) {
        incidentArr.push(incidentGR.getUniqueValue());
    }

    if (incidentArr.length === 0) {
        // No related active Incident record found, no need to check for Incident Tasks
        return;
    }

    // Get active Incident tasks of found Incident records
    var incTaskGR = new GlideRecord('incident_task');
    incTaskGR.addQuery('incident', 'IN', incidentArr);
    incTaskGR.addActiveQuery();
    incTaskGR.query();

    while (incTaskGR.next()) {
        incTaskArr.push(incTaskGR.getValue('number'));
    }

    // If there is an active Incident Task, abort action
    if (incTaskArr.length > 0) {
        current.setAbortAction(true);
        gs.addErrorMessage('Problem task still has an active incident task: ' + incTaskArr.toString());
    }

})(current, previous);​

 

 

 

Modify the code as you need. For example you can change the error message to include clickable link to which redirects user to the Incident Task record.

 

If my answer helped you, please mark it as correct and helpful, thank you 👍
Martin