Monday, April 11, 2011

Passing report filters as query params to Salesforce reports !

Today, I stumbled across an interesting problem, where one needs to

“Run a report by passing parameters through link or button click”

Their is no direct way visible in report configuration screen for the same. So I scanned salesforce developer boards for the same, and found this solution that worked perfectly for me, thanks “mikef” for sharing this solution(can’t find your twitter profile Sad smile). Using the same example in discussion board, we will see next how you create a sample Account report and pass its filters as query params. These steps mentioned by “mikef”  will be followed next in the post:

First step: Create your report. Make sure the report is saved somewhere other then your customer report folder.

Second step: Add all the filters you need, ie if you want to pass in the State value create a fileter ShippingState equals and leave the value box blank.

Third step: Create your button to the report. use the id of the report. ie. /00O800000044Dq6?pv0={!ShippingState}

Now you see the /ID and then after that is a '?' that starts your url query string. The 'pv0' is the first value box in the report filters, so if you want the second you say pv1 and so on.

 

First Step : Create an Account report !

I am assuming we all are too good in this, so skipping details of this Smile I have completed this step and I am on report configuration page for Account sobject.

Second Step : Add filters !

On the Account report configuration view, I added filters on two fields i.e. “Type” and “Industry”, each with value equals blank(“”). Same is shown below :

image

Hit “Save”, followed by “Run report”. Obtain the report ID shown next after you save or run report, easiest way to do that is copy it from your browser’s address bar. In my case its something like “https://ap1.salesforce.com/00O90000001HwVr”, so my report id turns to be “00O90000001HwVr”

Third Step : Create a link/button for executing this report.

Here is the sample visualforce page code that creates the hyperlink and passes two params, each for “Type=Customer-Direct” and “Industry=Energy”. 

<apex:page >
    <a href="/00O90000001HwVr?pv0=Customer%20-%20Direct&pv1=Energy" > Run Accounts Report </a>
</apex:page>

One can also run this report by directly hitting following URL in browser, I used this practice to test several combinations as its much faster then creating pages and buttons.

https://ap1.salesforce.com/00O90000001HwVr?pv0=Customer%20-%20Direct&pv1=Energy

Either way, i.e. hyperlink or direct url. Results will be as follows

image

You might need to tweak and play with param “pv”, “pv0--------pvN” to get the correct index of filters in query params. One can easily do that by saving the report once and playing with params using the report id url shown above.

Needless to say, you can create hyperlink and buttons in many more smarter and dynamic ways, to pass user-provided inputs to run these reports in visualforce pages etc, (including that piece is out of scope for this post)

Important points to consider !

As said by mikef in the discussion board thread

Some negative issues are if a user changes your report the button might not work correctly, and there is no error checking for null values.

So if we are using this hack, one needs to be careful about changes/addition of new filters to the reports.

If you have other interesting ways to pass dynamic params to report, please share !

19 comments:

kris said...

Hi Abhinav,

I am trying to add a link on a report, that will open the record, in a window. can this be done without doing a vf page?

kris said...

gr8 post...

Abhinav Gupta said...

Thanks Kris, we just need to create a link like this > /00O90000001HwVr?pv0=Customer%20-%20Direct&pv1=Energy

it doesn't needs to be in a VF page. If you want to write a formula that populates query params like pv0.. pvn that will be good per record too.

Sunil said...

Hi Abhinav,

If i want to pass more than 255 characters through URL how can I do that??

because at a time we cant pass more than 255 characters through any URL

any Inputs on this???

Abhinav Gupta said...

no way Sunil, we can't bypass HTTP norms, even though its cloudy all around :)

Sunil said...

Is there any other way to pass more than 255 characters to report other than URL??

Abhinav Gupta said...

No, reports give limited flexibility in this regard.

Unknown said...

Helpful article...thanks!

Can the same technique be used to pass values into a case listview? That is, I created a Case View with a single filter criteria(Subject Contains ""). Want to pass in a string so that resulting view filter is "Subject Contains ".

Am trying something like what you did here for reports:

https://na2.salesforce.com/500?fcf=3D00B40000006DVsz?pv0=TestString

But doesn't seem to passing String into the filter. The view returned by the URL above is still using original filter criteria(ie Subject Contains "").

Abhinav Gupta said...

Not sure about list views @Unknown.

A small change should fix it for reports i.e. you need to use "&" instead of "?" before pv0 param. Changed url shown below :

https://na2.salesforce.com/500?fcf=3D00B40000006DVsz&pv0=TestString

Amy said...

Can you pls post an example how to acheive this using custom setting object

Thanks

Abhinav Gupta said...

Sure Amy, right now I am tight but will do that soon.

Baskaran said...

Hi, i have created a custom link on account detail page. the link works fine if there is no white space. this link works fine https://na11.salesforce.com/00OG0000002imXl?pv0=test+agency as it is an account "test agency" but it failed to pull the report when the account name is "test agency, LLC". the link is display as https://na11.salesforce.com/00OG0000002imXl?pv0=test+agency%2C+LLC. when i look at the report it removes the space between comma and LLC. please help.

Abhinav Gupta said...

Baskaran, can you please try using URL Encoding for ex. Javascript's encodeURI () function or something similar in Apex.

Or try directly using the encoded value "test%20agency,%20LLC" for ""test agency, LLC"

pranay jha said...

Hi,

I am displaying reports in client portal and while displaying the report we see all the ugly filter parameters like all the account ids i had passed and all the years and months I was passing.
Is there a way to add a parameter to my report string which would hide the filter parameters or any other way would do too.
By the way I am using iframe for displaying the reports.

Pranay

Abhinav Gupta said...

Not sure Pranay,

I believe if you can encrypt all the params to one string and decrypt them using Javascript then it might help.

For ex. the whole URL would be
yourdomain.com?param=232r2hjk2h3jk2h3k23hk23hkj

Javascript will decrypt it to individual yourdomain.com?pv0=1222

Hope this helps.

pranay jha said...

Hi Abhinav,

What I meant was that when the report gets generated on the page we see all the passed parameters, and not the URL, something like this

Filtered By:
Account: Account ID equals 001Z0000002eOwY
AND Service Type equals Natural Gas,Electricity - Retail
AND Period greater or equal "201,201"
AND Period less or equal "201,212"

I looked at the source and this whole code is displayed in a div tag called filterHeader. I tried to make its visibility=none, but it did not work.

Nick Lachey said...

Fantastic post. Here’s a tool that lets you build all types of online reporting with graphs and charts for sales, marketing, finance, HR, support, etc. http://blog.caspio.com/web_apps/how-to-add-dynamic-data-charts-and-graphs-to-your-web-apps/

O said...

How about trying to filter the report by User?

I tried appending ?pv2={$User} but that does not seem to work. It thinks I am trying to filter the user column, in the report, by text "$User" instead of the User's actual name.

Abhinav Gupta said...

$User is a variable specific to visualforce pages and formula type fields, it will never work in requests. Try passing user id, it might help.