Is this script ok? It doesn't look to work...

Daniel Voutt
Tera Contributor
I have created the script below to use in a UI Action script window but it's not working as expected.
I want the script to populate a true value into a true/false field so that mandatory fields are then disabled until the value is removed again.  The below submits the form but doesn't apply the value.  Is it ok to use client and server side scripts at the same time, is that my issue?
 
Any guideance would be apprecaited! 
 
 
function saveDraft() {
    // Temporarily bypass the mandatory field validation on the client side
    g_form.checkMandatory = false;

    // Call the server-side function defined in the UI Action
    gsftSubmit(null, g_form.getFormElement(), 'save_draft_and_ignore_mandatory');
}

if (action.getGlideURI().toString().indexOf("save_draft_and_ignore_mandatory") > -1) {
    // Set the value of the field
    current.u_harp_draft_mode = true;
    // Update the record without running business rules or workflows
    // This is safer for simple updates
    current.setWorkflow(false);
    current.update();
     // Send a confirmation message to the user
    gs.addInfoMessage("Record saved as draft.");

    // Redirect the user back to the updated record
    action.setRedirectURL(current);
}
3 REPLIES 3

RaghavSh
Kilo Patron

You can directly set 

g_form.setValue(‘u_harp_draft_mode ‘,true);

 

Also this syntax of UI action doesn’t seems fine refer below :

https://www.servicenow.com/community/itsm-articles/client-side-and-server-side-code-in-one-ui-action... 

 


Raghav
MVP 2023

Astik Thombare
Tera Sage

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 😊💡

 

Chavan AP
Kilo Sage

@Daniel Voutt 

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);
}

 

 

Glad I could help! If this solved your issue, please mark it as Helpful and Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****