Is this script ok? It doesn't look to work...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
14 hours ago
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
13 hours ago
You can directly set
g_form.setValue(‘u_harp_draft_mode ‘,true);
Also this syntax of UI action doesn’t seems fine refer below :
Raghav
MVP 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
13 hours ago - last edited 13 hours ago
Hi @Daniel Voutt ,
You can keep both client and server logic in a single UI Action. The important part is to set your flag on the client before submit, then handle the update on the server side once the action runs.
Here’s a working example (UI Action: Client = true, Action name = save_draft_and_ignore_mandatory):
// ---------------- CLIENT ----------------
function saveDraft() {
// Mark the draft flag so client logic/UI Policies pick it up immediately
g_form.setValue('u_harp_draft_mode', 'true'); // string 'true' on client
// Submit to the server portion of this same UI Action
gsftSubmit(null, g_form.getFormElement(), 'save_draft_and_ignore_mandatory');
return false; // prevent double submit
}
// ---------------- SERVER ----------------
if (typeof window == 'undefined') {
runServerCode();
}
function runServerCode() {
// Set the draft flag on the record (boolean true on server)
current.setValue('u_harp_draft_mode', true);
// Optional: skip workflow/BRs if you don’t want them triggered
// current.setWorkflow(false);
current.update();
gs.addInfoMessage('Record saved as draft.');
action.setRedirectURL(current);
}
Reference: ServiceNowGuru – Client and server code in UI Actions
If my answer helped you, please consider marking it as ✅ Correct and giving it a 👍 Helpful — it might help other members in the community too!
Thanks,
Astik T 😊💡
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
8 hours ago - last edited 8 hours ago
in your script- g_form.checkMandatory = false will not work.
just tested the ui action in my PDI and working as expected, please refer below:
onclick- saveDraft()
action name- save_draft_and_ignore_mandatory
// Client-side function (runs when Save Draft button is clicked)
function saveDraft() {
// First, make all editable fields non-mandatory for draft save
var fields = g_form.getEditableFields();
for (var i = 0; i < fields.length; i++) {
g_form.setMandatory(fields[i], false); // Remove mandatory requirement
}
gsftSubmit(null, g_form.getFormElement(), 'save_draft_and_ignore_mandatory');
}
if (typeof window == 'undefined')
setRedirect();
function setRedirect() {
current.u_harp_draft_mode = "true";
current.update();
gs.addInfoMessage("Record saved as draft.");
action.setRedirectURL(current);
}