logo

NJP

Relating a Problem to a Closed Incident

Import · Oct 23, 2018 · article

Hey Everyone,

Something that I have come across a few times is the request to allow users to relate closed incidents to Problems (although it could be anything). OOB ACL rules stop users from updating any fields on the incident, and if you add the incident to the related list on the problem it doesn't work either. What can we do?

My solution was to create a UI action on the Incident form with a text box to enter the number of the problem you want to relate, and do it server-side. It might not be super elegant, but it works. Here's my code:

UI ActionName: "Relate to Problem"Form button: trueClient: trueOnclick: doConfirm();Condition: current.state == 7 && !current.problem_id;

Script:

function doConfirm() {
    //console.log("Poping up!");
    var gdw = new GlideDialogWindow('problemId');
    gdw.setTitle('Problem to Relate');
    gdw.render();
}

Requires role: ITIL

UI Page

Name: problemIdCategory: GeneralDescription: A small window for relating Problems to closed incidents. Calls "aaUtils" script include by Ajax to complete.

HTML:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
Please enter the Problem Number to relate:
    <form id="problemNumForm">
        <input type="text" name="problemNum"></input>
    </form>
        <button id="cancelpage" onclick="cancelPage();">Cancel</button>
        <button id="okpage" onclick="okPage();">Relate</button>
</j:jelly>

Client Script:

function okPage() {
    var probnum = document.getElementsByName("problemNum")[0].value;

    var ga = new GlideAjax('aaUtils');
    ga.addParam('sysparm_name', 'relateProblem');
    ga.addParam('sysparm_incid', g_form.getUniqueValue());
    ga.addParam('sysparm_probnum', probnum);
    ga.getXML(parse);

    function parse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        //console.log(answer);
        if (answer == "related") {
            g_form.addInfoMessage("Related to problem " + probnum);
            GlideDialogWindow.get().destroy();
        } else if (answer == "noprob") {
            g_form.addErrorMessage("Unable to relate - Problem Number incorrect. Confirm number and try again");
            GlideDialogWindow.get().destroy();
        } else if (answer == "noinc") {
            g_form.addErrorMessage("Unable to relate - Incident not found. Please raise with ServiceNow Admins");
            GlideDialogWindow.get().destroy();
        } else {
            g_form.addErrorMessage("Something went wrong. Please raise with ServiceNow Admins");
            GlideDialogWindow.get().destroy();
            console.log(answer);
        }

    }
}

function cancelPage() {
    GlideDialogWindow.get().destroy();
}

Script Include:Name: aaUtilsClient Callable: true

Script:

var aaUtils = Class.create();
aaUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    type: 'aaUtils',

    //Used on Incident to relate closed to problems, called from "Relate To Problem" UI Window
    relateProblem: function() {
        var prob = this.getParameter("sysparm_probnum");
        var inc = this.getParameter("sysparm_incid");
        var probid = '';

        var probGr = new GlideRecord("problem");
        probGr.addQuery("number", prob);
        probGr.query();
        if (probGr.next()) {
            probid = probGr.getUniqueValue();
        } else {
            return "noprob";
        }

        var gr = new GlideRecord("incident");
        gr.addQuery("sys_id", inc);
        gr.query();

        if (gr.next()) {
            gr.problem_id = probid;
            gr.update();    
            return "related";
        } else {
            return "noinc";
        }


    }
});

And that's it! Pretty easy. You could definitely expand it a bit further and add more error checking, or list the current open problems on the page in the Dialog window, but I was happy with quick and dirty.

Hope this Helps someone image

-Andrew

References:

AJAX Docs https://docs.servicenow.com/bundle/london-application-development/page/script/ajax/topic/p%5FAJAX.html

UI Window help https://community.servicenow.com/community?id=community_question&sys_id=98394fe1db5cdbc01dcaf3231f96...

Labels:

image

View original source

https://www.servicenow.com/community/itsm-articles/relating-a-problem-to-a-closed-incident/ta-p/2303325