How to download a report as a CSV directly from Salesforce Lightning?
Asked Answered
A

1

5

I am creating a Python script for downloading a report from Salesforce as a CSV.

My script is working perfectly fine for Salesforce Classic. However, I need to get it working for Lightning Experience. I'm using the simple-salesforce Python package to access our org. For SF Classic I enter a link that is structured like this: https://my-company.my.salesforce.com/my_report_id?view=d&snip&export=1&enc=UTF-8&xf=csv

The script is basically like this:

from simple-salesforce import Salesforce
import requests
import pandas as pd
import csv
from io import StringIO

sf = Salesforce(username="my_username", password="my_password",
                security_token="my_token")
sf_org = "https://my_company.my.salesforce.com/"
report_id = "0000" # Some report id

sf_report_loc = "{0}{1}?view=d&snip&export=1&enc=UTF-8&xf=csv".format(sf_org, report_id)

response = requests.get(sf_report_loc, headers=sf.headers, cookies={"sid": sf.session_id})
new_report = response.content.decode("utf-8")
df = pd.read_csv(StringIO(new_report)) # Save the report to a DataFrame.

Whenever I switch to Lightning, the link is invalid and I get redirected. Is there a way to make this work in Lightning?

Auberge answered 20/8, 2019 at 14:26 Comment(0)
E
6

Try with isdtp parameter. In classic it was used to force view pages without sidebar or header, for example add isdtp=vw to a random page and see what happens.

https://my_company.my.salesforce.com/00O.....?isdtp=p1&export=1&enc=UTF-8&xf=csv ?

(no idea what's 'p1' but that's what I see in Chrome's download history as part of the report's source URL)

Englishman answered 21/8, 2019 at 6:9 Comment(5)
Awesome! I checked the link you provided me. It now works for Lightning. But what's even better, it still works for Classic as well! Thank you very much.Auberge
Glad you got it to work again! Just remember this is UI hacking, not a real API so they can break it anytime (again). If you have some free time - the "more proper" way to fetch this report would be Analytics API: developer.salesforce.com/docs/atlas.en-us.api_analytics.meta/…Englishman
Analytics (00OR*) is not the same thing as Reports (00O6*); now my problem is that I only get a header of the report, without the details, any ideas? Chrome's download history has just isdtp in the querystring, the rest is probably POSTed but I struggle to catch it from lightning :(Gunsel
Don't have an org with Wave/Tableau connected handy, sorry. Consider posting @ salesforce.stackexchange.com ?Englishman
help.salesforce.com/s/… does anything clever happen if you click that "Share" button they mention?Englishman

© 2022 - 2024 — McMap. All rights reserved.