Hey guys, today in this post we are going to learn about How to Assign Custom Metadata value to account using select a picklist value and update textarea in Apex trigger
Salesforce.
Custom metadata is metadata that can be customized, deployed, packaged, and upgraded.
Records in custom metadata type are considered as metadata rather than data and we can deploy records of Custom Metadata Type with Change Set and other deployment tools.
We can issue an unlimited number of Salesforce Object Query Language (SOQL) queries for each Apex transaction using custom metadata types. To know more details about Custom Metadata, Click Here..
Live Example
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
- Find the below steps ▾
Create Apex Trigger
Step 1:- Create Apex Trigger : AccountTrigger.apxt
From Developer Console >> File >> New >> Apex Trigger
AccountTrigger.apxt [Apex Class Controller]
![]() |
TRIGGER AccountTrigger ON Account (BEFORE INSERT, after INSERT, BEFORE UPDATE, after UPDATE, BEFORE DELETE, after DELETE ) {
IF(TRIGGER.isBefore){
System.debug('I am i side before trigger');
}ELSE IF(TRIGGER.isAfter){
System.debug('I am i side after trigger');
IF(TRIGGER.isInsert)
{
System.debug('I am i side after insert trigger');
AccountTriggerHandler.insertAccAddress(TRIGGER.new);
}ELSE IF (TRIGGER.isUpdate){
AccountTriggerHandler.addressUpdate(TRIGGER.new, TRIGGER.oldMap);
}
}
}
Apex Trigger Handler Class
Step 2:- Create Apex Trigger : AccountTriggerHandler.apxc
From Developer Console >> File >> New >> Apex Trigger
AccountTriggerHandler.apxc [Apex Class Controller]
![]() |
public class AccountTriggerHandler {
public static void addressUpdate(List<Account> accList, Map<Id, Account> oldMap){
Map<String, Address_Location__mdt> mtdMap = NEW Map<String, Address_Location__mdt>();
List<Address_Location__mdt> mtData = [SELECT Id, MasterLabel, Office_Address__c FROM Address_Location__mdt];
FOR(Address_Location__mdt m:mtData){
mtdMap.put(m.MasterLabel, m);
}
system.debug('mtdMap## ' + mtdMap);
List<Account> accItemList = NEW List<Account>();
FOR(Account a:accList){
IF(mtdMap.containsKey(a.Office_Location__c) && a.Office_Location__c != oldMap.get(a.Id).Office_Location__c){
Account ac = NEW Account();
ac.Id = a.Id;
ac.Address__c = mtdMap.get(a.Office_Location__c).Office_Address__c;
accItemList.add(ac);
}
}
IF(!accItemList.isEmpty()){
UPDATE accItemList;
}
}
public static void insertAccAddress(List<Account> quoteList){
//FOR InsertQuoteAddress
system.debug('aaaaaa');
Set<String> setStr = NEW Set<String>();
Set<String> metaStr = NEW Set<String>();
Set<Id> setId = NEW Set<Id>();
Map<String, Address_Location__mdt> accMap = NEW Map<String, Address_Location__mdt>();
List<Address_Location__mdt> metaList = NEW List<Address_Location__mdt>();
FOR(Account qu:quoteList){
setId.add(qu.Id);
setStr.add(qu.Address__c);
}
List<Address_Location__mdt> quoteLocationList = [SELECT Id, MasterLabel, Office_Address__c FROM Address_Location__mdt];
FOR(Address_Location__mdt mdt:quoteLocationList){
accMap.put(mdt.MasterLabel, mdt);
}
system.debug('accMap@@@11 ' + accMap);
List<Account> accObj = NEW List<Account>();
FOR(Account q1:quoteList){
IF(accMap.containskey(q1.Office_Location__c)){
Account qut = NEW Account();
qut.Address__c=accMap.get(q1.Office_Location__c).Office_Address__c;
qut.Id = q1.Id;
accObj.add(qut);
system.debug('qqqqq##22 ' + accObj);
}
}
IF(!accObj.isEmpty()){
UPDATE accObj;
}
}
}
Further post that would you like to learn in Salesforce
Can we update custom metadata using apex?
we can Create, Read and Update Records of Custom Metadata but the Delete operation is not possible. DML operation on custom metadata from apex code is prohibited instead we use the deployment of custom metadata records.
Can we use custom metadata in trigger?
A trigger is Apex code that executes before or after the following types of operations: insert, update, delete, merge, upsert, undelete. and Custom Metadata: Is customizable, deployable, packageable, and upgradeable application metadata.
How do I call custom metadata in Apex?
Use the Apex getAll(), getInstance(recordId), getInstance(qualifiedApiName), and getInstance(developerName) methods to retrieve information from custom metadata type records faster.
Related Topics | You May Also Like
Our Free Courses →
👉 Get Free Course →
![]() 📌 Salesforce Administrators 📌 Salesforce Lightning Flow Builder 📌 Salesforce Record Trigger Flow Builder |
👉 Get Free Course →
![]() 📌 Aura Lightning Framework 📌 Lightning Web Component (LWC) 📌 Rest APIs Integration |