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 working on two clicks

shrutim
Kilo Contributor

Hello All,

I am having a small requirement   to force close change request   when a list choice UI action button"Force Close" is pressed by changing   the state to close complete.

I have made a List choice UI action named "Force Close" but the problem, I am facing here is that , change request get   closed only when I click on this button twice. Its not working on a single click. Also the page is not reloading in both the clicks, I tried to reload it with script too.. but then its reloading at first click and change request remained in their former state only , since as I told before changes are getting closed   when force close button is clicked twice.

Pasting my code snippet here:

UI Action:

Onclick : forceClose();

  Client callable : true

function forceClose(){

var sys_ids = g_list.getChecked();

var cr = new GlideRecord('change_request');

cr.addQuery('sys_id','IN',sys_ids);

cr.orderBy('sys_created_on');

cr.query();

while (cr.next()){

      if(cr.company != 'Company sys_id'){

      alert('Force Close is not applicable only for other company');

              return;

         

if(cr.company == 'Company sys_id') {

      close(cr.sys_id);

}

}

      function close(id){

      var ga = new GlideAjax('HelloWorld');

ga.addParam('sysparm_name','helloWorld');

ga.addParam('sysparm_sys_id',id);

ga.getXMLWait();

alert(ga.getAnswer());

      }      

}

Script Include:

Name: HelloWorld

var HelloWorld= Class.create();

HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, {

      helloWorld: function() {

              var sys_id = this.getParameter('sysparm_sys_id');

              var cr = new GlideRecord('change_request');

              cr.addQuery('sys_id','IN',sys_id);

              cr.query();

              while (cr.next()){

                      cr.state = 5;

                      cr.u_stage = 'Closed';

                      cr.update();

              }

              return "Change has been forced closed";

      }

});

Thanks in advance!!

3 REPLIES 3

zica
Giga Guru

Hi Shruti,



Unless I did not get it correctly, I think you dont need to use a script include to get your task closed.


USe the following script to force it to close (add the condition per your requierements )



actiion name : Reject_Comments_Mandatory


client : true,


show insert / update : true


onclick : closeRequest()



//Client-side 'onclick' function
function closeRequest() {
      g_form.setValue('state', 'rejected');


        // use your company condition here


        if ( comapny name   == 'xxxx') { // the company name that cannot close the call
  try {g_form.hideFieldMsg(your field name);} catch(e) {}
  g_form.showFieldMsg(your field name,Cannot close','error');
  g_form.flash("comments", "#FFFACD", 0);
  return false;   //Abort submission
  }

  //Call the UI Action and skip the 'onclick' function
  gsftSubmit(null, g_form.getFormElement(), 'Reject_Comments_Mandatory'); //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')
  serverReject();

function serverReject()
{
current.state='rejected';
current.update();
}


shrutim
Kilo Contributor

Hello   Zic,



Thanks for your response.



My requirement is to force close the change request for one particular company E.g. ABC   and show a popup for other companies stating that   "force close is not permitted for this company"


My this task is done, but just the issue I am facing is that its working when I click on "Force close" list choice UI action Button twice.


The reason I used Script include over here is that I want to force close Change Request at "server side" and at the same time get a popup from "client side" if the company is not E.g. ABC .Kindly suggest if it can be done in some other way too.



I tried your code too.. but I   didn't get the desired output.


zica
Giga Guru

Malaviya,



I tested it in my instance and i got it work,


Instead of testing with a company name, I tested if with the caller field on incident table (both are referenced field)


As it is just one particular caller, I put the sys_id of the value but you can GlideRecord and get the displayValue of the field.



Here is the script :



//Client-side 'onclick' function


action name : forceClose // important to put action name in order to call the server side script


CLient : true;


onClick : forceClose()



function forceClose() {


                if (g_form.getValue('caller_id') !=   '62826bf03710200044e0bfc8bcbe5df1') { // the company name that cannot close the call
              try {g_form.hideFieldMsg('caller_id');} catch(e) {}
              g_form.showFieldMsg('force close is not permitted for this company','error');
              g_form.flash("caller_id", "#FFFACD", 0);
              alert('force close is not permitted for this company');
              return false;   //Abort submission
  }


  //Call the UI Action and skip the 'onclick' function
  gsftSubmit(null, g_form.getFormElement(), 'forceClose'); //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')
  serverReject();


function serverReject()
{


current.state='6'; // change the state value with your changeRequest state Value
current.update();
}



Here are the screenshots :


When the caller is not abel tuter, then it does not allow me to change :


find_real_file.png



Conversly, while the caller is abel tuter, here is the update


find_real_file.png