Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • The Ultimate Guide to Data Cleanup Techniques for Salesforce
    • How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI
    • Top Mistakes Developers Make in Salesforce Apex Triggers
    • Introducing Agentforce3 to Salesforce Developers
    • The Ultimate Guide to Apex Order of Execution for Developers
    • How to Handle Bulkification in Apex with Real-World Use Cases
    • How to Confidently Manage Transactions in Salesforce Apex
    • Building a Dynamic Tree Grid in Lightning Web Component
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Saturday, August 2
    • Home
    • Salesforce Platform
      • Architecture
      • Apex
      • Lightning Web Components
      • Integration
      • Flows & Automation
      • Best Practices
      • Questions
      • News
      • Books Testimonial
    • Industries
      • Artificial Intelligence
    • Hire Me
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Downloads
      • Salesforce Release Notes
      • Apex Coding Guidelines
    • About Us
      • Privacy Policy
    • Contact Us
    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 Sahni6 Mins Read

    How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI

    July 28, 2025
    By Dhanik Lal Sahni7 Mins Read

    Top Mistakes Developers Make in Salesforce Apex Triggers

    July 25, 2025
    By Dhanik Lal Sahni14 Mins Read

    The Ultimate Guide to Apex Order of Execution for Developers

    July 20, 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
    SFBenTopDeveloper
    Ranked #4 Salesforce Developer Blog by ApexHours.com
    ApexHoursTopDevelopers
    Categories
    Archives
    Tags
    apex (116) apex best practices (5) apex code best practice (10) apex code optimization (6) Apex logging (4) apex rest (11) apex trigger best practices (6) architecture (22) Asynchronous apex (9) AWS (5) batch apex (10) best code practice (4) code optimization (9) custom metadata types (5) design principle (9) flow (16) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) news (4) optimize apex (5) optimize apex code (6) optimize apex trigger (5) Permission set (4) Queueable (9) queueable apex (4) rest api (23) salesforce (150) salesforce apex (52) salesforce api integration (5) Salesforce Interview Question (5) 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

    MailChimp

    Expert Salesforce Developer and Architect
    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • The Ultimate Guide to Data Cleanup Techniques for Salesforce
    • How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI
    • Top Mistakes Developers Make in Salesforce Apex Triggers
    • Introducing Agentforce3 to Salesforce Developers
    • The Ultimate Guide to Apex Order of Execution for Developers
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • Top 10 Salesforce CRM Trends to Watch in 2025 July 18, 2025
    • Discover the Top 10 Salesforce AppExchange Apps to Boost Productivity July 10, 2025
    • Top 20 Salesforce Data Cloud Interview Questions & Answers for Admins June 5, 2025
    • 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
    Archives
    Categories
    Tags
    apex (116) apex best practices (5) apex code best practice (10) apex code optimization (6) Apex logging (4) apex rest (11) apex trigger best practices (6) architecture (22) Asynchronous apex (9) AWS (5) batch apex (10) best code practice (4) code optimization (9) custom metadata types (5) design principle (9) flow (16) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) news (4) optimize apex (5) optimize apex code (6) optimize apex trigger (5) Permission set (4) Queueable (9) queueable apex (4) rest api (23) salesforce (150) salesforce apex (52) salesforce api integration (5) Salesforce Interview Question (5) 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.