We have a Custom Metadata Type which shows the relation between a product of interest and categories. When a product of interest is selected we would like the corresponding categories to be selected. If it is removed, we'd like the corresponding categories to be removed. A Trigger will be used that will execute the needed apex method(s).

So far my blocker is getting a lead's selected product(s) to compare to the custom metadata type.

Everything I've researched only gets me the options not the selected option(s). Options refer to the items that could be selected in the multi-select picklist. The selected options refer to the items on a lead that are already selected on the picklist. The option(s) were achieved using the code below, but I am unable to see which item(s) are selected this way.

I'm trying to find a way to take a lead's Id and check which options that are selected in the lead's multi-select picklist.

I've taken a different approach. The new approach is labeled "Edited" and the old is labeled "Origianl"

Given the change in approach, I now need to take the data I've collected and map it together into whatever data type that can be use with update for the lead.


Products_of_Interest_Categories__mdt:{Id=xxx, Product_of_Interest__c='POI Name', Category_xxx__c=false, Category_xxy__c=true, Category_XX_YYY__c=false,...}

MISSING PART OF updateLeadCategories: If POI Name, Category_xxy__c = true. On Lead, check Category_xxy__c.

public with sharing class ProductOfInterestService {

public static List<String> getCategories(){
    List<String> poiCategories = new List<String>();
    // This is the object for which we required data.
    Map<String, Schema.SObjectField> fieldMap = Products_of_Interest_Categories__mdt.sObjectType.getDescribe().fields.getMap();

    // Get all of the fields on the object
    Set<String> fieldNames = fieldMap.keySet();

    // Remove All But Categories
    for(String fieldName : fieldNames){
        If(fieldName.equalsIgnoreCase('DeveloperName') || fieldName.equalsIgnoreCase('MasterLabel') || fieldName.equalsIgnoreCase('Language') || fieldName.equalsIgnoreCase('NamespacePrefix') || fieldName.equalsIgnoreCase('Label') || fieldName.equalsIgnoreCase('QualifiedApiName')){
    poiCategories = new List<String>(fieldNames);

    return poiCategories;

public static List<String> getLeadPOI(Id leadId){
    Lead lead = [Select Id, Product_of_interest__c From Lead Where Id =:leadId];
    List<String> selectedPOI = lead.Product_of_interest__c.split(';');
    if(selectedPOI.isEmpty() && selectedPOI.size() > 1){
        system.debug('No POI Selected');
    System.debug('TEST!!!! ' + selectedPOI);
    return selectedPOI;

public static void updateLeadCategories(Lead lead){
    Map<String,String> compareMap = new Map<String,String>();
    Id leadId = lead.Id;
    List<String> possibleCategories = getCategories();
    List<String> leadPOI =  getLeadPOI(leadId);
    List<Products_of_Interest_Categories__mdt> categories = new List<Products_of_Interest_Categories__mdt>();
    categories = Database.query('select ' + string.join(possibleCategories, ',') + ' from Products_of_Interest_Categories__mdt');
    for(Products_of_Interest_Categories__mdt category : categories){
        //Update Logic
        System.debug('TEST Category ' + category);



public static List<String> getLeadPOIOptions(){
   // List<Products_of_Interest_Categories__mdt> categories = new List<Products_of_Interest_Categories__mdt>();
    Schema.DescribeSObjectResult objSchema = Schema.Lead.sObjectType.getDescribe();
    Map<String, Schema.SObjectField> fieldMap = objSchema.fields.getmap();
    List<Schema.Picklistentry>fld =fieldmap.get('Product_of_interest__c').getDescribe().getpicklistValues();
    List<String> leadPOI = new List<String>();

    for(Schema.Picklistentry pl : fld){
    System.debug('pickList '+ leadPOI);
    return leadPOI;

Now trying to use something similar to get the selected POI.

public static void getLeadPOI(Id leadId){
    Lead lead = [Select Id, Product_of_interest__c From Lead Where Id =:leadId];
    List<String> leadPOIOptions = getLeadPOIOptions();
    List<String> selectedPOI = new List<String>();
    for(String option : leadPOIOptions){
        If(option == lead.Product_of_interest__c.contains(option)){
  • Welcome to SFSE! Have you taken the tour and read How do I ask a good question? Could you please share some code of what have you achieved so far? – Przemysław Tamoń Jan 14 at 10:32
  • I'm having a hard time following. In schema terms, what are the "options" and "selected options"? In schema terms, what data are you trying to change, and what does the Apex trigger have to do with this code? Can you edit to be more specific about what you're not able to find? – David Reed Jan 14 at 15:15
  • if(selectedPOI.isEmpty() && selectedPOI.size() > 1){ will never be true; and I agree with David - a schema of the MDT would be useful as it looks like there are many columns – cropredy Jan 15 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.