UI action not working on Mandatory and updation

sumityadav8
Tera Contributor
I created an UI action named resolved on my custom HR case table, but my UI Action is not working as per my requirement, I am trying to make resolution notes field mandatory and also after running client script, I am makking the state resolved, but state is not changing
 
Please guide, Thank you
 
 
 
 
 
 
function checkres() {
    var res = g_form.getValue('u_resolution_notes');
    if (!res || res.trim() == '') {
         g_form.setMandatory('u_resolution_notes',true);
        g_form.showFieldMsg('u_resolution_notes', "Resolution notes is manadatory", 'error');
    } else {
        gsftSubmit(null, g_form.getFormElement(), 'hr_in_resolved');
    }
    if (typeof window == undefined) {
        current.u_state == 21;
        current.update();
        action.setRedirectURL(current);
    }
alert("hiii");
}
current.u_state == 21;
current.update();
action.setRedirectURL(current);
2 ACCEPTED SOLUTIONS

Rafael Batistot
Kilo Patron

Hi @sumityadav8 

 

It appears you're trying to combine both client-side and server-side logic within a single UI Action, which is a common point of confusion in ServiceNow

 

  1. current.u_state == 21; → this is a comparison, not an assignment.
    • It never changes the value. You need = not ==.

May you try this code separate

 

OnClick client function (validation only):

function checkres() {
var res = g_form.getValue('u_resolution_notes');

if (!res || res.trim() == '') {
g_form.setMandatory('u_resolution_notes', true);
g_form.showFieldMsg('u_resolution_notes', "Resolution notes is mandatory", 'error');
return false; // stop here
}

// If valid, submit to server script
gsftSubmit(null, g_form.getFormElement(), 'hr_in_resolved');
}

 

Server-side script (update record):

if (typeof g_form == 'undefined') { // server-side only
if (action.getName() == 'hr_in_resolved') {
current.u_state = 21; // <-- assignment, not comparison
current.update();
action.setRedirectURL(current);
}
}

 

 

 

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

View solution in original post

Its_Azar
Tera Guru
Tera Guru

Hi @sumityadav8 

 

 your script is mixing up client-side and server-side logic, thats the reason for not working

On the client script you should only handle form validation (making resolution notes mandatory, showing field message, etc.). That part with g_form.setMandatory and gsftSubmit is correct.

Updating the record (current.u_state = 21; current.update();) should be done in the UI Action’s server-side script section, not in the client-side onClick. 

☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.




Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG

View solution in original post

2 REPLIES 2

Rafael Batistot
Kilo Patron

Hi @sumityadav8 

 

It appears you're trying to combine both client-side and server-side logic within a single UI Action, which is a common point of confusion in ServiceNow

 

  1. current.u_state == 21; → this is a comparison, not an assignment.
    • It never changes the value. You need = not ==.

May you try this code separate

 

OnClick client function (validation only):

function checkres() {
var res = g_form.getValue('u_resolution_notes');

if (!res || res.trim() == '') {
g_form.setMandatory('u_resolution_notes', true);
g_form.showFieldMsg('u_resolution_notes', "Resolution notes is mandatory", 'error');
return false; // stop here
}

// If valid, submit to server script
gsftSubmit(null, g_form.getFormElement(), 'hr_in_resolved');
}

 

Server-side script (update record):

if (typeof g_form == 'undefined') { // server-side only
if (action.getName() == 'hr_in_resolved') {
current.u_state = 21; // <-- assignment, not comparison
current.update();
action.setRedirectURL(current);
}
}

 

 

 

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

Its_Azar
Tera Guru
Tera Guru

Hi @sumityadav8 

 

 your script is mixing up client-side and server-side logic, thats the reason for not working

On the client script you should only handle form validation (making resolution notes mandatory, showing field message, etc.). That part with g_form.setMandatory and gsftSubmit is correct.

Updating the record (current.u_state = 21; current.update();) should be done in the UI Action’s server-side script section, not in the client-side onClick. 

☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.




Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG