State is not setting to previous value in before update BR?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 12:13 AM
Hi all,
When change task move to close state if there is no attachment change task abort the action and set state and closed code to previous value.
I have written Before update BR on change_task table:
condition state change to closed.
only the state is not set to previous value
can anyone helpme on this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 01:49 AM
not possible.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 12:37 AM
Hi @Saranya K
The abort business rule only prevents data updates in the database. Although the state value may appear as "Closed," it will revert to the previous value once you refresh the page.
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 12:54 AM
Hi @J Siva
I want to set state value to previous one without refreshing the form how to achieve it .please let me know
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 01:03 AM
@Saranya K I don't think it's possible. it's OOB behavior.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 01:14 AM - edited 06-03-2025 01:48 AM
Hi @Saranya K ,
To update the value without requiring a page reload, it is advisable to use a Client Script instead of a Business Rule.
To update the state and close_code without reloading the page in ServiceNow, you can use GlideAjax to make an asynchronous call to a Script Include. This way, you can validate attachments and update the fields dynamically without forcing a page reload.
Steps to Achieve This:
Create a Script Include (Server-Side)
Go to Server-Side Script Include and create a new Script Include (set it as Client Callable).
Use the following script to check for attachments and revert the state:
var ValidateAttachment = Class.create();
ValidateAttachment.prototype = {
initialize: function() {},
checkAttachment: function(changeTaskId) {
var attach = new GlideRecord('sys_attachment');
attach.addQuery('table_name', 'change_task');
attach.addQuery('table_sys_id', changeTaskId);
attach.query();
if (!attach.next()) {
var task = new GlideRecord('change_task');
if (task.get(changeTaskId)) {
return JSON.stringify({
errorMessage: "Kindly attach evidence before closing.",
previousState: task.state,
previousCloseCode: task.close_code // you need to check your logic and update
});
}
}
return JSON.stringify({
successMessage: "Valid attachment found."
});
}
};
Create a Client Script (Client-Side)
Go to Client Scripts and create a new script that runs onSubmit.
Call the Script Include using GlideAjax:
function onSubmit() {
var ga = new GlideAjax('ValidateAttachment');
ga.addParam('sysparm_name', 'checkAttachment');
ga.addParam('sysparm_changeTaskId', g_form.getValue('sys_id'));
ga.getXMLAnswer(function(response) {
var result = JSON.parse(response);
if (result.errorMessage) {
g_form.addErrorMessage(result.errorMessage);
g_form.setValue('state', result.previousState);
g_form.setValue('close_code', result.previousCloseCode);
return false; // Prevent form submission
}
});
return false; // Ensures submission is stopped if validation is needed
}
Stay awesome,
Roshnee Dash
