Onload client script button not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago - last edited 2 hours ago
Hi,
In the below onload client script, I kept the button "clear flag" when we click on this button it should make the checkbox field u_cirt_in_progress to false and should save the form and stay on the same page but right now it is taking me to the new record page after clicking and it is not setting the checkbox field u_cirt_in_progress to false. Could you please provide the fix for this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
When i use type="button" I can't able to click the button. I tried removing the g_form.save(); but still it is taking me to the new record page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
32m ago
but what sort of business requirement is this?
Ankur
⨠Certified Technical Architect || ⨠10x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Hi @krishnanara ,
There are two issues with the current implementation:
There is a missing closing quote in the g_form.setValue() statement
g_form.setValue('u_cirt_in_progress', 'false');
I reproduced this scenario
1. OnLoad Client Script
Keep the Client Script only for displaying the message:
function onLoad() {
if (g_form.getValue('u_cirt_dfir_case_in_progress') == 'true') {
g_form.addErrorMessage(
'ā This account currently has a CIRT/DFIR case in progress. ' +
'Use the "Clear Flag" button to clear it.'
);
}
}
2. Create a Client UI Action
Create a UI Action on the same table:
Name: Clear Flag
Action name: clear_cirt_flag
Form button: ā
Client: ā
Onclick:clearCirtFlag();
function clearCirtFlag() {
if (g_form.getValue('u_cirt_dfir_case_in_progress') != 'true') {
g_form.addInfoMessage('CIRT/DFIR flag is already cleared.');
return false;
}
g_form.setValue('u_cirt_dfir_case_in_progress', 'false');
g_form.save();
return false;
}
You can also add the following Condition to the UI Action so that the button is displayed only when the flag is true
current.u_cirt_in_progress == true
Result
When u_cirt_in_progress = true:
ā This account is currently in progress.
[Clear Flag]Clicking Clear Flag changes:
u_cirt_in_progress = trueto:
u_cirt_in_progress = falseand saves the record while keeping the user on the same record/form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
I'm looking to keep the button within the banner message
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
41m ago
Hi @krishnanara ,
If you want to keep the Clear Flag button inside the banner message, I tested the same approach and was able to get it working
- Correct the missing quote in g_form.setValue().
- Add type="button" and return false to the HTML button.
- Uncheck Isolate script in the Client Script so the inline onclick="clearflag()" can access the function.
function onLoad() {
if (g_form.getValue('u_cirt_dfir_case_in_progress') == 'true') {
g_form.addErrorMessage(
'ā This account currently has a CIRT/DFIR case in progress. ' +
'<button type="button" onclick="clearflag(); return false;">Clear Flag</button>'
);
}
}
function clearflag() {
g_form.setValue('u_cirt_dfir_case_in_progress', 'false');
g_form.save();
return false;
}
Also make sure Isolate script is unchecked for this Client Script.
