Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Extract data in CSV file for selected records via Scripted REST API

Shalaka Vaze
Tera Contributor

Hi Experts,

I am new to SRAPIs. I am trying to implement a user functionality where, the user can select any number of records from the list view of a table and download it in CSV format. I am trying to achieve this via making use of SRAPI. However, I am unsure on how do I pass the user selected records sys ids while making the API call. Can anyone please help me undersatnd this?

1 ACCEPTED SOLUTION

Hi @Shalaka Vaze 

While I have not tested the following script, you can try it-

 

function invokeGetAPI() {
    
    var sysIds = g_list.getChecked(); // Get the selected sys_ids from the list view

    
    var apiUrl = '/api/your_scripted_rest_api_endpoint?selectedSysIds=' + sysIds.join(','); // Construct the URL for the GET API endpoint

    // Make the GET request to the Scripted REST API
    var xhr = new XMLHttpRequest();
    xhr.open('GET', apiUrl, true);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.onreadystatechange = function () {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200) {
                
                console.log(xhr.responseText);
            } else {
        
                console.error('Error:', xhr.statusText);
            }
        }
    };
    xhr.send();
}

Also, can you mark my SRAPI answer correct and helpful for the interest of community?

 

Regards,

Amit

View solution in original post

5 REPLIES 5

Amit Pandey
Kilo Sage

Hi @Shalaka Vaze 

 

Do you want to make a GET call to download CSV in response from the Scripted REST API endpoint?

 

Regards,

Amit

Yes Thats correct

Hi @Shalaka Vaze 

 

You can create a scripted REST API something like below-

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var selectedSysIds = request.queryParams.selectedSysIds.split(',');
    var gr = new GlideRecord('your_table_name');
    gr.addQuery('sys_id', 'IN', selectedSysIds);
    gr.query();

    // Prepare CSV header
    var csv = 'Field1,Field2,Field3\n'; // Customize with your actual field names
    while (gr.next()) {
        csv += '"' + gr.getValue('field1') + '","' + gr.getValue('field2') + '","' + gr.getValue('field3') + '"\n';
    }

    response.setContentType('text/csv');
    response.setBody(csv);
})(request, response);

Your endpoint URL should look like this where you can enter sys_ids (comma separated) to download the CSV-

 

GET /api/your_scripted_rest_api_endpoint?selectedSysIds=<comma_separated_sys_ids>

Please mark my answer helpful and correct.

 

Regards,

Amit

Thanks @Amit Pandey ! Can you also help me understand how can I pass the sys_ids from the UI action script? When the user will click on the button, that UI action should invoke this GET API maybe via a script include or if possible, directly from itself. I already have the SRAPI ready but the part I am not undersatnding is  passing the list of sys_ids which we will be able to get from the UI action with the help of below code - 

 

var sys_id = g_list.isChecked();

var sys_arr = sys_id.split(",");