Related list UI action to update selected records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-19-2023 04:14 AM
I have a custom table called coverage templates and in that there are related lists of last months work order tasks, current month work order tasks, following month wots and all wots. I need an UI Action that will ask the user 'you would like to close the tasks or cancel the tasks?' and if user gives input as close then make the state of selected the tasks as closed complete and viceversa. Please provide code for ui page , ui action if possible.
I noticed that g_list cannot be used in related list ui actions , it can only be used on form ui actions. So without using g_list how to update the selected records in a related list.
related field:
the number (u_number) in coverage tickets table(u_coverage_tickets).
number (u_coverage_template_number) in work order task(wm_task).

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-19-2023 04:19 AM
Hi,
To create a UI action that asks the user to close or cancel tasks in a related list and update their state accordingly, you can use a combination of a UI action, UI page, and a server-side script in ServiceNow. Here's an outline of the steps you can follow:
Create a UI Page:
- Go to the UI Pages module and create a new UI page.
- Define the HTML template for the UI page with a form containing radio buttons for the user to select "Close" or "Cancel".
- Add a submit button to the form.
Create a UI Action:
- Go to the UI Actions module and create a new UI action.
- Set the Table to the related list table where you want to perform the action (e.g., wm_task).
- Set the Action Name and Label to a descriptive name for your UI action.
- Set the Client to true to execute the UI action on the client side.
- In the Onclick field, enter the JavaScript code to open the UI page you created earlier using g_modal.open.
- Pass the selected records' sys_ids and any other necessary parameters to the UI page using g_modal.open.
- Here's an example of the UI action code:
if (typeof(g_form) == 'undefined') {
var sysIds = g_list.getChecked();
var url = 'your_ui_page_name.do?sysparm_sys_ids=' + sysIds.join(',');
g_modal.open(url, 'UI Page Title');
} else {
alert('This UI Action is only supported in list view.');
}
3. Handle the UI Page Submission:
- In the UI page script, retrieve the selected records' sys_ids from the URL parameter.
- Process the user's selection (close or cancel) and execute the necessary server-side logic to update the records' state.
- Use GlideRecord queries to retrieve and update the related records in the server-side script.
- Here's an example of the server-side script code:
var sysIds = request.getParameter('sysparm_sys_ids').split(',');
var closeOrCancel = request.getParameter('sysparm_action');
var grTask = new GlideRecord('wm_task');
grTask.addQuery('sys_id', 'IN', sysIds);
grTask.query();
while (grTask.next()) {
if (closeOrCancel === 'close') {
grTask.state = 3; // Set state to "Closed Complete"
} else if (closeOrCancel === 'cancel') {
grTask.state = 4; // Set state to "Closed Incomplete"
}
grTask.update();
}
Thanks,
Rahul Kumar
Thanks,
Rahul Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-19-2023 06:15 AM
Can you please provide HTML code also?