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.


Thanks & Best regards,
Medi

View solution in original post

13 REPLIES 13

Kumar147
Tera Contributor

Hi @Medi C 

I am trying with script include. I am getting response from script include but not entering into the UI Action script. Script as follows

Script include:

var pagedR = new sn_ws.RESTMessageV2('Rest Message Name', 'Method');
        pagedR.setEndpoint('URL');

        var response = pagedR.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();
        var prsData = JSON.stringify(responseBody);
        gs.log('test data :'+prsData);
        return prsData;
 
Ui Action Script:
function restCall() {
    var ga = new GlideAjax('Script Include Name');
    ga.addParam('sysparm_name', 'Function');
    ga.getXML(processResult);

    function processResult(response) {

        var answer = response.responseXML.documentElement.getAttribute("answer");
        answer = JSON.parse(answer);
        if (answer != '' && answer != undefined) {
            gs.log('answer is coming');
        }

    }
}
 
Kumar147_1-1741785668140.png

 


 

@Kumar147 

did log from script include come?

try this

function restCall() {
    var ga = new GlideAjax('Script Include Name');
    ga.addParam('sysparm_name', 'Function');
    ga.getXMLAnswer(function(answer) {
        alert(answer);
    });
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello @Ankur Bawiskar 

I am getting alert and UI action is triggering

@Kumar147 

did script include trigger? what came in log for this line?

gs.log('test data :'+prsData);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello @Ankur Bawiskar 

I am getting response body from the target instance. I am trying to update the response in my custom table using the following script

 

var ga = new GlideAjax('Script Include Name');
    ga.addParam('sysparm_name', 'Function');
    ga.getXMLAnswer(function(answer) {
        answer = JSON.parse(answer);

        alert(answer.result[0].number);

        for (var i = 0; i <= answer.result.length; i++) {

            var restGR = new GlideRecord('u_custom_change_request');
            restGR.addQuery('u_number', answer.result[i].number);
            restGR.query();
            if (restGR.next()) {
                restGR.u_change_sys_id = answer.result[i].sys_id;
                restGR.u_active = answer.result[i].active;
                restGR.u_type = answer.result[i].type;
                restGR.u_state = answer.result[i].state;
                restGR.u_start_date = answer.result[i].start_date;
                restGR.u_end_date = answer.result[i].end_date;
                restGR.u_correlation_id = answer.result[i].correlation_id;
                restGR.u_correlation_display = answer.result[i].correlation_display;
                restGR.sys_created_by = answer.result[i].sys_created_by;
                restGR.sys_updated_by = answer.result[i].sys_updated_by;
                restGR.sys_created_on = answer.result[i].sys_created_on;
                restGR.update();
            } else {
                restGR.initialize();
                restGR.u_active = answer.result[i].active;
                restGR.u_number = answer.result[i].number;
                restGR.u_change_sys_id = answer.result[i].sys_id;
                restGR.u_type = answer.result[i].type;
                restGR.u_state = answer.result[i].state;
                restGR.u_start_date = answer.result[i].start_date;
                restGR.u_end_date = answer.result[i].end_date;
                restGR.u_correlation_id = answer.result[i].correlation_id;
                restGR.u_correlation_display = answer.result[i].correlation_display;
                restGR.sys_created_by = answer.result[i].sys_created_by;
                restGR.sys_updated_by = answer.result[i].sys_updated_by;
                restGR.sys_created_on = answer.result[i].sys_created_on;
                restGR.insert();
            }
        }
        alert('List Refreshed');
        g_navigation.reloadWindow();
    });