Is updating fields in server side of UI Action that were set in client script redundant?

Jacob Nan
Tera Guru

I'm running into an issue with a 'Resolve Incident' UI action where I have a client script that checks if one of my custom fields 'Root Cause Analysis' is empty to determine if a major incident should be closed. When pressing the 'resolve' button, a client script properly sets state to 'pending analysis' if the field is empty and prompts a warning to the user. The record appears to save with the appropriate value in 'state' but upon reloading it appears to be changed back to resolved. Is this because the server side executes an update to the record for the state and incident state fields? I foresee this causing more issues in the future and want to make sure that doubling down on setting the fields isn't necessary. Commenting out the lines updating the fields allows my client script to function correctly. 

function resolveIncident() {
    //Set the 'Incident state' and 'State' values to 'Resolved', and display mandatory fields\
    g_form.setValue('incident_state', 6);
    g_form.setValue('state', 6);
    g_form.setValue('resolved_by', g_user.userID);

    gsftSubmit(null, g_form.getFormElement(), 'resolve_incident'); //MUST call the 'Action name' set in this UI Action
}

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
    serverResolve();

function serverResolve() {
    current.incident_state = IncidentState.RESOLVED;
    current.state = IncidentState.RESOLVED;
    current.resolved_by = gs.getUserID();
    current.update();
}

 

Thank You 

1 ACCEPTED SOLUTION

Sagar Pagar
Tera Patron

Hi @Jacob Nan,

 

Yes. Your serverResolve() function sets/updates the state to Resolved in UI action scripts.

 

Thanks,

Sagar Pagar

 

 

The world works with ServiceNow

View solution in original post

4 REPLIES 4

Sagar Pagar
Tera Patron

Hi @Jacob Nan,

 

Yes. Your serverResolve() function sets/updates the state to Resolved in UI action scripts.

 

Thanks,

Sagar Pagar

 

 

The world works with ServiceNow

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

Not sure if I got your issue. Can you paste some screenshot of what issue you are facing on the form

I have a client script that sets state to 'Resolved-Pending Analysis' if someone tries to close an incident with the 'Resolved' State without filling out a string field (Root Cause Analysis). When pressing this 'Resolve' ui action, it overwrites my client script and sets the incident state to Resolved still. I just wanted to make sure I can remove the lines that set fields in serverResolve(), since they are already being set in the ui action (under normal circumstances).

Here is my other client script that is being overwritten by serverResolve()

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
    if (g_form.getValue('u_major_incident') == 'true' && g_form.getValue('state')=='6') {
        g_form.setDisplay('u_root_cause', true);
        if (g_form.getValue('u_root_cause') == ''&& g_form.getValue('state')!='5') {
            g_form.setValue('state', 5);
			g_form.setValue('incident_state',5);
            alert('This incident has been marked as resolved but will remain active until a Root Cause Analysis has been provided.');
        }

    }
}