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 LIST view

Mark Wood
Tera Contributor

Hello Experts,

I have created a UI action on the list view when I click on that button I want to show a confirm pop update
when users click on ok it will delete events which are in the closed state.
I have used g_list.action()because I want to execute server-side UI action after client-side UI action.

without selecting any record I want to execute my server side.
Written below code please guide me on this thank you.

 

function cleanup() {

 

 

    var c = confirm("Are you sure you want to perform cleanup?");
    alert(c);
    if (c) {
        // g_form.addInfoMessage('c block');
        var tablename = "u_ma_frm_event";
     //   var list = GlideList2.get(tablename);
       // var sysIds = list.getChecked();
    //gs.log("Sysid of list:"+sysIds);

 

            g_list.action('16a6a6b81b61fd50a72cfdd2cd4bcbec', 'demo');

 

        //   gsftSubmit(null, g_form.getFormElement(), 'demo');

 

    } else {
        // User cancelled the operation
        alert("Cleanup operation canceled.");
    }

 

 

}
if (typeof window == 'undefined') {
    var gr = new GlideRecord('u_ma_frm_event');
    gr.addQuery('u_ma_fld_state', 'closed');
    gr.query();
    gr.setWorkflow(false);
    if (gr.getRowCount() > 0) {
        gs.cacheFlush();
        // gr.deleteMultiple();
        var rec = new GlideRecord('sysauto_script');

 

 

        rec.get('sys_id', 'fff61e5d1be5b190a72cfdd2cd4bcb37');

 

 

        SncTriggerSynchronizer.executeNow(rec);
        // gs.executeNow(rec);
        //    
    }
}

has context menuParagraph

1 REPLY 1

Ratnakar7
Mega Sage

Hi @Mark Wood ,

 

To accomplish this, you can create a UI action and use a client-side script to confirm the action, and then trigger a server-side script to delete records.

Here's a step-by-step guide:

1. Create a UI Action:

  • Go to "System Definition" > "UI Actions."
  • Create a new UI action with the following details:
    • Name: Cleanup
    • Table: u_ma_frm_event (or the table you are working with)
    • Show Insert: Checked
    • Show Update: Unchecked
    • Show Delete: Unchecked
    • Client: True
    • Script: Add the client-side script to confirm the action (as you've shown in your code).

2. Create a Server Script:

  • Go to "System Definition" > "Script Includes."
  • Create a new Script Include with the following details:
    • Name: CleanupScript (or a suitable name)
    • Script: Write the server-side script to delete records in the closed state. Here's an example script:
      var CleanupScript = Class.create();
      CleanupScript.prototype = Object.extendsObject(AbstractAjaxProcessor, {
      
          cleanupRecords: function() {
              var tableName = 'u_ma_frm_event'; // Update with your table name
              var stateField = 'u_ma_fld_state'; // Update with your state field name
              var stateValue = 'closed'; // Update with the state value to match
      
              var gr = new GlideRecord(tableName);
              gr.addQuery(stateField, stateValue);
              gr.query();
              
              while (gr.next()) {
                  gr.deleteRecord();
              }
              
              return "Cleanup completed.";
          },
      
          type: 'CleanupScript'
      });
      ​

3. Update the Client-Side Script:

  • In your client-side script, you can use AJAX to call the server-side script. Modify your cleanup function as follows:
    function cleanup() {
        var c = confirm("Are you sure you want to perform cleanup?");
        if (c) {
            var ga = new GlideAjax('CleanupScript'); // Reference to your server-side script include
            ga.addParam('sysparm_name', 'cleanupRecords'); // Name of the server-side function
            ga.getXML(function (response) {
                var message = response.responseXML.documentElement.getAttribute('message');
                alert(message);
                // Optionally refresh the list view here
                // Example: GlideList2.get('u_ma_frm_event').refresh();
            });
        } else {
            alert("Cleanup operation canceled.");
        }
    }
    ​

4. Adding the UI Action to the List View:

  • Go to the list view of your table (e.g., u_ma_frm_event).
  • Click on "Personalize List" and add your "Cleanup" UI action to the list view.

Now, when you click the "Cleanup" UI action, it will confirm the action, call the server-side script to delete records in the closed state, and display a message indicating the cleanup is completed.

 

Thanks,

Ratnakar