Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Export data in excel sheet on button click

(0) ShareShare
ReportReport
Posted on by 105

Hello,

I have created a button(Approve) on account entity. If I click on this button it should export data in excel format with field name as column heading. I have written JS code to achieve this requirement.

var accName=Xrm.Page.getAttribute("name").getValue();

var data = [[accName]];
var csvContent = '';
data.forEach(function (infoArray, index) {
dataString = infoArray.join(';');
csvContent += index < data.length ? dataString + '\n' : dataString;
});

var download = function(content, fileName, mimeType) {
var a = document.createElement('a');
mimeType = mimeType || 'application/octet-stream';

if (navigator.msSaveBlob) { // IE10
return navigator.msSaveBlob(new Blob([content], { type: mimeType }), fileName);
} else if ('download' in a) { //html5 A[download]
a.href = 'data:' + mimeType + ',' + encodeURIComponent(content);
a.setAttribute('download', fileName);
document.body.appendChild(a);
setTimeout(function() {
a.click();
document.body.removeChild(a);
}, 66);
return true;
} else { //do iframe dataURL download (old ch+FF):
var f = document.createElement('iframe');
document.body.appendChild(f);
f.src = 'data:' + mimeType + ',' + encodeURIComponent(content);

setTimeout(function() {
document.body.removeChild(f);
}, 333);
return true;
}
}

download(csvContent, 'csv file.csv', 'text/csv');

And the result is:

Screenshot-_2800_3_2900_.png

It is working fine but how to display fields name too..? or Is there any other way to achieve this requirement? like flow or any other?

Thanks in Advance.

  • cloflyMao Profile Picture
    25,202 on at
    RE: Export data in excel sheet on button click

    Hi nanu2008,

    1. You can extract account name from "When a record is updated".

    In my demo, I extract the value from "When a record is selected":

    6014.JPG

    2. We should use following filter to find label of the option, ${optionsetvalue} is from the beginning action.(When xx is selected or updated.)

    Set the limit count to 1.

    objecttypecode eq 'account' and attributename eq 'industrycode' and attributevalue eq ${optionsetvalue}
    
    

    1452.JPG

    Due to we use "List records" action to find label of option, the result will be a collection,

    so we need to use an expression to only extract the first item and create a variable to save the label value,

    body('Retrieve_label_of_option')?['value'][0]?.value

    finally apply the variable to "Create CSV" action:

    pastedimage1607335393295v2.png

    Result

    pastedimage1607335426414v3.png

    You could refer to following tutorial to get the first record from the list:

    https://crmtipoftheday.com/1256/get-attribute-of-the-first-record-from-the-list/

    Regards,

    Clofly

  • _nanu2208 Profile Picture
    105 on at
    RE: Export data in excel sheet on button click

    Hi Clofly,

    1.How can we set filename as a account name. For ex. accountname.csv. I'm using SharePoint path in flow. 2. I'm getting value of Option set(Industry and relationship Type) instead of label. I have tried this solution( https://community.dynamics.com/365/f/dynamics-365-general-forum/392431/setting-text-for-option-set-fields-in-flow-email-step ) but didn't get result. How can I achieve this?

    Screenshot-_2800_58_2900_.png

    My Flow:

    1. Screenshot-_2800_64_2900_.png

    2.Screenshot-_2800_65_2900_.png

    3.Screenshot-_2800_66_2900_.png

    4.Screenshot-_2800_67_2900_.png

    5.Screenshot-_2800_68_2900_.png

  • _nanu2208 Profile Picture
    105 on at
    RE: Export data in excel sheet on button click

    Hi,

    Okay. Thanks for the quick reply.

  • cloflyMao Profile Picture
    25,202 on at
    RE: Export data in excel sheet on button click

    Hi nanu2208,

    Currently flow only supports to save file to OneDrive or SharePoint(remote server), if it is required to download the file to local machine, I suggest you keep working with JavaScript approach.

    Regards,

    Clofly

  • _nanu2208 Profile Picture
    105 on at
    RE: Export data in excel sheet on button click

    Hi,

    Thanks for the reply. I have created flow like above. Can we download this file in our machine using flow?

  • Verified answer
    cloflyMao Profile Picture
    25,202 on at
    RE: Export data in excel sheet on button click

    Hi nanu2208,

    As per my understanding, you may want to display field name as row header.

    If so, you just need to insert field as first array item to the data variable.

    var data = [
        ["Account Name"],
        [accName]
    ];

    pastedimage1606896428654v1.png

    Obviously, it will be easier if using flow.

    1. Select "Create a flow" of Flow button in ribbon, by doing that we will get the trigger "When a record is selected".

    2. Get fields value of the selected account in array format.

    3. Select "Create CSV table" action to create a csv table by using array format result of action 2.

    4. Create a CSV file in OneDrive from the output of action 3.

     pastedimage1606897307033v2.png

    In Account form, perform the flow.

    pastedimage1606897656268v3.png

    The result is same to the JavaScript approach.

    Regards,

    Clofly

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,140 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,895 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans