We have an Invocable Apex Class to do SOQL which uses generic SObject in flow but returns an error the same if trying to use that Sobject for the Custom Metadata getAll method.
global with sharing class CreateSobjectInvoc {
@InvocableMethod(label='Executes a SOQL query' category='Data')
global static List<OutputParameters> bulkInvoke(
List<InputParameters> inputs
) {
List<OutputParameters> outputs = new List<OutputParameters>();
for (InputParameters input : inputs) {
outputs.add(invoke(input));
}
return outputs;
}
private static OutputParameters invoke(InputParameters input) {
List<SObject> records = SObject.getAll().values()
// Return output
OutputParameters output = new OutputParameters();
output.records = records;
return output;
}
global class InputParameters {
@InvocableVariable(required=false)
global String query;
}
global class OutputParameters {
@InvocableVariable
global List<SObject> records;
}
}
Error
Method does not exist or incorrect signature: void getAll() from the
type SObject
The class works fine if I supply a specific Custom Metadata Object name instead but that would require a separate class for each Custom Metadata object. Is there a way to acheive this without doing that?
Best Answer
Not sure why you would think
would be supported
Each CMDT is a separate SobjectType and to fetch the records given an SObjectType you can just use
SObjectDescribe
methods and dynamic SOQL (which does not burn any of your SOQL limits).It is also best practice to create a selector class that wraps your access to CMDT records so you can mock that selector in testmethods and thus not have to rely on the org's current values for any CMDT (i.e. test isolation).