SOQL to Find Custom Labels Where the Value Contains a Specific String
Finding custom labels in Salesforce using SOQL that match a specific value requires understanding how custom labels are stored and accessed. There isn't a direct field containing the label's value; instead, you access it indirectly. This means we can't directly query Label__c.Value__c
(assuming a custom label named Label__c
). We need to utilize the $Label
global variable within SOQL. This is done via a workaround using Apex.
Because direct SOQL querying of custom label values isn't possible, this task is best achieved using Apex code. Below is an Apex solution to find custom labels where the value contains a specified string:
public class FindCustomLabels {
public static List<String> findLabelsWithValue(String searchString) {
List<String> matchingLabels = new List<String>();
List<String> allLabels = new List<String>();
// Get all custom label names (this is the only SOQL part)
for (CustomLabel cl : [SELECT Label, NamespacePrefix FROM CustomLabel]) {
String labelName = cl.Label;
if (String.isNotBlank(cl.NamespacePrefix)) {
labelName = cl.NamespacePrefix + '.' + cl.Label;
}
allLabels.add(labelName);
}
// Iterate and check values using $Label
for (String labelName : allLabels) {
String labelValue = $Label.get(labelName); //This accesses the label value.
if (labelValue.contains(searchString)) {
matchingLabels.add(labelName + ' : ' + labelValue);
}
}
return matchingLabels;
}
}
How it Works:
-
findLabelsWithValue(String searchString)
: This method takes the search string as input. -
Retrieving Label Names: It first queries the
CustomLabel
object using SOQL to get a list of all custom label names. It handles namespaced labels correctly by appending the namespace. -
Iterating and Checking Values: It iterates through the list of label names. For each label name, it uses the
$Label.get(labelName)
to retrieve the actual value of the custom label. Thecontains()
method checks if the label value includes the search string. -
Returning Matching Labels: If a label's value contains the search string, it adds the label name and its value to the
matchingLabels
list, which is then returned.
How to Use:
- Create a new Apex class in your Salesforce org and paste this code into it.
- Create a test method within the Apex class to execute the function with your search string, such as:
@isTest static void testFindLabelsWithValue(){
//Example usage
String searchTerm = 'Example';
List<String> results = FindCustomLabels.findLabelsWithValue(searchTerm);
System.debug(results);
}
- Run the test method. The debug log will display the labels whose values contain your search string.
Important Considerations:
- Error Handling: This code lacks explicit error handling (e.g., handling cases where
$Label.get
returns null). Consider adding more robust error handling for production environments. - Performance: For a very large number of custom labels, consider optimizing this by batching the label value retrieval.
- Namespace: This code accounts for namespaced custom labels. Remember that the
NamespacePrefix
is included in thelabelName
to ensure it works correctly in all contexts.
This Apex approach effectively solves the problem of finding custom labels based on their values, a task impossible to accomplish directly with SOQL alone. Remember to replace "Example"
in the test method with your actual search string.