0

We are thinking of using External Services to integrate with an external application. Due to the nature of the application, we want to create logging records in Salesforce (creating a Log object to capture the message and response). Usually we store the request and response body in text areas on a log record so we have an exact account of what was sent and what was returned (for audit purposes).

External Services make use of Dynamic Apex Classes. These classes are fully managed within Flow - all the properties are set in Flow and the callout and callout result (including the response body) are available in the Flow runtime. However, there is no trivial way to serialize these classes and store them as a string in the request body fields on the log object - especially not in a way that is repeatable for different Dynamic Apex types. There is no JSON.serialize(requestBodyObject) or requestBodyObject.toString(), nor can an untyped Object be used as an InvocableVariable, it must have some type to pass it to Apex. We've tried the below, but this generates an error when deploying.

public class Input {
    InvocableVariable(Label='Request Object' Description='The External Service object that is sent' Required=true)
    public Object requestObject;
    InvocableVariable(Label='Response Object' Description='The result object of the External Service' Required=true)
    public Object responseObject;
}
Error: InvocableVariable fields do not support type of Object

Alternatively we've tried wrapping the requestObject and responseObject in an Apex Defined Data Type, however any properties with type Object will not show up in Flow Assignments and cannot be used. We are also aware that we can create a text-template with the JSON structure and fill that with the data for each single callout, but that is hardly repeatable or maintainable.

So the question is whether there is a reusable way to stringify objects in Flow, or pass generic objects/ Dynamic Apex objects to Apex for the above use case. Any help is greatly appreciated.

2
  • ...using External Services to integrate with an external application (that is, one application) but that is hardly repeatable or maintainable are contradictory statements.
    – identigral
    Commented Oct 16, 2023 at 19:09
  • This External Service has multiple endpoints and services, it's one External Service though. These services will be used in different flows and I don't want to create logging in these different flows using a convoluted solution - that makes it "hardly repeatable and maintainable".
    – xouns
    Commented Oct 17, 2023 at 8:27

1 Answer 1

0

When you specify a type variable in a class, you should use the following form: ExternalService.YourClass.

Example:

 public class ConvetObjectToStringForFlow {

    @InvocableMethod(label='Convert Flow Callout Data to String')
    public static List<Output> convertToString (List<Input> req){

        List<Output> outputs = new List<Output>();
        Output output = new Output();
        output.bodyCalluotString = JSON.serialize(req[0].bodyCalluot);
        output.responsCalluotString = JSON.serialize(req[0].responsCalluot);
        outputs.add(output);

        return outputs;     
    }

    public class Input {
        @InvocableVariable
        public ExternalService.MulesoftIntegrationUpdateSAPWBSElementExternalService_Mulesoftx20Integrationx20tox20updatex20SAPx20WBSx20Elementx20Action_IN_body bodyCalluot;
        @InvocableVariable
        public ExternalService.MulesoftIntegrationUpdateSAPWBSElementExternalService_Mulesoftx20Integrationx20tox20updatex20SAPx20WBSx20Elementx20Action_OUT_2XX responsCalluot;
    }

    public class Output {
        @InvocableVariable
        public String bodyCalluotString;
        @InvocableVariable
        public String responsCalluotString; 
    }
}

See Setup>Apex Classes for the names of your classes enter image description here

I hope this helps you.

1
  • I understand that I can create a dedicated invocable for each dynamic class, but that defeats the purpose of using Dynamic Apex. Instead we are looking for a single Invocable pattern that we create once and use multiple times.
    – xouns
    Commented Apr 11 at 8:13

You must log in to answer this question.

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