Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • 10 Salesforce Chrome Extensions to Boost Your Productivity
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Sunday, June 1
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Find Referenced Metadata using Salesforce Dependency API

    Find Referenced Metadata using Salesforce Dependency API

    Dhanik Lal SahniBy Dhanik Lal SahniMay 23, 2020Updated:June 11, 20235 Comments4 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Find Referenced Metadata using Salesforce Dependency API
    Share
    Facebook Twitter LinkedIn Pinterest Email

    While developing new enhancement or user story we introduce lot of new features in Salesforce Application. Before doing changes in existing system, we need to analyze referenced objects. We can not easily identify referenced objects.

    Salesforce has introduced new object MetadataComponentDependency in Tooling API to resolve this problem. We can get references of all custom objects like fields, class or lightning components using this new object.

    We can also utilized this object to identify unused metedata so that it can be removed from org to increasing code limit.

    Let us see, how we can get dependency of metadata object. In this blog, I have created Lightning Web Component to show dependency based on selection of type of metadata like field, apex class or lightning component.

    Steps:

    1. Get Custom Field, Class and Lightning Bundle Detail
    2. Get Dependency using Dependency API
    3. Show Dependency on Lightning Web Component
    4. Export Dependency data as CSV

    1. Get Custom Field, Class and Lightning Bundle Detail

    Let us collect detail of custom objects in salesforc org. We can use standard objects like ApexClass, AuraDefinitionBundle and Tooling API objects to get those information.

    a. Custom Fields:

    We can get custom field detail using CustomField tooling object. Field TableEnumOrId of this object is used to check this field belongs to which object. In case of standard object we will get object name like ‘Account’ or ‘Contract’, in case of custom object we will get object id for table name.

    SELECT Id,DeveloperName,TableEnumOrId from CustomField

    As we want to show object as first drop down and then field drop down. We have to get custom object label instead of id from above TableEnumOrId. We can use other Tooling API object CustomObject for this.

    SELECT Id,DeveloperName from CustomObject

    We can relate CustomObject.Id with CustomField.TableEnumOrId to get custom objects’ label.

    We can do this comparison in LWC component to get custom object label.

     // To Get Label for Standard Object
    var obj=objs.find(x => x.Id === this.objectFields[i].TableEnumOrId);
    if(obj!=undefined)
    {
           label=obj.DeveloperName;
    }

    b. Apex Class

    We can get list of apex class using ApexClass object.

    Select Id,Name from ApexClass

    c. Lightning Component

    We have AuraDefinitionBundle object which we can utilized to get lightning component detail.

    Select Id,DeveloperName from AuraDefinitionBundle

    2. Get Dependency using Dependency API

    We can get dependency of any custom object using MetadataComponentDependency object of tooling API. This object is still in beta version but it is available for Developer/Administrator after Summer 20 Release.

    We can use below query in Tooling API to get dependency of any metadata.

    Select MetadataComponentId, MetadataComponentName, RefMetadataComponentName, RefMetadataComponentId,MetadataComponentType from MetadataComponentDependency where RefMetadataComponentId=\'id\''

    Replace id with any metadata entity id.

    3. Show Dependency on Lightning Web Component

    We have got custom metadata from first step and dependency from second step. Let us show those information on Lighting Web Component.

    We can add dropdown to show type of entity like field, apex class and lightning component.

    @api
        get types() {
            return [
                { label: 'Please Select', value: '' },
                { label: 'Apex Class', value: 'apex' },
                { label: 'Lightning Component', value: 'lightning' },
                { label: 'Field', value: 'field' },
            ];
        } 
     <lightning-combobox name="metdataType"
            label="Metdata Type"
            placeholder="Select Type"
            options={types}
            onchange={handleType}>
            </lightning-combobox>

    Based on selection of type of metedata, We can show respective drop-down like Apex Class or Lightning Component or Object and Field.

    We can call apex method using wire api and transform data based on our requirement.

    import getDepdency from '@salesforce/apex/DependencyController.getDepdency';
    getDepdency({ id: objectId})
            .then(data => {
                if (data) {
                    this.fields=data;         
                    this.error = undefined;
                } else if (error) {
                    this.error = error;
                }
    })

    4. Export Dependency data as CSV

    We can download dependency result data as CSV or other format. I have used CSV for downloading data.

    let csvContent = "data:text/csv;charset=utf-8,";
                
    this.fields.forEach(function(rowArray) {
        let row = rowArray.MetadataComponentName+","+rowArray.MetadataComponentType+",";
        csvContent += row + "\r\n";
    });
    var encodedUri = encodeURI(csvContent);
    var link = document.createElement("a");
    link.setAttribute("href", encodedUri);
    link.setAttribute("download", "Dependent.csv");
    document.body.appendChild(link); 
    link.click();

    IMPORTANT STEP:

    We have to call Tooling API from Lightning Web Component so refer post CALL TOOLING API FROM LIGHTNING WEB COMPONENT. You can use Default scope refresh_token full in Auth Provider and Named Credential, if you don’t want to change user.

    Complete Code:

    Lighting Web Component :

    Apex Code:

    Demo Video:

    Salesforce Metadata Dependency Explorer

    References:

    https://releasenotes.docs.salesforce.com/en-us/summer20/release-notes/rn_feature_impact.htm

    https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/tooling_api_objects_metadatacomponentdependency.htm

    https://developer.salesforce.com/docs/component-library/documentation/lwc

    https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/

    Related Posts

    Salesforce DevOps for Developers: Enhancing Code Quality and Deployment Efficiency

    Apex Code Coverage In Custom Object

    Get All Used Custom Metadata Detail

    Find Referenced Metadata using Salesforce Dependency API

    Extract list of all fields from Page Layout

    Field Access Explorer In lightning Web Component

    Call Tooling API from Lightning Web Component

    apex Lightning web component lwc named credential salesforce tooling api
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleExtract list of all fields from Page Layout
    Next Article Get All Used Custom Metadata Detail
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni9 Mins Read

    10 Salesforce Chrome Extensions to Boost Your Productivity

    June 1, 2025
    By Dhanik Lal Sahni4 Mins Read

    How to Build a Generic Modal Window in Lightning Web Component

    May 26, 2025
    By Dhanik Lal Sahni6 Mins Read

    Top 10 Salesforce Flow Features of Salesforce Summer ’25

    May 11, 2025
    View 5 Comments

    5 Comments

    1. Pingback: Get All Used Custom Metadata in Salesforce | SalesforceCodex

    2. shaktivel on November 10, 2022 9:12 pm

      Hi, I have tried your code I am getting error Unable to build Lightning Component source for markup://c:wireGetObjectInfo: Invalid suffix: json.

      Reply
      • Dhanik Lal Sahni on November 16, 2022 6:24 am

        Hello Shaktivel,

        Please try solving using this post https://salesforce.stackexchange.com/questions/340357/unable-to-build-lightning-component-source-for-markup-chelloworldlightningweb

        Thank You,
        Dhanik

        Reply
    3. Greed on August 29, 2023 4:50 pm

      Hi, I tried this but it is now showing this error
      System.JSONException: Malformed JSON: Expected ‘{‘ at the beginning of object
      Class.DependencyController.getDepdency: line 69, column 1
      Class.DependentInfo.parse: line 30, column 1

      Reply
      • Dhanik Lal Sahni on September 3, 2023 5:16 pm

        Hello Greed,

        Please check your response once. Are you getting List or single object? based on response you have to do deserilization. You can find similar solution at https://salesforce.stackexchange.com/questions/178810/malformed-json-expected-at-the-beginning-of-object

        Thank You,
        Dhanik

        Reply
    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • 10 Salesforce Chrome Extensions to Boost Your Productivity
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code analysis (3) code optimization (8) custom metadata types (5) design principle (9) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (65) lightning-combobox (5) lightning-datatable (10) lightning component (31) Lightning web component (63) lwc (52) named credential (8) news (4) optimize apex code (4) optimize apex trigger (3) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (142) salesforce apex (47) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • 10 Salesforce Chrome Extensions to Boost Your Productivity
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code analysis (3) code optimization (8) custom metadata types (5) design principle (9) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (65) lightning-combobox (5) lightning-datatable (10) lightning component (31) Lightning web component (63) lwc (52) named credential (8) news (4) optimize apex code (4) optimize apex trigger (3) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (142) salesforce apex (47) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.