function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ViplavViplav 

How to compare values present in custom metadata to List of type string ?

Hi Everyone,

I have created a custom metadata and have kept three values in it. Consider those values as- 
'God'
'Is'
'Great'

Now, I have a list of type string which can either have these three values or altogether different values. I want to compare the custom metadata values agaist this list but I am not able to do that using Apex code. 
Is there a way I can compare String to a custom metadata list.

List <custom_metadataname__mdt> SNamefromCsetting = [Select sname__c from custom_metadataname__mdt];
System.Debug('SNamefromCsetting :' +SNamefromCsetting );
String[] st = Name.split(' , ');



 
Best Answer chosen by Viplav
v varaprasadv varaprasad
Hi Viplav,

Please check the sample code below : 
 
List <custom_metadataname__mdt> SNamefromCsetting = [Select sname__c from custom_metadataname__mdt];
System.Debug('SNamefromCsetting :' +SNamefromCsetting );

list<string> firstlist = new list<string>();
list<string> secondlist = new list<string>{'god','test','great'};



for(custom_metadataname__mdt md : SNamefromCsetting){
  firstlist.add(md.sname__c);
}



for(string s : firstlist){
     if(secondlist.contains(s) ){
          system.debug('==matched string'+s);
     }else{
	    system.debug('not matched=='+s);
	 }
}



Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For  Support: varaprasad4sfdc@gmail.com
Blog : http://salesforceprasad.blogspot.com/



 

All Answers

v varaprasadv varaprasad
Hi Viplav,

Please check the sample code below : 
 
List <custom_metadataname__mdt> SNamefromCsetting = [Select sname__c from custom_metadataname__mdt];
System.Debug('SNamefromCsetting :' +SNamefromCsetting );

list<string> firstlist = new list<string>();
list<string> secondlist = new list<string>{'god','test','great'};



for(custom_metadataname__mdt md : SNamefromCsetting){
  firstlist.add(md.sname__c);
}



for(string s : firstlist){
     if(secondlist.contains(s) ){
          system.debug('==matched string'+s);
     }else{
	    system.debug('not matched=='+s);
	 }
}



Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For  Support: varaprasad4sfdc@gmail.com
Blog : http://salesforceprasad.blogspot.com/



 
This was selected as the best answer
ViplavViplav
Hi Varaprasad,

The solution worked perfectly for me. :) Thanks a ton for giving such quick response

Thanks,
Viplav