1

I query a custom metadata type value and try to use it in IN clause.

  • Scenario 1: Works!

Custom Metadata value has AccountHandlerTest ( not in single quotes). enter image description here

ACustomMetaDatatype__mdt custMeta =[select id,Value__c from ACustomMetaDatatype__mdt where developername='ARecord'];

String[] ss = new String[]{custMeta.Value__c};
ApexClass[] testClasses = [SELECT Id FROM ApexClass WHERE Name in :ss];

|SOQL_EXECUTE_END|[3]|Rows:1
  • Scenario 2: Does'nt Work

Custom Metadata value has AccountHandlerTest','CaseHandlerTest enter image description here

ACustomMetaDatatype__mdt custMeta =[select id,Value__c from ACustomMetaDatatype__mdt where developername='ARecord'];

String[] ss = new String[]{custMeta.Value__c};
ApexClass[] testClasses = [SELECT Id FROM ApexClass WHERE Name in :ss];

|SOQL_EXECUTE_END|[3]|Rows:0

I had AccountHandlerTest','CaseHandlerTest format so that I get a result like - 'AccountHandlerTest','CaseHandlerTest', but unfortunately that is not expected.

Any suggestions?

1

Instead of storing values in format like this AccountHandlerTest' , 'CaseHandlerTest

You can use comma separated values and then use split() function to convert it into list

like this:

ACustomMetaDatatype__mdt custMeta =[select id,Value__c from ACustomMetaDatatype__mdt where developername='ARecord'];

String[] ss = custMeta.Value__c.split(',');
ApexClass[] testClasses = [SELECT Id FROM ApexClass WHERE Name in :ss];
1

This is the issue you are facing value comes out a single string 'AccountHandlerTest','CaseHandlerTest'

you have to split the string and push into the list and query the list

classes__mdt custMeta =[select id,Value__c from classes__mdt];
List<string> ss = new List<string>();
for(string s: custMeta.value__c.split(',')){
    ss.add(s);
}
ApexClass[] testClasses = [SELECT Id FROM ApexClass WHERE Name in :ss];
  • 1
    Oops didn't realize you already answered it – salesforce Developer Nov 10 '17 at 17:53
  • Thank you both. I see user entering the value as AccountHandlerTest, CaseHandlerTest makes more sense than AccountHandlerTest','CaseHandlerTest – SfdcBat Nov 10 '17 at 17:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.