UI action button

Aishwarya3017
Tera Contributor

I need to create a UI action button next to flag article button for downloading the knowledge article in csv format.

1 REPLY 1

Dnyaneshwaree
Mega Sage

Hello @Aishwarya3017

Create Client callable UI action and name it accordingly and add your table name(Knowledge). And refer below code as a example and write your script accordingly:

function downloadAsCSV() {
    var articleSysId = g_form.getValue('sys_id'); // Get the current article's sys_id
    var url = '/api/now/table/knowledge_base_article/' + articleSysId + '?sysparm_fields=title,text'; // Adjust fields as needed
    
    // Perform a REST API call to get article data
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.setRequestHeader('Accept', 'application/json');
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4 && xhr.status === 200) {
            var article = JSON.parse(xhr.responseText);
            
            // Convert article data to CSV format
            var csvContent = "data:text/csv;charset=utf-8,";
            csvContent += "Title,Text\n";
            csvContent += '"' + article.title + '","' + article.text.replace(/"/g, '""') + '"\n';
            
            // Create a temporary link element and trigger the download
            var encodedUri = encodeURI(csvContent);
            var link = document.createElement("a");
            link.setAttribute("href", encodedUri);
            link.setAttribute("download", "article.csv");
            document.body.appendChild(link); // Required for Firefox
            link.click();
            document.body.removeChild(link); // Clean up
        }
    };
    xhr.send();
}

downloadAsCSV(); // Call the function immediately upon clicking the UI action

Please accept my solution if it works for you and thumps up to mark it as helpful.

Thank you!!

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru