Pages

Showing posts with label Metadata. Show all posts
Showing posts with label Metadata. Show all posts

Friday, December 17, 2021

Salesforce: Retrieve Report Metadata

Here is the use case, you are presented with a report with duplicate fields label, whether from the same object or different objects, but how can you easily identify each field? See a sample from the below screenshot, there is no way to tell which Created By and Created Name are from which object.


Even by editing the report, it will not tell you, of course, you can remove and add the fields back, but you are supposed not to change the report.


The answer is to check the metadata of that report, unfortunately, Salesforce does not offer to get this without using tools, such as VS Code or other IDE. But, we can use Workbench to retrieve it.

You can follow this blog to prepare the package.xml file, and retrieve the metadata using Workbench.

1. Get the "fullName"


The report should not be stored in the Private Reports folder, otherwise, you will not see the report here.

2. Prepare XML file

Copy the following XML and save it as a file called "package.xml"

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>Johan_Change/Opty_and_Acct</members>
        <members>A_folder/Opportunity_by_Stage</members>    
        <name>Report</name>
    </types>
    <version>53.0</version>
</Package>

If you would like to retrieve metadata from multiple reports, you can add them to the XML file.

What is this mean <members>Johan_Change/Opty_and_Acct</members>

Text in red is the report folder API name, you can get the report folder API name from Folder Unique Name from rename folder. While the blue text is the report API name, you can get the report API name from the Report Unique Name in the report properties.

If the folder is Public Reports, use unfiled$public as the report API folder name in the Package.XML file, also you will not see that folder in Workbench step (1).


3. Retrieve Package

Open workbench:
  • select migration | Retrieve
  • select the XML file prepared in step 2
  • select "Single Package"
  • click the Next button then click the Retrieve button
  • when done, click the "Download ZIP File" link
  • extract the zip file and looks for the report
  • open the file with any text editor

<?xml version="1.0" encoding="UTF-8"?>
<Report xmlns="http://soap.sforce.com/2006/04/metadata">
    <columns>
        <field>Opportunity$Name</field>
    </columns>
    <columns>
        <field>Opportunity$Account</field>
    </columns>
    <columns>
        <field>Opportunity$CreatedBy</field>
    </columns>
    <columns>
        <field>Opportunity$CreatedDate</field>
    </columns>
    <columns>
        <field>Opportunity$Account.CreatedBy</field>
    </columns>
    <columns>
        <field>Opportunity$Account.CreatedDate</field>
    </columns>
    <currency>EUR</currency>
    <format>Tabular</format>
    <name>Opty and Acct</name>
    </params>
    <reportType>Opty_and_Account__c</reportType>
    <roleHierarchyFilter>CEO</roleHierarchyFilter>
    <scope>organization</scope>
    <showDetails>true</showDetails>
    <showGrandTotal>true</showGrandTotal>
    <showSubTotals>true</showSubTotals>
    <timeFrameFilter>
        <dateColumn>Opportunity$CloseDate</dateColumn>
        <interval>INTERVAL_CUSTOM</interval>
    </timeFrameFilter>
</Report>

The report metadata tell us that the 1st and 2nd field are from Opportunity, while the 3rd and 4th fields are from Account.

Here is the list of Metadata Type supported by Metadata API: https://developer.salesforce.com/docs/metadata-coverage

Some metadata types support using wildcard character (*), so you can retrieve all items without the need to mention each member name: https://developer.salesforce.com/docs/atlas.en-us.208.0.api_meta.meta/api_meta/meta_types_list.htm 

As you see from the above URL, Report and ListView are No, but Profile and PermissionSet are Yes.
Sample XML file:

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>*</members>
        <name>PermissionSet</name>
    </types>
    <types>
        <members>*</members>
<name>Profile</name>
    </types>
    <version>53.0</version>
</Package>


Tuesday, May 30, 2017

Salesforce: List View Sharing

As per API 39.0, looking at ListView object, we can't retrieve who can access a list view with a simple SOQL query. But, looking at this metadata API documentation, we can retrieve who can access a list view using sharedTo field. We can use a SOQL query for this, as this is metadata API.

Here is the step by step to retrieve that info, you will need to use Workbench.
Let's say we would like to check who can access a list view called "Account start A".

1. Get the "fullName"
From Workbench:
- select Info | Metadata Types & Components
- select Listview from the dropdown
- Click Expand all and find the view name
- if the view is "Visible only to me", you will not find it here


2. Prepare XML file
Copy following XML and save it to a file called "package.xml"
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>Account.Account_start_A</members>
        <name>ListView</name>
    </types>
    <version>39.0</version>
</Package>

3. Retrieve Package
Open workbench again:
- select migration | Retrieve
- select the XML file prepared in step 2
- tick "Single Package"
- Click the Next button
- Click Retrieve button
- when done click the Download ZIP File link
- extract the zip file
- look for the object folder
- in my case, this is Account object, so open the "Account.object" file with any text editor
- here is the result, you will easily see who has access to the view

<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <listViews>
        <fullName>Account_start_A</fullName>
        <columns>ACCOUNT.NAME</columns>
        <columns>ACCOUNT.SITE</columns>
        <columns>ACCOUNT.ADDRESS1_STATE</columns>
        <columns>ACCOUNT.PHONE1</columns>
        <columns>ACCOUNT.TYPE</columns>
        <columns>CORE.USERS.ALIAS</columns>
        <filterScope>Everything</filterScope>
        <filters>
            <field>ACCOUNT.NAME</field>
            <operation>startsWith</operation>
            <value>A</value>
        </filters>
        <label>Account start A</label>
        <language>en_US</language>
        <sharedTo>
            <group>Coba_Group</group>
            <role>CEO</role>
            <role>CFO</role>
            <role>COO</role>
        </sharedTo>
    </listViews>
</CustomObject>


Let's extend this to retrieve for many list views, modify the package XML file as below:

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>Account.Account_start_A</members>
<members>Account.NewThisWeek</members>
<members>Account.RT_Ke2</members>
        <name>ListView</name>
    </types>
    <version>39.0</version>
</Package>

This is the sample result:
<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <listViews>
        <fullName>Account_start_A</fullName>
        <columns>ACCOUNT.NAME</columns>
        <columns>ACCOUNT.SITE</columns>
        <columns>ACCOUNT.ADDRESS1_STATE</columns>
        <columns>ACCOUNT.PHONE1</columns>
        <columns>ACCOUNT.TYPE</columns>
        <columns>CORE.USERS.ALIAS</columns>
        <filterScope>Everything</filterScope>
        <filters>
            <field>ACCOUNT.NAME</field>
            <operation>startsWith</operation>
            <value>A</value>
        </filters>
        <label>Account start A</label>
        <language>en_US</language>
        <sharedTo>
            <group>Coba_Group</group>
            <role>CEO</role>
            <role>CFO</role>
            <role>COO</role>
        </sharedTo>
    </listViews>
    <listViews>
        <fullName>NewThisWeek</fullName>
        <columns>ACCOUNT.NAME</columns>
        <columns>ACCOUNT.CREATED_DATE</columns>
        <columns>Record_Type_Name__c</columns>
        <filterScope>Everything</filterScope>
        <filters>
            <field>ACCOUNT.CREATED_DATE</field>
            <operation>equals</operation>
            <value>THIS_WEEK</value>
        </filters>
        <label>New This Week</label>
        <language>en_US</language>
    </listViews>
    <listViews>
        <fullName>RT_Ke2</fullName>
        <columns>ACCOUNT.NAME</columns>
        <columns>ACCOUNT.SITE</columns>
        <columns>ACCOUNT.ADDRESS1_STATE</columns>
        <columns>ACCOUNT.PHONE1</columns>
        <columns>ACCOUNT.TYPE</columns>
        <columns>CORE.USERS.ALIAS</columns>
        <filterScope>Everything</filterScope>
        <filters>
            <field>ACCOUNT.RECORDTYPE</field>
            <operation>equals</operation>
            <value>Account.AccRtKe2</value>
        </filters>
        <label>RT Ke 2 Manual</label>
        <language>en_US</language>
        <sharedTo>
            <group>All_Internal_User_Group</group>                               <roleAndSubordinates>ChannelSalesTeam</roleAndSubordinates>
        </sharedTo>
    </listViews>
</CustomObject>

From the XML returned:
- the 1st view "Account_start_A" is shared to 1 group and 3 roles
- the 2nd view "NewThisWeek" is visible to all users, therefore there is no sharedTo tag
- the 3rd view, "RT_Ke2" is shared to 1 group and 1 role with subordinates.

Let's randomly confirm if the return is correct:



Deploy change
So for this sample, you work on the Account.object, let's say you remove and add "group" or "role" from sharedTo. To deploy this back to Salesforce, you just need to zip the file back as the same structure as you originally downloaded the zip file.

Open Workbench, navigate to migration | Deploy, and select "Single Package".




Additional notes:
With REST explorer from Workbench, we can get data of list view, example:
- to get all list view in Account: /services/data/v39.0/sobjects/Account/listviews/

- to get list view data from a list view:
/services/data/v39.0/sobjects/Account/listviews/00B50000008DouiEAC/results

* 00B50000008DouiEAC is ListView ID


Last updated: 4-Mar-2019


Reference:
Metadata API Developer Guide - ListView
Force.com REST API Developer Guide - List Views




Page-level ad