Created
January 26, 2021 00:20
-
-
Save forcethesales/c9e9b62d27bb1855b7cb85ad6a855ff2 to your computer and use it in GitHub Desktop.
invocable method in Flow to Update Custom Metadata Type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://www.biswajeetsamal.com/blog/create-or-update-custom-metadata-type-using-apex/ | |
public class CustomMetadataCallBack implements Metadata.DeployCallback { | |
//Inteface method | |
public void handleResult(Metadata.DeployResult result, Metadata.DeployCallbackContext context) { | |
if (result.status == Metadata.DeployStatus.Succeeded) { | |
//Success | |
} else { | |
//Failed | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://www.biswajeetsamal.com/blog/create-or-update-custom-metadata-type-using-apex/ | |
public class CustomMetadataUtils implements Metadata.DeployCallback { | |
//Interface method | |
public void handleResult(Metadata.DeployResult result, Metadata.DeployCallbackContext context) { | |
if (result.status == Metadata.DeployStatus.Succeeded) { | |
//Success | |
System.debug('Success Result-' + result); | |
} else { | |
//Failed | |
System.debug('Failed Result-' + result); | |
} | |
} | |
//Create Custom Metadata record | |
public static void createCustomMetadata(String metdataName, String label, Map<String, Object> metadataFieldValueMap){ | |
String recordDevName = label.replaceAll(' ', '_'); | |
Metadata.CustomMetadata cMetadata = new Metadata.CustomMetadata(); | |
cMetadata.fullName = metdataName + '.' + recordDevName; | |
cMetadata.label = label; | |
for(String key : metadataFieldValueMap.keySet()){ | |
Metadata.CustomMetadataValue cMetadataValue = new Metadata.CustomMetadataValue(); | |
cMetadataValue.Field = key; | |
cMetadataValue.Value = metadataFieldValueMap.get(key); | |
cMetadata.values.add(cMetadataValue); | |
} | |
Metadata.DeployContainer mdContainer = new Metadata.DeployContainer(); | |
mdContainer.addMetadata(cMetadata); | |
CustomMetadataUtils callback = new CustomMetadataUtils(); | |
Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, callback); | |
} | |
//Update Custom Metadata record | |
public static void updateCustomMetadata(String metdataName, String recordDevName, String label, Map<String, Object> metadataFieldValueMap){ | |
Metadata.CustomMetadata cMetadata = new Metadata.CustomMetadata(); | |
cMetadata.fullName = metdataName + '.' + recordDevName; | |
cMetadata.label = label; | |
for(String key : metadataFieldValueMap.keySet()){ | |
Metadata.CustomMetadataValue cMetadataValue = new Metadata.CustomMetadataValue(); | |
cMetadataValue.Field = key; | |
cMetadataValue.Value = metadataFieldValueMap.get(key); | |
cMetadata.values.add(cMetadataValue); | |
} | |
Metadata.DeployContainer mdContainer = new Metadata.DeployContainer(); | |
mdContainer.addMetadata(cMetadata); | |
CustomMetadataUtils callback = new CustomMetadataUtils(); | |
Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, callback); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//primary source https://www.biswajeetsamal.com/blog/create-or-update-custom-metadata-type-using-apex/ | |
//I update a custom metadata type from Flow with an invocable action because I can't do it directly. | |
public class invokeUnsubscribeJan2021 { | |
//Flow screen takes information and passes to this invocable method | |
@InvocableMethod (label='Create Unsubscribe Record Jan2021'description='CreateUnsubscribe') | |
//pass a list of records from the flow, but it's actually just one record | |
//the type of variable being passed is a record variable of a custom metadatatype | |
public static void Unsubscribe(List<Unsubscribe_Link__mdt> recordPassed) { | |
//declare a new map | |
Map<String, Object> metadataFieldValueMap = new Map<String, Object>(); | |
//add values to the map of Field Name on CMT and value passed in from the Flow | |
metadataFieldValueMap.put('Our_Organization__c', recordPassed[0].Our_Organization__c); | |
metadataFieldValueMap.put('Site_Domain__c', recordPassed[0].Site_Domain__c); | |
//call the class and method to update the CMT | |
CustomMetadataUtils.updateCustomMetadata('Unsubscribe_Link__mdt', 'Unsubscribe','Unsubscribe', metadataFieldValueMap); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment