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.

on submit client script

tushar_ghadage
Tera Contributor

Hi All, 

if some tries to resolve (using an Ui action) the problem record and if there are open problem task records, I want to restrict that and keep the state in fix in progress state only.

 

I am trying this using on submit client script and not business rule because when I try to resolve it state will temporarily move to resolve state but when I refresh it, it will move back to fix in progress. and I don't want that. nor did the client. 

 

so here is the script below -

client script - on submit 

function onSubmit() {
    // Check if state is changing to "Resolved"
    var state = g_form.getValue('state');
    if (state == '106') {
        var problemSysId = g_form.getUniqueValue();

        var ga = new GlideAjax('CheckOpenProblemTasks');
        ga.addParam('sysparm_name', 'hasOpenTasks');
        ga.addParam('sysparm_problem_sys_id', problemSysId);

        ga.getXMLAnswer(function(response) {
            if (response === 'true') {
                g_form.addErrorMessage('There are active Problem Tasks. These need to be closed before resolving the Problem');
                return false;
            } else {
                return true;
            }
        });

        //return false; // Stop default submit until GlideAjax completes
    }

    return true; // Allow submit if not resolving
}
 
script include - 
var CheckOpenProblemTasks = Class.create();
CheckOpenProblemTasks.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    hasOpenTasks: function() {
        var problemSysId = this.getParameter('sysparm_problem_sys_id');
        var gr = new GlideRecord('problem_task');
        gr.addQuery('problem',current.getUniqueValue());
        gr.addActiveQuery(true);
        gr.query();

        if (gr.hasNext()) {
            return 'true';
        }
        return 'false';
    }
}); 
 
but here something seems to be missing as when I try to resolve the problem even when there are active p Task it will get resolved and state will move ahead.
 
please do correct ..
 
thanks 
15 REPLIES 15

its OOTB so could not make any updates in it ..

as I made some changes to the script and it worked but

my screen got freez when i click on resolve ( hopefully it did not move forward ) 

as shown below 

Screenshot (575).png

why is that ??

@tushar_ghadage 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Anand2799
Tera Guru

Hi @tushar_ghadage ,

 

OnSubmit client script is not going to work here you need to use business rule or update the UI Action.

Create a before business rule.

Use the below script:

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

    var isPTaskExist = false;
    var grPTask = new GlideRecord('problem_task');
    grPTask.addQuery('problem', current.getUniqueValue());
    grPTask.addActiveQuery();
    grPTask.setLimit(1);
    grPTask.query();
    if (grPTask.hasNext()) {
        isPTaskExist = true;
    }
    if (isPTaskExist) {
        gs.addErrorMessage(gs.getMessage("There are active problem task related to the problem. Please close them before closing this problem"));
        gs.setRedirectURL(current);
        current.setAbortAction(true);
    }

})(current, previous);

 

You can also update the Resolve button condition to call your script include.

 

Thanks

Anand

sangrulkaradvai
Kilo Guru

hide the Resolve UI Action when open tasks exist