0

We have a visualforce/googlechart element on our page that our users would like to be able to click on a bar and run a specific report. However, there are 12 bars on the graph, and each would run a different report. We can add the window.open function, but we do not want to hard-code the report URLs. Can we get the ID of the report based on name by only using VisualForce? (Please ignore the duplication of code, we're testing for a variety of fields to add to the bars)

<apex:page standardController="Engagement__c">
    <head>

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
  google.charts.load('current', {packages: ['corechart']});
  google.charts.setOnLoadCallback(drawChart);

 function drawChart() {
var data = new google.visualization.DataTable();

data.addColumn('string', 'Status');
data.addColumn('number', 'Record Count');

data.addRow(['Initial Contact',  {v:{!Engagement__c.Sum_of_Party_Initial_Contact__c}}]);
data.addRow(['Marketing Material Sent', {v:{!Engagement__c.Sum_of_Party_Marketing_Material_Sent__c}}]);
data.addRow(['IOIs Received',  {v:{!Engagement__c.Sum_of_Party_IOIs_Received__c}}]);
data.addRow(['MPs Attended',  {v:{!Engagement__c.Sum_of_Party_MPs_Attended__c}}]);
data.addRow(['Initial Contact',  {v:{!Engagement__c.Sum_of_Party_Initial_Contact__c}}]);
data.addRow(['Marketing Material Sent', {v:{!Engagement__c.Sum_of_Party_Marketing_Material_Sent__c}}]);
data.addRow(['IOIs Received',  {v:{!Engagement__c.Sum_of_Party_IOIs_Received__c}}]);
data.addRow(['MPs Attended',  {v:{!Engagement__c.Sum_of_Party_MPs_Attended__c}}]);
data.addRow(['Initial Contact',  {v:{!Engagement__c.Sum_of_Party_Initial_Contact__c}}]);
data.addRow(['Marketing Material Sent', {v:{!Engagement__c.Sum_of_Party_Marketing_Material_Sent__c}}]);
data.addRow(['IOIs Received',  {v:{!Engagement__c.Sum_of_Party_IOIs_Received__c}}]);
data.addRow(['MPs Attended',  {v:{!Engagement__c.Sum_of_Party_MPs_Attended__c}}]);
data.addRow(['Initial Contact',  {v:{!Engagement__c.Sum_of_Party_Initial_Contact__c}}]);
data.addRow(['Marketing Material Sent', {v:{!Engagement__c.Sum_of_Party_Marketing_Material_Sent__c}}]);
data.addRow(['IOIs Received',  {v:{!Engagement__c.Sum_of_Party_IOIs_Received__c}}]);
data.addRow(['MPs Attended',  {v:{!Engagement__c.Sum_of_Party_MPs_Attended__c}}]);

   var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
        chart.draw(data, {width: 600, height: 200, title: 'Party Summary', 

legend:'none',
        vAxis: {minValue:0}, hAxis: {slantedTextAngle:45}});


//REQUEST ID OF REPORT BY NAME
         var ReportID = database.query('SELECT Id FROM Report where DeveloperName = 'Engagement_Parties'');       

            var selectHandler = function(e) {
                 var selectedItem = chart.getSelection()[0];
                 if (selectedItem) {
                  var party = data.getValue(selectedItem.row, 0);
                 }
               if (party == 'Initial Contact'){

//PASS REPORT ID TO window.open FUNCTION
             window.open ('https://rwbaird2--pocpartial.cs22.my.salesforce.com/' + ReportID +'?pv0={!Engagement__c.Id}');
                 }
              }

       google.visualization.events.addListener(chart, 'select', selectHandler);
};
</script>
        </head>
    <body>
<div id="chart_div" />
</body>
    </apex:page>
1
  • It sounds like you specifically want to avoid using Apex?
    – Adrian Larson
    May 25, 2016 at 17:28

3 Answers 3

1

If you are looking to avoid Apex, you can use the AJAX Toolkit. Take a look at Connecting to the API to help you get started. The next page (Embedding API Calls in Javascript) also shows how you can run a query:

result = sforce.connection.query(
    "SELECT Id FROM Report WHERE DeveloperName = 'Engagement_Parties'"
);
records = result.getArray("records");

for (var i=0; i< records.length; i++) {
  var record = records[i];
  log(record.Name + " -- " + record.Id);
}

Not the cleanest JS ever written, but it should get you started.

0

You can query the id of the report using SOQL inside your visualforce controller and probably store the names of the reports inside a custom setting or custom metadata.

SELECT Name, DeveloperName, Description, NamespacePrefix, LastRunDate FROM Report
5
  • Thank you, but we are trying to avoid a custom controller just to get the report IDs.
    – HomerJ
    May 25, 2016 at 17:32
  • 1
    if the reports are fixed, then you can store the report id's in Custom Labels and refer them directly in visualforce code. That way you need not write apex code.
    – Cloud Man
    May 25, 2016 at 18:36
  • or write javascript
    – Cloud Man
    May 25, 2016 at 19:00
  • I like the idea of using custom labels. Can you expand on that option? I'm not familiar.
    – HomerJ
    May 25, 2016 at 19:38
  • 1
    you can create custom labels from setup menu Setup => Create => Custom Labels and refer to it directly as a formula expression in visualforce. For example: {!$Label.LABEL_NAME} More Info here : developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/…
    – Cloud Man
    May 25, 2016 at 19:51
0

We ended up using the Custom Labels functionality for easy access to the IDs in question. So the link/window looks like this.

window.open ('https://rwbaird2--pocpartial.cs22.my.salesforce.com/{!$label.report1}?pv0={!Engagement__c.Id}');

Thanks to all for the help.

6
  • 1
    Careful to make sure you craft the link so it works in other environments. cs22 won't work in prod.
    – Adrian Larson
    May 26, 2016 at 14:41
  • 1
    Indeed, what @AdrianLarson said. Use URLFOR: window.open ("{!URLFOR($label.report1 + '?pv0=' + Engagement__c.Id)}"); and it should work across all environments May 26, 2016 at 15:33
  • Thanks for the reminder. This is just a POC so there are a couple additional 'deployable' code changes we'll make as well as the URL change. BTW, in Salesforce1, the link opens a new browser window instead of in the app. Is that an easy correction in the code?
    – HomerJ
    May 27, 2016 at 1:17
  • 1
    Just do sforce.one.navigateToUrl instead of window.open. You need to check if sforce is defined though and use your current approach as a fallback.
    – Adrian Larson
    May 31, 2016 at 16:20
  • 1
    Sounds like you should ask a new question!
    – Adrian Larson
    Jun 2, 2016 at 21:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .