Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

UI Action is not working

tushar_ghadage
Tera Contributor

I have a requirement where :

when only on assess and RCA state reject button should be visible ( working)

and when if clicked on assess then first make the work notes mandatory (its working) it should move back to new state (not working) & if clicked on RCA first it should make the work notes mandatory and then it should move back to assess state. (not working).

 

here is the code: 

onclick: setnotes()

condition :(current.state == 102 || current.state == 103) &&( current.approval == 'not requested');

 

function setnotes() {

    var prbvalue = g_form.getValue('state');
    var workvalue = g_form.getValue('work_notes');
    if (prbvalue == '102') {
        if( workvalue ==''){
        g_form.setMandatory('work_notes', true);
        }
        gsftSubmit(null, g_form.getFormElement(), 'reject_problem');
        saveNew();
       
    } else if (prbvalue == '103') {
        if( workvalue ==''){
        g_form.setMandatory('work_notes', true);
        }
        gsftSubmit(null, g_form.getFormElement(), 'reject_problem');
        saveAssess();
    }
}

function saveNew()
{
    current.state = 101; // new state
    current.update();
    action.setRedirectURL(current);
}


function saveAssess()
{
    current.state = 102; // Assess state
    current.update();
    action.setRedirectURL(current);
 
 what's the issue here 
 
anything would help!!
 
thanks!!
 
5 REPLIES 5

Shaqeel
Mega Sage
Mega Sage

Hi @tushar_ghadage 

 

The issue here is that you're mixing client-side code (UI actions) with server-side operations (changing current.state and calling current.update()) in the same function.

When you call saveNew() or saveAssess() from the client-side function, those server-side lines never execute, because the client script cannot directly modify current or call update().

 

current.state = 101; current.update(); is server-side only.Your onclick function runs in the browser, so those lines are ignored.gsftSubmit() submits the form with the action name (reject_problem), but you haven't implemented the logic for state change in the server-side UI Action or Business Rule.

 

You need to split responsibilities:

  1. Client-side UI Action:

    • Validate work notes.
    • Make work_notes mandatory.
    • Submit the form with a custom action name.
function setnotes() {
    var prbvalue = g_form.getValue('state');
    var workvalue = g_form.getValue('work_notes');

    if (workvalue == '') {
        g_form.setMandatory('work_notes', true);
        alert('Please fill in Work Notes before rejecting.');
        return false; // Stop submission
    }

    if (prbvalue == '102') {
        gsftSubmit(null, g_form.getFormElement(), 'reject_to_new');
    } else if (prbvalue == '103') {
        gsftSubmit(null, g_form.getFormElement(), 'reject_to_assess');
    }
}

 

2. Server-side UI Action (with Server checked):

  • Create two separate actions: reject_to_new and reject_to_assess.
  • In each, update the state and redirect.

Example for reject_to_new:

current.state = 101; // New
current.update();
action.setRedirectURL(current);

Example for reject_to_assess:

current.state = 102; // Assess
current.update();
action.setRedirectURL(current);

 

 


***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

***********************************************************************************************************************





Regards

Shaqeel

that means -

create a new (1st)Ui action with action name - reject_to_new - client ( checkbox false) -with below code

and show update/insert checked also also form button and rest unchecked-->

current.state = 101; // New
current.update();
action.setRedirectURL(current);

create 2nd Ui action with action name - reject_to_assess - client ( check box false) with below code ->

&& show update/insert checked also form button and rest unchecked -->

current.state = 102; // Assess
current.update();
action.setRedirectURL(current);

 

keep the rest of the logic in main Ui action - which is visible on the form (Reject) where the client check box is true. as given below -

function setnotes() {
    var prbvalue = g_form.getValue('state');
    var workvalue = g_form.getValue('work_notes');

    if (workvalue == '') {
        g_form.setMandatory('work_notes', true);
        alert('Please fill in Work Notes before rejecting.');
        //return false; // Stop submission
    }

    if (prbvalue == '102') {
        gsftSubmit(null, g_form.getFormElement(), 'reject_to_new');
    } else if (prbvalue == '103') {
        gsftSubmit(null, g_form.getFormElement(), 'reject_to_assess');
    }
}

 

is my understanding correct??

 

 

i have tried you method its working partially but i am facing with one error have you got any idea :

Screenshot (578).png

Sarthak Kashyap
Mega Sage

Hi @tushar_ghadage ,

 

Can you change your UI Action to server callable only, no client callable and try to add below script 

 

Condition - (current.state == 102 || current.state == 103) && current.approval == 'not requested'

 

if (current.work_notes.nil()) {
    gs.addErrorMessage("Work notes are mandatory to reject.");
    action.setRedirectURL(current);
    return;
}

if (current.state == 102) {
    current.state = 101;
} else if (current.state == 103) {
    current.state = 102;
}

current.update();
action.setRedirectURL(current);

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,
Sarthak