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

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Karen,



Typically a button like that would just set the state of the task to closed. What's the purpose of using the button if the user is able to use the field to set the closed status?


Brad thanks for that.



I did not initially set it up and I am not sure of the logic that went into setting it like that. I am not sure if it was requested by the business with little understanding of the purpose. This is why I am trying to do some cleanup in various places. I've seen much inconsistency.


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


}


Mike thank you, I am just getting back to this project. I will try that and let you know if I was successful.