In Service portal wrong button showing as 'Cancel Incident' for requests in my tickets

vass2
Mega Expert

Friends,

I have an issue with service portal showing 'Cancel Incident' button for request , I am not good at coding.

Is it a coding issue, or can I fix this with minimum effort?

I have observed, It is showing correctly as cancel request if the ticket is in open/wip process until it gets approved.

Once approved the button showing is wrong.

find_real_file.png

I am giving the below W/F for actions button. Let me know if anything else required. Please advise me on this.

Client script

function() {

  var c = this;

c.uiAction = function(action) {

  c.data.action = action;

  c.server.update().then(function() {

    c.data.action = undefined;

  })

}

}

Server script

(function() {

data.showCancel = false; // Making cancel Incident button invisible

data.showReopen = false;

data.showRequest =false;

// Get table & sys_id

data.table = input.table || $sp.getParameter("table");

data.sys_id = input.sys_id || $sp.getParameter("sys_id");

// Valid GlideRecord

gr = new GlideRecord(data.table);

if (!gr.isValid())

  return;

// Valid sys_id

if (!gr.get(data.sys_id))

  return;

// For reopen Button

if (gr.getValue('state') == '6') {  

  data.showReopen = true;  

      }   else

    data.showReopen = false;

// For Cancel Request Button

if (gr.getValue('request_state') == 'requested'){

  data.showRequest =true;

 

 

} else

 

  data.showRequest =false;

// For Cancel Incident Button

if (gr.getValue('state') == '1'&& gr.getValue('sla_due') != '') {  

  data.showCancel = true;

 

      }   else

    data.showCancel = false;

if (input && input.action) {

  var action = input.action;

 

  // If Request table

  if (data.table == 'sc_request') {

    if (action == 'cancelrequest') {

      // Cancel Request

      //gr.setValue('incident_state', 8);

      gr.setValue('request_state','closed_cancelled');

    // gr.setValue('request_state','closed_incomplete');

      gs.addInfoMessage(gs.getMessage("You have cancelled Request : " +gr.number));

    //gr.setValue('resolved_by', gs.getUserID());

      gr.comments = 'Request cancelled by ' + gs.getUserDisplayName()   + ' via Service Portal';

      gr.work_notes = 'Request Canceled   by' + gs.getUserDisplayName() + ' via Service Portal';

      data.showRequest =false; // Making the Cancel Request button invisible after the state is changed to 8

      gr.update();

    }

  }

  // If Incident table

  if (data.table == 'incident') {

    if (action == 'cancel') {

      // Cancel Incident

      //gr.setValue('incident_state', 8);

      gr.setValue('state', 8);

      gr.setValue('resolved_by', gs.getUserID());

      gr.u_cancellation_notes = 'Incident cancelled by ' + gs.getUserDisplayName() + 'via Service Portal';

      gr.work_notes = 'Incident Canceled   by' + gs.getUserDisplayName() + ' via Service Portal';

      data.showCancel = false; // Making the Cancel   button invisible after the state is changed to 8

      gr.update();

    }

    if (action == 'Reopen') {

      // Reopen the Incident

      gr.setValue('incident_state',2);

      gr.setValue('state',2);

      gr.comments = 'Incident Reopned by' + gs.getUserDisplayName() + ' via Service Portal';

      data.showReopen = false; // Making the reopen button invisible after the state is changed to 2

      gr.update();

    }

  }

}

})();

Regards,
Srinivas

1 ACCEPTED SOLUTION

Corey,



We can see in the provided code that it is not a UI Action since the associated behaviour is coded in the Server Script :



  // If Incident table


  if (data.table == 'incident') {


    if (action == 'cancel') {


      // Cancel Incident


      //gr.setValue('incident_state', 8);


      gr.setValue('state', 8);


      gr.setValue('resolved_by', gs.getUserID());


      gr.u_cancellation_notes = 'Incident cancelled by ' + gs.getUserDisplayName() + 'via Service Portal';


      gr.work_notes = 'Incident Canceled   by' + gs.getUserDisplayName() + ' via Service Portal';


      data.showCancel = false; // Making the Cancel   button invisible after the state is changed to 8


      gr.update();


    }



Moreover, there is no code to get the UI Actions in the Server side nor in the Client controller.



Regards,


Alexis


View solution in original post

7 REPLIES 7

Alexis Hemled
ServiceNow Employee
ServiceNow Employee

Hello Srinivas,



You can add conditions in your Server Script in order to hide the button depending on the current table.



From your server side code, I can see that the button "Cancel Incident" is displayed when the variable data.showCancel is true.


However, when changing its value from false to true, the only check that is done is on the state field. You could modify this by adding checks in order to test if you are displaying an incident or a request.



Here is how to test the current table :



// For Cancel Request Button



if (data.table == 'sc_request' && gr.getValue('request_state') == 'requested'){


        data.showRequest = true;


} else {


        data.showRequest = false;


}



// For Cancel Incident Button


if (data.table == 'incident' && gr.getValue('state') == '1'&& gr.getValue('sla_due') != '') {


        data.showCancel = true;


} else {


        data.showCancel = false;


}



You can see in both if (line 3 and 10) that the first condition are testing the variable data.table.



Hope this helps



Regards,


Alexis


I could be wrong but it seems like this is an issue with the UI action itself. This ui action should not be related to this record in the first place.



If you query the table 'sys_ui_action' for a ui action with the name of 'Cancel Incident'.



Ensure the table is incident and not global or Reqest


Corey,



We can see in the provided code that it is not a UI Action since the associated behaviour is coded in the Server Script :



  // If Incident table


  if (data.table == 'incident') {


    if (action == 'cancel') {


      // Cancel Incident


      //gr.setValue('incident_state', 8);


      gr.setValue('state', 8);


      gr.setValue('resolved_by', gs.getUserID());


      gr.u_cancellation_notes = 'Incident cancelled by ' + gs.getUserDisplayName() + 'via Service Portal';


      gr.work_notes = 'Incident Canceled   by' + gs.getUserDisplayName() + ' via Service Portal';


      data.showCancel = false; // Making the Cancel   button invisible after the state is changed to 8


      gr.update();


    }



Moreover, there is no code to get the UI Actions in the Server side nor in the Client controller.



Regards,


Alexis


Hi Alexis,



Do I need any permissions to update the same, because I did the changes and look back they are gone



Regards,
Srinivas