We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

UI Action used for the form button "Close Task"

Cupcake
Mega Guru

Hi there is a UI action for the Close Task button, the script is set to mark the task Closed Complete upon clicking that button, but I want the task to mark based on the state; therefore IF the state = Closed Complete then when you click the Close Task button the functionality should mark the task as Closed Complete and IF the state = Closed Incomplete then when the Close task button is clicked the functionality should mark the task as Closed Incomplete.

I am having some difficulty in making that happen.

find_real_file.png

function closeTask(){

      if(g_form.getValue('assigned_to') == '') {

          //Remove any existing field message, set comments mandatory, and show a new field message

          g_form.setMandatory('assigned_to', true);

          g_form.showFieldMsg('assigned_to','Assigned to is mandatory when closing the task.','error');

          return false;   //Abort submission

    }

      g_form.setValue('state', '3');

    //Call the UI Action and skip the 'onclick' function

    gsftSubmit(null, g_form.getFormElement(), 'close_Task'); //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')

    serverReopen();

function serverReopen(){

    //Set the 'State' to 'Active', update and reload the record

   

    current.update();

    action.setRedirectURL(current);

}

Thanks,

Karen

1 ACCEPTED SOLUTION

You should be able to say:



function closeTask(){


        var state = g_form.getValue('state');


      if(g_form.getValue('assigned_to') == '') {


          //Remove any existing field message, set comments mandatory, and show a new field message


          g_form.setMandatory('assigned_to', true);


          g_form.showFieldMsg('assigned_to','Assigned to is mandatory when closing the task.','error');


          return false;   //Abort submission


    }



if(state == 4) // or whatever your closed incomplete value is


      g_form.setValue('state', state);


else


      g_form.setValue('state', '3');



    //Call the UI Action and skip the 'onclick' function


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


}


View solution in original post

6 REPLIES 6

Thank you Mike - I just had a chance to get back to this and it worked perfectly.


Your a life saver.



Have an Awesome Day !


Hi Karen,



I agree with Brad here. Can you please elaborate further on your req.