0

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?

1 Answer 1

1

Not sure why you would think

List<SObject> records = SObject.getAll().values()

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).

4
  • Updated question to include entire Apex Class. Class was originally used for a SOQL and working fine. For MDT query not needed for getAll but left as not required just to ensure stayed with a class that was orig working.
    – ddeve
    Commented Oct 29, 2021 at 1:36
  • @ddeve - not sure I understand your comment. getAll() has to be used with a specific sobjectType, i.e. a specific CMDT
    – cropredy
    Commented Oct 29, 2021 at 22:15
  • since Flow requires us to select the Sobject when using an Invocable class I wasn't clear why this pattern doesnt also work in this case. So the name of the CMDT is being sent from the Flow as happens with each of the Invocable classes. @cropredy
    – ddeve
    Commented Oct 30, 2021 at 21:25
  • 1
    It would work for insert, update, delete for sobjectTypes other than CMDT or platform events but not getAll as that is for CMDT only
    – cropredy
    Commented Oct 31, 2021 at 1:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .