In my last post, i tried to explain a use case for custom metadata and talked about its advantages -
Salesforce Custom Metadata - Usage and Advantages. As promised in this post, i am going to walk you through an example of what I built for this particular custom metadata type.
The use case for this example is detailed in the earlier post.
- To create a custom metadata, go to Setup -> Develop -> Custom Metadata Types.
- Click New Custom Metadata Type button and populate the Label details.
- Also there is a setting "Visibility" that controls if only a managed package can use this metadata type or any Apex code can use it. I set it to an Apex code based on my use case.
Below is the new metadata type i created -
- On drilling into the new metadata type, you can define what all fields are required to hold the metadata information. I configured two custom fields to capture the queue name and the cutoff time fields. So every record in this metadata type will have these two fields populated.
- Now to add actual metadata records, click the Manage SLA Cutoff Settings button. Here I can click New and add a new record which captures the Queue name and the cutoff time in it for 1 queue -
- Similarly I can add as many records as I need. Remember these are metadata records you are adding for the new metadata type you created earlier. So these are available to the Metadata API, change sets, ANT based Force migration tool etc.
- Now that we have setup the configuration records that we can use in your logic, the next logical step is to see how exactly can i use it. Consider you want to use it in an Apex class or trigger, you can do this by using a simple SOQL statement. See below soql that i can use to pull the cutoff time for a particular queue -
- Now lets talk about migration. Remember this is the biggest advantage of having this configuration data as a custom metadata type. Lets say i want to migrate all this queue cutoff information from one sandbox to the next, I can do so using ANT based force migration tool alone. No need for data loader or manual data creation. Below I will show how we can use ANT to retrieve this metadata we just created.
- First let me put together a simple package.xml that can be used to retrieve all custom metadata types -
- Now in the build.xml that will be used by ANT, below target is configured to retrieve the metadata specified by above package.xml -
- Now let me execute the ANT command and then we can check what is retrieved -
- Lets examine what got retrieved, to confirm it is not just the definition of the SLA Cutoff Time metadata type but also the actual metadata records we created earlier to capture the queue to cutoff time configuration information.
There you go, the retrieved metadata has the actual data values we created in the UI. Thus when we deploy this to the next environment, these records are automatically added. So now we need not worry about getting the data records migrated separately!
Hope this was informative and useful!
Comments
Post a Comment