When we use g_form.setValue() on Client side function in UI Action, the value is not saving

AbhiramA7726340
Kilo Contributor
function StatusCompleted() {
    var now = new Date();
    g_form.setValue('check_in_time', now.toISOString());
    g_form.setValue('status', 'completed');
    g_form.setModified('check_in_time', true);
    g_form.setModified('status', true);
    gsftSubmit(null, g_form.getFormElement(), 'check_in');
}
if (typeof window == 'undefined') {
    StatusBlocked();
}

function StatusBlocked() {
    var gr = new GlideRecord('x_table_name');
    gr.addQuery('sys_id', current.room);
    gr.query();
    if(gr.next()) {
        gs.info(gr.getValue('status'));
        gr.setValue('status', 'blocked');
        gr.update();
    }
    action.setRedirectURL(current);
}

The values from client side script are not updating on the database
 
4 REPLIES 4

anshul_goyal
Kilo Sage

Hello @AbhiramA7726340,

What are you trying to set based on your code? What’s the business requirement here?

Are you getting any errors while using g_form.setValue()?

Best Regards,
Anshul

SANDEEP28
Mega Sage

@AbhiramA7726340 try using "g_form.save()" method in client script after setting up values. 

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Rafael Batistot
Tera Sage

Hi @AbhiramA7726340 

 

This function sets values on the form client-side and then submits the form with gsftSubmit.

 

But:

  • The values do appear on the UI, but don’t save to the database.
  • This often happens when you call a custom UI Action (like ‘check_in’) but don’t handle it properly server-side, or the server-side portion overrides it.

The line:

gsftSubmit(null, g_form.getFormElement(), 'check_in');

submits the form and triggers the UI Action with name=“check_in” on the server side.

 

But here’s the catch: if the server-side UI Action code doesn’t do anything with the form values (and especially if it’s set to “Client: true” and doesn’t let the form submit), your changes are not saved.

 

 

Solution

Let the form submit normally

 

 

If your goal is just to set values and save the form, don’t use a custom UI Action name. Just submit the form:

 

g_form.setValue('check_in_time', new Date().toISOString());
g_form.setValue('status', 'completed');
g_form.submit();

 

This submits the form with all field values you just set.

Ankur Bawiskar
Tera Patron
Tera Patron

@AbhiramA7726340 

you are setting status in both client and server side.

why not set it in server side only?

OR

Simply use server side script

Update your script with 1st approach i.e. Client + Server

function StatusCompleted() {
    gsftSubmit(null, g_form.getFormElement(), 'check_in');
}
if (typeof window == 'undefined') {
    StatusBlocked();
}

function StatusBlocked() {
    var gr = new GlideRecord('x_table_name');
    gr.addQuery('sys_id', current.room);
    gr.query();
    if (gr.next()) {
        gs.info(gr.getValue('status'));
        gr.setValue('status', 'blocked');
        gr.update();
    }
    current.setValue('check_in_time', new GlideDateTime());
    current.setValue('status', 'completed');

    action.setRedirectURL(current);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader