How to trigger scheduled job of get api from ui action in list view

Kumar147
Tera Contributor

Hi,

I am getting records from another instance and storing in custom table using GET Api in scheduled job and set it to run in every 60mins. I want a ui action to be created in list view of custom table so user can click on the button to get the updated records. How can i do that.

 

Thanks in advance

1 ACCEPTED SOLUTION

Medi C
Giga Sage

@Kumar147 

Another possible solution would be to directly have your REST Call on a function on your script include, then you can simply call your function from the UI Action and update the records based on the returned response.


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

View solution in original post

13 REPLIES 13

Medi C
Giga Sage

Hi @Kumar147 ,

 

The following script can be used to invoke "Execute Now" on a a scheduled job:

 

 

(function(){
  var gr = new GlideRecord("sysauto_script");
  gr.addQuery("name", "NAME OF YOUR SCHEDULED JOB);
  gr.query();
  if (gr.next()) {
    SncTriggerSynchronizer.executeNow(gr);
    gs.log("Scheduled Job executed);
  } else {
    gs.log("Scheduled Job not found");
  }
})();

 

 

 

But since you want to pass some data as per your list selection, I would recommend using "event" rather than "Scheduled Job" as you can pass parameters and the whole operation would be running in the background, so users will not get stuck waiting for the operation to terminate.

 

  1. You can create your event from  All > System Policy > Events > Registry
  2. Create a Script Action All > System Policy > Events > Script Action Set the event name to your event created in step 1
  3. You can set your GET REST Call on the Script Action and access the event parameters as follow:

 

 

var selectedItems = event.parm1;
var anotherParameter = event.parm2;
//YOUR REST CALL IN HERE

 

 

  • Now, on your UI action, you could simply gather the selected rows from your list, and make a GlideAjax to a script include which would trigger the event:

 

 

    var tblName = g_list.getTableName();
    var selSysIds = g_list.getChecked();​
    var ajaxHelper = new GlideAjax('YOUR SCRIPT INCLUDE CLIENT CALLABLE');
    ajaxHelper.addParam('sysparm_name', 'triggerEvent');
    ajaxHelper.addParam('sysparm_listids', selSysIds);
    ajaxHelper.addParam('sysparm_tablename', tblName);
    ajaxHelper.getXML(processResult);

    function processResult(response) {
       var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer) {
            //DO YOUR LOGIC
        }
    }

 

 

 

  •  On the function on your script include you can trigger the event

 

 

gs.eventQueue('YOUR_EVENT_NAME', current, PARAM1, PARAM2);​

 

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Kumar147
Tera Contributor

Hello @Medi C 

 

Thanks for your response.

If i unchecked the client in ui action its working when i selected any of the record in list view. Similarly i want this to be worked even if i not checked any of the record. Just to refresh the page when i clicked on ui action and to trigger the api.

Medi C
Giga Sage

Hi @Kumar147 ,

The following can be used on UI Action to reload / refresh the page.

 

 

 

Client unchecked: 

action.setRedirectURL(current);

 

Client checked:

g_navigation.reloadWindow();

 

You can based if there are items selected on the list to determine if you want to update only the selected records or all of them.


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Medi C
Giga Sage

@Kumar147 

Another possible solution would be to directly have your REST Call on a function on your script include, then you can simply call your function from the UI Action and update the records based on the returned response.


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.