I want to generate a excel file of selected records in a list?

Shrey hurana
Tera Contributor

Hi All,

I want to generate a excel report of selected records in a list, when the user clicks on 'Generate Excel Report' UI action(List Choice),created by me.

Is there a way I could do this.

4 REPLIES 4

Hello Shibasou,

 

Yes I tried, but not as per my requirement because it is download based on the view.

I need to download the excel file based on the columns shown on the list layout by user when he clicks on this button

Can you please help me with the same.

 

Regards,
Shrey Khurana

Deepak Shaerma
Kilo Sage

Hi @Shrey hurana 
Step 1: Create the UI Action

1. Navigate to System Definition > UI Actions.
2. Click New to create a new UI Action.
3. Fill in the fields:
Name: Give your UI Action a descriptive name, e.g., “Generate Excel Report”.
Table: Choose the table from which you want to generate the report, e.g., Incident.
Action name: This is automatically filled based on the Name, but you can customize it.
Client: Check this box since we’ll use client-side code to capture selected records.
List: Check this box to make this UI Action appear in the list context menu.
Form: You might leave this unchecked if you don’t want it to appear in form view.
Step 2: Scripting the UI Action

function() {
    // Use the GlideList2 API to get the current list and the sys_ids of selected items
    var sysIds = g_list.getChecked(); // captures the sys_ids of selected records
    
    // Check if at least one record is selected
    if (sysIds) {
        // Construct the URL to export the selected records to Excel
        var url = 'YOUR_INSTANCE_NAME/xlsx.do?sysparm_sys_ids=' + sysIds + '&sysparm_table=YOUR_TABLE_NAME';
        
        // Open the URL to initiate download (replace YOUR_INSTANCE_NAME and YOUR_TABLE_NAME with actual values)
        window.open(url, '_blank');
    }
    else {
        // If no record is selected, alert the user
        alert("Please select at least one record.");
    }
}

Replace YOUR_INSTANCE_NAME with your ServiceNow instance name.
- Replace YOUR_TABLE_NAME with the actual table name you’re targeting, corresponding to the table you set for the UI Action.

Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma 





Thank you for the response.

It did not worked.

Please find below attached screenshots.

Shreyhurana_0-1712294337579.png

 

Shreyhurana_1-1712294355512.png

 

Shreyhurana_2-1712294372073.png

 

Request you to please help me with the same.