Itsm

Snowl13052025
Tera Contributor

I want a popup on the incident form if I try to resolve the incident while the associated alert is still open and help me the script and solution.

 

thanks in advance 

1 ACCEPTED SOLUTION

Bhimashankar H
Mega Sage

Hi @Snowl13052025 ,

 

I hope you saw my reply.

----------------------------------------------------------------------------------------
If my answer helps you or resolves your query, please consider marking as 'Accept As Solution'. So future readers with similar questions can find it easily. Your coordination will help community to grow stronger!.
----------------------------------------------------------------------------------------

Thank you.
Bhimashankar

View solution in original post

11 REPLIES 11

Weird
Mega Sage

So you want to use a client script to query em_alert table to see if the current incident is associated with an open alert.
To do this you have to work around the onSubmit process.
You see, when you click the submit button, you're triggering any onSubmit client scripts and those must return false in order to block submit.
You can call a GlideAjax script include, but since that runs in the background and your client script doesn't exactly wait for it, you end up finishing the submit before you get the answer from your script include and could decide whether to return true or false.

But there's a trick!

function onSubmit() {
    if (g_scratchpad.canSubmit){
        return true;
    }
    if(g_form.getValue('state') != '6'){
    return true;
    }
    var ga = new GlideAjax('<your_script_include>'); 
    ga.addParam('sysparm_name', '<your_function>'); 
    ga.addParam('sysparm_<your_parameter_name>', '<your_parameter>');
    ga.getXMLAnswer(handleReply);
    return false;


    function handleReply(answer) {
        if (answer == 'false') {
            g_form.alert('A related alert is still active.');
            return false;
        }
        var submitAction= g_form.getActionName();
        g_scratchpad.canSubmit= true;
        g_form.submit(submitAction);

    }
}

What this does is that it checks if the g_scratchpad has a value that tells it to submit the script.
If not, you'll be calling the script include and returning false. Your call will still however run and if it returns "false" you'll get your error popup. If it returns true it actually sets te g_scratchpad value as true and resubmits the form correctly.

Now, in your script include you'll need a few things. First make sure it's client callable.
Then create a function that queries the em_alert table. sysparm_name will be the name of the function and sysparm_<your_parameter_value_here> could be the sys_id of your incident. In your function you'll look for em_alert that has the sys_id on the inciden(?) field and if one is found, you'll return false. Otherwise return true and submit works.

The script include could look like this:

var incidentAlertUtil = Class.create();
incidentAlertUtil .prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkActiveAlert: function () {
        var answer = false;
        var incId= this.getParameter('sysparm_incident');
        var gr = new GlideRecord('em_alert');
        gr.addQuery('incident', incId);
        gr.addQuery('state', '<your active value>');
        gr.query();
        if (!gr.next()) {//if no match found
            answer = true;
        }
        return answer;
    },

    type: 'incidentAlertUtil '
});

 And then the client script would look like:

function onSubmit() {
    if (g_scratchpad.canSubmit){
        return true;
     }
    if(g_form.getValue('state') != '6'){
        return true;
    }
    var ga = new GlideAjax('incidentAlertUtil'); 
    ga.addParam('sysparm_name', 'checkActiveAlert'); 
    ga.addParam('sysparm_incident, g_form.getUniqueValue());
    ga.getXMLAnswer(handleReply);
    return false;


    function handleReply(answer) {
        if (answer == 'false') {
            g_form.alert('A related alert is still active.');
            return false;
        }
        var submitAction= g_form.getActionName();
        g_scratchpad.canSubmit= true;
        g_form.submit(submitAction);

    }
}


Note that I wrote this from somewhat hazy memory and without using having the actual script available, so there might be typos etc, but this should give the right idea on how to proceed.

Bhimashankar H
Mega Sage

Hi @Snowl13052025 ,

 

I hope you saw my reply.

----------------------------------------------------------------------------------------
If my answer helps you or resolves your query, please consider marking as 'Accept As Solution'. So future readers with similar questions can find it easily. Your coordination will help community to grow stronger!.
----------------------------------------------------------------------------------------

Thank you.
Bhimashankar