Using Electronic Reporting for API requests (I)

When we need to make a request to an external API, it’s really helpful to use ER because it’s a low-code environment that helps us build, test, and fix our messages much faster. ER supports XML and JSON, so we won’t have a hard time designing the request body or reading the response body.

With ER destinations, it is possible to send a file to SharePoint, use a Logic App or a Power Automate flow to call an API, leave the answer in SharePoint again, and automatically import it using a batch with ER origins. This process takes too much time; while sometimes this doesn’t matter, in other cases we may need to read the response in almost real time. For example, before confirming a sales order, we may need to ask our insurance company if they cover our customer or, when confirming a purchase order, we may need to ask our bank if they would finance us.

For those cases, I separate duties: I use ER to generate the body and read the answer, and I use code to communicate with the API.

In this guide, I’m going to show how to make a POST request with a simple body and Basic Auth. This request will be executed in a form, will get data from a field, and will write info from the answer into another field. It will also save the body and the answer body in an attachment.

I will be using a test API from httpbin. While it doesn’t really have basic auth or any authorization whatsoever, I’m just going to pretend it has.

URL: https://httpbin.org/post
Method: Post
Auth: Basic
User: Any
Pass: Any
Headers: {“Content-Type”: “application/json”}
Body: {“String”: [Data]}
Answer: {…,”Data”: [Body],…}

Let’s start with a table and form with three columns and a button ‘Run api’ pointing to a class:

Now we can start by parameterizing the API. I normally use the standard parameter forms that exist in each module. In this example, I’ve extended asset parameters:

I’m using the following EDTs:
Format mapping (Export): ERFormatMappingID
Model mapping (Import): ERModelMappingID
Url: String
User: String
Password: EncryptedField

For the password, I’ve also created an EDT String. This would be necessary for using labels, and I’ve increased the default string size.

Then, with CoC, I’ve created an extension of the table and added a method that will be used to encrypt and decrypt the password. The return type is the EDT I’ve just created.

[ExtensionOf(tableStr(AssetParameters))]
final class AssetParameters_Table_TST_Extension
{
    public edit TST_Password TST_ApipassNameEdit(boolean _set, Name _value)
    {
        return Global::editEncryptedField(this, _value, fieldNum(AssetParameters, TST_ApiPass), _set);
    }

}

Finally, we’ll add the fields in the form extension. All are standard except for the password. For the password, we’ll create a new string and point to the method we’ve just created. Also, the property PasswordStyle must be set to Yes:

Now let’s create the ERs. I’ve decided to put the import and export circuits together in the same model and mapping nodes, but it’s just a personal choice:

I’m not gonna explain step by step how to create the ER’s, but I have a series for creating Export and Import messages in ER.

This is the model, with two roots and, to keep it simple, only one string for each:

The mapping node has two datasources:

The export mapping has a user input parameter $id that’s used to filter our table and find a single record:

The import mapping has a user input parameter ‘$RecId’ that’s used to identify the record. By default, in the import, you have to declare the model in ‘Data Source Types > Data model > Data model’ and point it to the import root of our model. On the right, let’s select the destination table, set the record action to ‘Update’, and check the ‘Integration Point’ option. Finally, let’s map the string and the ‘$RecId’ to the table:

The export format is very simple:

And the import:

Now we can complete our formats and parameterize them in AssetParameters. In the next post, I’m going to explain the code to connect to the API.


Posted

in

, ,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *