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
Dave GelinasDave Gelinas 

HTTP POST to Power Automate Flow

Hello,

I'm trying to send a simple HTTP Post from Salesforce to a PowerAutomate Flow that is setup. I think I'm struggling with authenticating but the JSON being passed is OK. Not really sure as I'm new to APEX. The PowerAutomate developer provided a long URL for Azure which had some authentication variables such as sig. I remove those from the endpoint URL and put them into the body along with the expect data which is the OpportunityID. 

I also add the Azure endpoint URL to the Remote Site settings in Salesforce. Do I need to add named credentials as well? if so, what type? 

Here's the HTTP Post APEX. I when I execute which I've been doing from the Execute Anonymous Window, I get a 400 error.
 
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://prod-999.westus.logic.azure.com:443/workflows/9999999999/triggers/manual/paths/invoke');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setBody('{"api-version":"2016-06-01","sp":"/triggers/manual/run", "sv":"1.0","sig":"ABCDEFGHIJKLMNOP","OpportunityID":"0062E00001Oa8GcQAJ"}');
HttpResponse response = http.send(request);
// Parse the JSON response
if(response.getStatusCode() != 201) {
    System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getStatus());
} else {
    System.debug(response.getBody());
}

Here's the expected PowerAutomate schema which also provided the endpoint URL and URL parameters as I noted above. 

User-added image
Adam Zuckerman 3Adam Zuckerman 3

Since you are not that well versed in Apex, I would suggest that you try to POST to the PowerAutomate URL via Postman first and remove that from the equation.

Generally, it does not appear that your request matches the Request Body JSON Schema. I'm not sure about the authentication, but I don't see that your request is authenticated at all for instance with an API key set in the header or some other means.

I think you should validate that you can POST to the URL via a tool list Postman and then consider a declarative way to build the payload such as Declarative Webhooks (https://appexchange.salesforce.com/appxListingDetail?listingId=a0N3u00000MSv8REAT). No sense in banging your head against the wall not only building the payload, but also have to deal with a trigger, future method or maybe a VF page, Aura component or LWC. With DW, you can trigger a callout from Process, Builder, Flow, or even a custom button on a record page.