Code Monkey home page Code Monkey logo

force-cmdt's Introduction

Managing & Testing Custom Metadata

Managing custom metadata & custom metadata records via the Salesforce UI is tiedious at best, especially if the number of records is large.

Alternative - You can build UI using Flow or LWC but you will need to jump through a number of complex Apex hoops using Metadata.CustomMetadat & Metadata.DeployCallback to get the job done.

Best Path - Or you can pull the records into a CSV and use the sfdx-cli to prepare them to be pushed - easy as that

force:cmdt COMMAND

create and update custom metadata types and their records

USAGE

 sfdx force:cmdt:COMMAND

TOPICS

  force:cmdt:field   generate a custom metadata field based on the field type provided
  force:cmdt:record  create and update custom metadata type records

COMMANDS

  force:cmdt:create    creates a new custom metadata type in the current project
  force:cmdt:generate  generates a custom metadata type and all its records for the provided sObject

Baiscs of insert CMT using CLI

Insert cmdt records using CLI

sfdx force:cmdt:record:create -t MyCustomType -n SFDX -l "Salesforce DX" Integration__c=sfdx

Insert cmdt records using CSV

sfdx force:cmdt:record:insert -f cmdt.csv -t MyCustomType

Create new cmdt type

sfdx force:cmdt:create -n Token -l "Crypto Tokens" -p Tokens -d force-app/main/default/objects     

Testing Custom Metadata without any Org Dependency

Switching to an Apex Property and making it testVisible allows for CustomMetadata to be defined and set in the Test Classes (no org dependency) - like magic!

Apex Class Properties

Example Apex Property Pattern

   @testVisible
    public static Map<String, MyCustomType__mdt> getCustomMetadataService {
        get {
            if (getCustomMetadataService == null){
                getCustomMetadataService = new Map<String, MyCustomType__mdt>();
                for (MyCustomType__mdt cmt : [SELECT MasterLabel,
                                                    Integration__c
                                                    FROM MyCustomType__mdt]) {
                        System.debug(LoggingLevel.INFO, cmt);
                                                        getCustomMetadataService.put(cmt.MasterLabel, cmt);
                }

            }
            System.debug(LoggingLevel.INFO, 'Generating CMT Map using an Apex Class Property');
            return getCustomMetadataService;
        }
        set;
    }

Example Apex Test

 @isTest
    static void testCustomMetadataService(){

        // Set my TestVisible getCustomMetadataService using an Automatic Property
        GetCustomMetadataService.getCustomMetadataService = testCMT;

        Test.startTest();
        MyCustomType__mdt myCMT = GetCustomMetadataService.getCustomMetadataService.get('Test');
        System.debug(LoggingLevel.INFO, 'My Custom Metadata record for testing:'+myCMT);
        Test.stopTest();
        // Can I assert my Test CustomMetadata?
        System.assertEquals('Test',myCMT.Integration__c,'Something went wrong!');

    }

 
    private static Map<String, MyCustomType__mdt> testCMT {
        get {
            return new Map<String, MyCustomType__mdt> {
                       'Test' => ( MyCustomType__mdt ) JSON.deserialize(
                           '{ "MasterLabel" : "Test", "Integration__c" : "Test" }', MyCustomType__mdt.class )
            };
        }
        private set;
    }

Read All About It

force-cmdt's People

Contributors

agentgill avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.