Connections are the place where RESTwithUS stores all links between records in Business Central and records in an external system. You can access them from a provider list by clicking on any connections number:
Or you can go to entities by selecting Entities in the menu and click the connection numbers on selected entity:
In the example you have 2 healthy connections and 1 broken connection for the Customer entity. Let's click on the number in Total Connections to view them:
All connections contain the following information:
Plus there are some interesting options in Details menu:
Connections can be created, updated or deleted by the Operations JSON response. The setup is always very similar, so let's have an example of a POST operation on a Customer which creates a new record. Response JSON body is following:
Table
.In the details window pay attention to Connections tab. First you need to set the mapping operation:
Create or Update
Create
Update Only
(or not needed at all, this will update the mapping description only)Delete
(this will only work if the operation returns some kind of JSON body; if it doesn't, you need to delete the mapping with code)Second important setting is the Table View which defines, how should RESTwithUS search for related record in Business Central:
This is a standard Business Central table view expression and you can update it in any way you want (i.e. use any field or combination of fields for the filtering).
You can even use helper function Generate Table View to generate the table view with it. Just select the table index that suits you the most (e.g. if you match the records by Business Central Customer No. select the no. primary key.) – and RESTwithUS will create the table view for you.
Note the {{Field1}}
variable in the expression. During the operation run, this expression will be replaced by data from JSON response. You just have to tell RESTwithUS where to find the value:
DELETE operations sometimes don't return any meaningful JSON body. If they do, you can use the Connection Operation settings as usual – but otherwise, you need to delete the mapping with code after the operation run. Use following line of code using API Script RWU codeunit:
//Select provider and entity by enum ID (recommended)
var
APIScriptRWU: Codeunit "API Script RWU";
EntityID: Enum "Entity ID RWU";
ProviderID: Enum "Provider ID RWU";
ApiScriptRWU.DELETE_MAPPING(ProviderID::TestProvider,EntityID::TestProvider_Customer,Customer);
//Select the mapping by record SystemId (recommended for BC 18 and newer)
APIScriptRWU.DELETE_MAPPING(ProviderID::TestProvider,EntityID::TestProvider_Customer,Customer.SystemId);
//Select provider and entity by description (for testing purposes)
ApiScriptRWU.DELETE_MAPPING('Test Provider','Customer',Customer);
Parameters are: Provider name, entity name and a Business Central record, whose connection you want to delete.
Tip: For more API Script functions see guideline API Script Functions Reference.
Mostly you want to look into the connections for a record External ID (the unique id in an external system).
For example, let's have a Customer 01121212
. You want to make an action on the Business Central page, that will create the customer in an external system – or update his data if he already exists.
To make such a function you first need to know, if the record was already created in an external system. But you are saving the information into connections, so if a POST operation was done on the record, you should have a trace there. Plus you have saved the External ID there, too – and you will need that to make a PUT/PATCH call.
To get the id write one line of code using API Script RWU codeunit:
//Select provider and entity by enum ID (recommended)
MappingId := APIScriptRWU.MAPPING(ProviderID::TestProvider,EntityID::TestProvider_Customer, Customer);
//For BC 18 and newer use record SystemId instead
MappingId := APIScriptRWU.MAPPING(ProviderID::TestProvider,EntityID::TestProvider_Customer, Customer.SystemId);
//Select provider and entity by description (for testing purposes)
MappingId := APIScriptRWU.MAPPING('Test Provider','Customer', Customer);
Mapping Id
is a Text variable. The function will return an empty string, if no connection was found, and the record External ID othewise.You can then use the result to find out, which operation you should call:
if MappingId = '' then begin
//Call POST operation to create the Customer
end
else begin
//Call PUT/PATCH operation to update the Customer
end
Tip: For more API Script functions see guideline API Script Functions Reference.
If you have a red broken connections in the list, there are more ways to fix them. Probably the easiest way is to create a GET Multiple Records operation, that will get all records from an external system and Create or Update
the connections. If there are still some errors after the operation run, you can easily track them in Batch Entries and find out, what happened.
Common issues are following:
{{!Field*}}
variables can be found in the response body (e.g. in the example, the JSON response should contain Business Central table field number 1
).E-mail
field in Business Central and email
node in an external system), you must have the same values in both fields for the records to match.Second option how to fix the connection is to select the broken connection and use Connection / Fix Connection action:
This action will show you the list of all records in the table and you can select the right one and press OK:
Just confirm the selection and the connection will be fixed.