In the first part we looked at how you can easily use Power Automate to create a REST endpoint which can receive a file and then save it to SharePoint. In this post we will expand a little upon that, because one of the great design patterns that Power Automate can help you to implement is the broker pattern. 😊
So, what is the broker pattern?🤔 The idea behind the broker pattern is that you only have one endpoint which will then handler your integrations going forward here by making your Integrations very simple and making it easy to expand in the future. 😎
To implement the broker pattern for our Save File Flow, we will first need to add a new variable to our JSON body, which we will call Type so your JSON body should look like this:
{
"type": "object",
"properties": {
"Document": {
"type": "string"
},
"ApiToken": {
"type": "string"
},
"FileName": {
"type": "string"
},
"Type": {
"type": "string"
}
}
}
Next we need to add a switch to our Flow, so inside your if Yes press Add an action:
Choose control and choose switch:
In the On statement choose our new Type, and in the first case add a key called sharepoint:
Next create a new, Create File like the one we had before and add it inside our Case, and then delete our old Create file so your flow looks something like this:
And you have now created a broker, try to add a new Case and call it FTP:
And here add an FTP action:
Fill in your FTP server information, next fill out the information much like the information for SharePoint:
Now as you can see it is now easy to expand our flow to be able to place our file anywhere by expanding our switch statement.
Next, we need to change our Business Central code to allow for this new change, and be doing this correctly we should never have to change our AL code again 😉
Your code could look something like this:
procedure SendToFlow(FileName: Text; Base64: Text; Type Code[20])
Var
client: HttpClient;
cont: HttpContent;
header: HttpHeaders;
response: HttpResponseMessage;
Jobject: JsonObject;
tmpString: Text;
Begin
Jobject.Add('ApiToken', ‘{token}’);
Jobject.Add('Document', Base64);
Jobject.Add('FileName', FileName);
Jobject.Add('Type', Type);
Jobject.WriteTo(tmpString);
cont.WriteFrom(tmpString);
cont.ReadAs(tmpString);
cont.GetHeaders(header);
header.Remove('Content-Type');
header.Add('Content-Type', 'application/json');
client.Post(‘{ flow url}’, cont, response);
end;
Where Type can be changed between whatever you wish your flow to execute so in my example it could be either Sharepoint or FTP.
Well that was it for this post, hopefully you found some inspiration, and until next time stay safe. 😷