Pages

Tuesday, August 30, 2016

Salesforce: Change Report Type

So, here is the use case: users need to change Report Type used for existing reports. But, can we change Report Type used by report?
Let's analyze, click "Customize" button and there is NO option to change the report type used for the report.


But, is there any workaround? Let's have a look with the report metadata.
 <?xml version="1.0" encoding="UTF-8"?>  
 <Report xmlns="http://soap.sforce.com/2006/04/metadata">  
   <columns>  
     <field>Account$Id</field>  
   </columns>  
   <columns>  
     <field>Account$Name</field>  
   </columns>  
   <columns>  
     <field>Account$Hello__c</field>  
   </columns>  
   <format>Tabular</format>  
   <name>Account with Hello</name>  
   <params>  
     <name>co</name>  
     <value>1</value>  
   </params>  
   <reportType>Account_Only_1__c</reportType>  
   <scope>organization</scope>  
   <showDetails>true</showDetails>  
   <timeFrameFilter>  
     <dateColumn>Account$CreatedDate</dateColumn>  
     <interval>INTERVAL_CUSTOM</interval>  
   </timeFrameFilter>  
 </Report>  
We found it and marked as bold above, is this mean we can just update it?

Next, let's see the Account_Only_1__c report type metadata:
 <?xml version="1.0" encoding="UTF-8"?>  
 <ReportType xmlns="http://soap.sforce.com/2006/04/metadata">  
   <baseObject>Account</baseObject>  
   <category>accounts</category>  
   <deployed>false</deployed>  
   <description>Account Only with Hello</description>  
   <label>Account Only 1</label>  
   <sections>  
     <columns>  
       <checkedByDefault>false</checkedByDefault>  
       <field>Id</field>  
       <table>Account</table>  
     </columns>  
     <columns>  
       <checkedByDefault>false</checkedByDefault>  
       <field>Name</field>  
       <table>Account</table>  
     </columns>  
     <columns>  
       <checkedByDefault>false</checkedByDefault>  
       <field>Country__c</field>  
       <table>Account</table>  
     </columns>  
     <columns>  
       <checkedByDefault>false</checkedByDefault>  
       <field>Hello__c</field>  
       <table>Account</table>  
     </columns>  
     <columns>  
       <checkedByDefault>false</checkedByDefault>  
       <field>CreatedDate</field>  
       <table>Account</table>  
     </columns>  
     <masterLabel>Accounts</masterLabel>  
   </sections>  
 </ReportType>  

That's okay, now let's see the new report type that we would use as replacement, it is called "Account_Only_2__c"
 <?xml version="1.0" encoding="UTF-8"?>  
 <ReportType xmlns="http://soap.sforce.com/2006/04/metadata">  
   <baseObject>Account</baseObject>  
   <category>accounts</category>  
   <deployed>false</deployed>  
   <description>Account Only without Hello</description>  
   <label>Account Only 2</label>  
   <sections>  
     <columns>  
       <checkedByDefault>false</checkedByDefault>  
       <field>Id</field>  
       <table>Account</table>  
     </columns>  
     <columns>  
       <checkedByDefault>false</checkedByDefault>  
       <field>Name</field>  
       <table>Account</table>  
     </columns>  
     <columns>  
       <checkedByDefault>false</checkedByDefault>  
       <field>Country__c</field>  
       <table>Account</table>  
     </columns>  
     <masterLabel>Accounts</masterLabel>  
   </sections>  
 </ReportType>  

Next, go back to the report metadata and update <reportType> tag, from <reportType>Account_Only_1__c</reportType> to <reportType>Account_Only_2__c</reportType>

Save the metadata (in this case I am using MavensMate, the best IDE as David Liu said), hit Ctrl+S to save and will get following error:

The issue here because a custom field called Hello__c is not added to Account_Only_2__c report type, check that metadata above, so this is make sense.

Now, let's change it with other report type where Hello__c field is added, this report type called Account_Only_3__c.
 <?xml version="1.0" encoding="UTF-8"?>  
 <ReportType xmlns="http://soap.sforce.com/2006/04/metadata">  
   <baseObject>Account</baseObject>  
   <category>accounts</category>  
   <deployed>false</deployed>  
   <description>Account Only with Hello</description>  
   <label>Account Only 3</label>  
   <sections>  
     <columns>  
       <checkedByDefault>false</checkedByDefault>  
       <field>Id</field>  
       <table>Account</table>  
     </columns>  
     <columns>  
       <checkedByDefault>false</checkedByDefault>  
       <field>Name</field>  
       <table>Account</table>  
     </columns>  
     <columns>  
       <checkedByDefault>false</checkedByDefault>  
       <field>Hello__c</field>  
       <table>Account</table>  
     </columns>  
     <columns>  
       <checkedByDefault>false</checkedByDefault>  
       <field>CreatedDate</field>  
       <table>Account</table>  
     </columns>  
     <masterLabel>Accounts</masterLabel>  
   </sections>  
 </ReportType>  
Back to the report metadata and update <reportType> tag, from <reportType>Account_Only_1__c</reportType> to <reportType>Account_Only_3__c</reportType>
Hit Ctrl+S to save the report, and we get Success message.


Now, back to the report in Salesforce and check again the report type by click Customize button, make sure to refresh the browser, not just click Customize or Run Report button when the report is already open.



Note: default report option availability such as: "Summarize information by" and  "Timeframe" is depend on the field defined in report type as well.


Conclusion: we can update Record Type used in Report by editing the report metadata, and the replacement report type should have all fields used in the report.