Sunday, 30 June 2019

Load or Update Records with the Custom Metadata Loader

Hi,
Usually, we can't insert or update Custom Metadata records using Apex with normal DML statements.

We have custom meta loader to insert or update multiple records into Custom Metadata.


The custom metadata loader lets you load or update up to 200 records with a single call.

  1. Download the tool from GitHub and deploy the package to your org via Workbench. Create the .zip file from the contents of the custom_md_loader directory instead of zipping up the directory itself.
  2. Create a .csv file with a header that contains the custom metadata type’s field API names. Either the Label or the Developer Name field is required. See sample.csv in your download for an example. If your org is namespaced, include the namespace prefix in your header. To update an existing custom metadata record, use the Label or Developer Name field to identify it.
  3. From Setup, assign the Custom Metadata Loader permission set to the appropriate users, including yourself.
  4. From the App Picker, select Custom Metadata Loader.
  5. Click the Custom Metadata Loader tab. If you haven’t already done so, the app prompts you to configure your Remote Site Settings.
  6. Upload your .csv file and select the corresponding custom metadata type.
  7. Click Create/Update custom metadata to bulk-load the records from the .csv file or update existing records. If the file has duplicate Label or Developer Name entries, the last entry becomes the new or updated record.


References:

Saturday, 22 June 2019

Custom Metadata Record creation in apex

Hi,

Here I am going to explain how can we create custom metadata record in apex.

Usually, we can' create custom metadata record through DML statements in apex.


But we have  "Metadata" class to create Custom Metadata record in apex.

When we use this no DML operations happen for creating records. instead, it does the deployment of custom metadata values.

Whenever we want to use this we have to create a call back class which implements "Metadata.DeployCallback" interface as shown below as this action is asynchronous.

Callback Class:
public class MetadataCallbackCls implements Metadata.DeployCallback {
    public void handleResult(Metadata.DeployResult result,
                             Metadata.DeployCallbackContext context) {
        if (result.status == Metadata.DeployStatus.Succeeded) {
            System.debug('success: '+ result);
        } else {         
            System.debug('fail: '+ result);
        }
    }
}


Example Snippet for creating Custom Meta Data:


Let's take custom metadata type "User_Creds__mdt" which has a custom field "Password__c".

To insert a record into the same we have to follow the same below.

    Metadata.CustomMetadata customMetadataObj =  new Metadata.CustomMetadata();
    customMetadataObj .fullName = 'User_Creds__mdt.Balaji_M';
    customMetadataObj .label = 'Balaji_M';

    Metadata.CustomMetadataValue customFieldObj = new Metadata.CustomMetadataValue();
    customFieldObj .field = 'Password__c';
    customFieldObj .value = 'XXXXX';

    customMetadataObj.values.add(customFieldObj);

    Metadata.DeployContainer containerObj= new Metadata.DeployContainer();
    containerObj.addMetadata(customMetadataObj);


    MetadataCallbackCls  callback = new MetadataCallbackCls ();
 
    Id jobId = Metadata.Operations.enqueueDeployment(containerObj, callback);


Output:




References:

https://developer.salesforce.com/docs/atlas.en-us.208.0.apexcode.meta/apexcode/apex_class_Metadata_CustomMetadata.htm#apex_class_Metadata_CustomMetadata


https://trailhead.salesforce.com/en/content/learn/modules/apex_metadata_api/apex_metadata_api_updates

https://trailhead.salesforce.com/en/content/learn/modules/custom_metadata_types_adv/cmt_develop

https://developer.salesforce.com/docs/atlas.en-us.208.0.apexcode.meta/apexcode/apex_interface_Metadata_DeployCallback.htm#apex_Metadata_DeployCallback_methods

Mastering Agentforce: The Power of Agent Script

Hi, In the rapidly evolving world of AI, the challenge for developers has always been balancing the creative reasoning of Large Language Mod...