How to validate if atleast one child record exists in UI action

vijay23
Tera Contributor

Hi . 

 

 i have a requirement .

to create a UI action - "Monitoring" in problem table

when click on the button , it should check if at least one child task(action table) related to problem record exists.

if not exists, it should throw an error message - "at least one action record needs to be created to move to monitoring"

all these should execute on client side .

because in sever side if we execute , state value will move to "monitoring" and error message will be displayed and after refreshing it will move back to previous state, so we need to avoid this confusion to users 

also the same requirement should work in service operation workspace .

 

Can anyone please suggest how to build the code "GLideajax call" and validate if at least one action related to problem exists and should execute this in client side?

 

1 ACCEPTED SOLUTION

seethapathy
Giga Guru
HI @vijay23  I tried your requirement in PDI And found this Logic is Working. if this helpful mark it as helpful.
 
UI Action logic
function monitoring(){
var pbid =g_form.getUniqueValue();
var ga = new GlideAjax("Your Script Include Name");
ga.addParam("sysparm_name""Your Script include Function Name");
ga.addParam("sysparm_id",pbid);
ga.getXML(testing);
function testing(response){
    var sp =response.responseXML.documentElement.getAttribute("answer");
        if(sp== true){
g_form.setValue('state','monitoring');
    }
    else{
        g_form.addErrorMessage("at least one action record needs to be created to move to monitoring");
    }
   
}}
 
Script Include 
    checkTask: function() {
        var sysid=this.getParameter('sysparm_id');
         var gr = new GlideRecord('Your child Table Name');
        gr.addQuery('problem',sysid);
        gr.query();
        if (gr.hasNext()) {
            return 'true';
        }
        else {
            return 'false';
        }

    }

View solution in original post

8 REPLIES 8

Mani A
Tera Guru

@vijay23 

Ui action logic

 

 

    var ga = new GlideAjax('CheckChildTask');

ga.addParam("sysparm_name","validateTask");

    ga.addParam('sys_id', g_form.getUniqueValue());

    ga.getXMLAnswer(function(response) {

        if (response == 'true') 

            g_form.showFieldMsg('fieldName, 'At least one action record needs to be created to move to Monitoring', 'error');

        } else {

            g_form.setValue('state', "monitoring");

        }

    });

 

 

Script include:

 

    

    validateTask: function() {

        var problemId = this.getParameter('sys_id');

        var gr = new GlideRecord('childTable');

        gr.addQuery('problem', problemId); // Replace problem field with correct one

        gr.query();

       

        if (gr.hasNext()) {

            return 'true'; 

        } else {

            return 'false'; 

        }

    }

@vijay23  please check the logic and if it is helpful please accepted as solution.

Pramod Khanadal
Tera Contributor

 

Spoiler

You can do something like this:

Client script with client checked:

function checkChangeReq() {
//Check for open tasks
    var getImpTask = new GlideAjax('YourScriptInclude');
    getImpTask.addParam('sysparm_PARM1', 'functionNAME');
    getImpTask.addParam('sysparm_PARM2', 'PARM2 Value (Parent sysID)');
    getImpTask.getXML(taskStatus);

    function taskStatus(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        if (answer == 'true') {//if active child tasks are present then abort form submission
            g_form.addErrorMessage('ERROR');
            return false;
        }
        gsftSubmit(null, g_form.getFormElement(), 'ACTIONNAME');
    }
}

if (typeof window == 'undefined')
    reviewCompletion();

function reviewCompletion() {
    script// executed if all child tasks are closed
}
 
Client callable script include:
var SCRIPTINCLUDENAME = Class.create();
NSCRIPTINCLUDENAME.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getImpTaskStatus: function() {
        var getTaskInfo = new GlideRecord('CHILDTABLENAME');
        getTaskInfo.addQuery('QUERY')//call your parameter here (parent sysID)
        getTaskInfo.query();
        if (getTaskInfo.next()) {
            return true;
        }
        return false;
    }
 
 
Regards,
Pramod

Ravi Gaurav
Giga Sage
Giga Sage

Hi @vijay23 

 

You can create a UI Action on the problem table that runs on the client side and triggers a GlideAjax call to the server-side script to check for related action tasks.
Few minutes back I have tested the script and it seems to be working as expected

UI Action Script (Client-Side):

// This is the Client Script part of the UI Action

function checkRelatedActions() {
var ga = new GlideAjax('CheckRelatedActions'); // Create a GlideAjax object
ga.addParam('sys_id', g_form.getUniqueValue()); // Send the current Problem record's sys_id
ga.getXMLAnswer(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer"); // Get the response from the server
if (answer === 'false') {
g_form.addErrorMessage('At least one action record needs to be created to move to monitoring.');
return;
} else {
// Move the problem state to "monitoring" or other actions here
g_form.setValue('state', 'monitoring'); // Change state to 'monitoring' (adjust as necessary)
g_form.save();
}
});
}

You'll need a Script Include to check if the related action tasks exist for the problem record.

Script Include (CheckRelatedActions):

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

checkRelatedActions: function() {
var problemSysId = this.getParameter('sys_id'); // Get the Problem sys_id from the client

// Query the action table for related tasks
var actionTaskGR = new GlideRecord('action'); // Replace 'action' with the correct table name if needed
actionTaskGR.addQuery('problem', problemSysId); // Assuming the 'problem' field relates to the parent problem record
actionTaskGR.query();

if (actionTaskGR.hasNext()) {
return 'true'; // At least one related action exists
} else {
return 'false'; // No related actions found
}
}
});

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/