Play Games

Search This Blog

Tuesday, February 15, 2022

How to create custom metadata type records from apex - Salesforce Globe For You

Solution:

Step1 : create a class which implements DeployCallback interface. HandleResult() needs to be implemented.

public class CreateMetadataController implements Metadata.DeployCallback {

    public void handleResult(Metadata.DeployResult result, Metadata.DeployCallbackContext callbackContext) {

        if(result.status == Metadata.DeployStatus.Succeeded) {

            system.debug('Custom metadata record creation is successful: '+result);

        } else {

            system.debug('Custom metadata record creation failed: '+result);

        }

    }

}

Step 2: create any method and write the logic to deploy inside it.

public void createCustomMetadataRecord() {

//2.1 create the custom metadata record to be inserted

//In this example Country_Code__mdt metadata record will be inserted

Metadata.CustomMetadata objCustomMetadata =  new Metadata.CustomMetadata();

objCustomMetadata.fullName = 'Country_Code__mdt.India';// custom metadata object api name.name

objCustomMetadata.label = 'India';

//2.2 add custom fields using CustomMetadataValue

Metadata.CustomMetadataValue objCustomField1 = new Metadata.CustomMetadataValue();

objCustomField1.field = 'Code__c';

objCustomField1.value = 'IND';

objCustomMetadata.values.add(objCustomField1);

//2.3 add custom metadata to the DeployContainer

Metadata.DeployContainer objContainer = new Metadata.DeployContainer();

objContainer.addMetadata(objCustomMetadata);

//2.4 deploy the metadata using Metadata.Operations.enqueueDeployment()

Metadata.Operations.enqueueDeployment(objContainer,new CreateMetadataController);

}

overall apex class

public class CreateMetadataController implements Metadata.DeployCallback {

    public void handleResult(Metadata.DeployResult result, Metadata.DeployCallbackContext callbackContext) {

        if(result.status == Metadata.DeployStatus.Succeeded) {

            system.debug('Custom metadata record creation is successful: '+result);

        } else {

            system.debug('Custom metadata record creation failed: '+result);

        }

    }

    public static void createCustomMetadataRecord() {

        Metadata.CustomMetadata objCustomMetadata =  new Metadata.CustomMetadata();

        objCustomMetadata.fullName = 'Country_Code__mdt.India';

        objCustomMetadata.label = 'India';

        

        Metadata.CustomMetadataValue objCustomField1 = new Metadata.CustomMetadataValue();

        objCustomField1.field = 'Code__c';

        objCustomField1.value = 'IND';

        objCustomMetadata.values.add(objCustomField1);

        

        Metadata.DeployContainer objContainer = new Metadata.DeployContainer();

        objContainer.addMetadata(objCustomMetadata);

                Metadata.Operations.enqueueDeployment(objContainer,new CreateMetadataController());

    }

}

Step 3: call the above method from developer console

CreateMetadataController.createCustomMetadataRecord();

Output:





No comments:

Post a Comment