- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2024 09:51 PM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2024 02:41 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2024 10:16 PM
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';
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2024 03:25 AM
@vijay23 please check the logic and if it is helpful please accepted as solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2024 10:46 PM
You can do something like this:
Client script with client checked:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2024 11:34 PM
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/