This is a common challenge in ServiceNow Workspace UI Actions when working with new records that don't yet have a sys_id. The main problem is that g_form.save() triggers a full form save which subsequently reloads the page, interrupting any further client script execution—so your modal cannot open afterward.
Key Points
- You need the sys_id of the record to proceed.
- You want to save the record first, but without full page reload.
- After saving, open the modal.
Possible Approaches
1. Use g_form.save(null, callback) (Asynchronous Save with Callback)
In client scripts on ServiceNow forms (traditional UI), g_form.save() has an asynchronous version that accepts a callback executed after the save is complete. But in ServiceNow Workspace, which runs on the Fusion UI framework (Flow Designer with React), g_form.save() behaves differently.
Still, you should check if the UI Action in the Workspace supports an async save with a callback.
Example:
g_form.save(null, function() {
// Now you have a sys_id because form is saved, open your modal here
openModal();
});
If this works, great. This pattern saves the form and after the save complete, runs the callback (no page reload).
2. Use GlideForm API / g_form.saveRecord with Promises (Workspace specific)
In Workspace, the API utilizes promises:
g_form.saveRecord().then(function() {
// form is saved, sys_id exists now
openModal();
}).catch(function(error) {
console.error('Save failed:', error);
});
This will save the record "silently" and allow you to continue your logic after the promise resolves.
Note: This is the preferred method in Workspace.
3. Use GlideRecord Insert via Script Include with client-side call
If for some reason you cannot save the form in the client script without reload, an alternative is:
- In the client script, check if sys_id is empty.
- If so, call a Script Include (via GlideAjax) that creates the record server-side using parameters submitted from the form, and returns the new sys_id.
- Update the client form with the received sys_id.
- Then proceed to open your modal.
Drawbacks:
- You must manually handle copying form field data to the create call.
- More complex; generally avoid if direct save works.
4. Use g_form.submit with onSubmit handler (not recommended)
g_form.submit() causes page reload and submit, so it is not appropriate here.
Recommended Solution for Workspace UI Action:
// Example Workspace UI Action script
(async function() {
// Check if record is new (no sys_id)
if (!g_form.getUniqueValue()) {
try {
await g_form.saveRecord();
// Now record is saved, sys_id is available
openModal();
} catch (err) {
console.error('Save failed', err);
}
} else {
// existing record, directly open modal
openModal();
}
})();
function openModal() {
// Your modal opening logic here
// e.g., Workspace modal API call or GlideModal
}
Why async/await?
Because g_form.saveRecord() returns a promise in Workspace, so you wait for the save to complete and then open the modal.
Summary
- Do not use
g_form.save() alone because it reloads the page and halts execution.
- In Workspace UI Actions, use
g_form.saveRecord() which returns a promise.
- Await the save to complete, then open your modal.
- This preserves current form state and allows your script to continue.
If you do not see g_form.saveRecord() available, or it behaves differently, please verify you are using it in the correct context of Workspace client script.
If you want, I can help you craft the exact client script UI Action code once you share more details about the modal implementation and Workspace version.