How to customize Publication with an integration
This article describe how to use publication to send data to 3rd party.
Publication Interface
Pimics has interface "PIMX Publication Update Line c7" which is used in Publication to generate data in format what is expected by channel.
When you want to prepare a data for another solution, the best way is to use this interface and generate data in the format what is expected by the party.
Use the interface
Implement interface
codeunit 50000 "Custom Publication Update" implements "PIMX Publication Update Line c7"
{
var
QueueHelper: Codeunit "PIMX Publication Queue Helper";
...
procedure UpdateItem(var _line: Record "PIMX Publication Line"; AllocationLine: Record "PIMX Allocation Line"; Data: Codeunit "PIMX Publication Data"): Boolean
var
_Item: Record Item;
newJsonText: Text;
begin
Data.GetItem(_Item, _line.Nummer); //It reads data item from cache
//Generate the JSON
//Save and Send the JSON
if newJsonText <> _Line.GetData() then
QueueHelper.InsertMessage(method::POST, Url, newJsonText);
end;
...
}
Extend enum
enumextension 50000 "PIMX Publication Data Type Ext" extends "PIMX Publication Data Type"
{
value(50000; "Custom")
{
Caption = 'Custom';
Implementation = "PIMX Publication Update Line c7" = "PIMX Custom Publication Update";
}
}
Publication Queue
As you can see above the code use a Publicatio Queue. It is queue which has been use for sending data. You can check the state of sending on the page Publication Queue. For processing data you need to implement the interface below and define a Job Queue.
Setup Job Queue
There is codeunit 70113798 "PIMX Job Send Publ. Queue" this needs to be added to Job Queues and set when and how you want to run it.
Sent Messages
codeunit 50001 "Custom API Client" implements "PIMX API Client"
{
var
auth: Record "PIMX Authentication";
SendFail_Err: Label 'Unknown Error in API';
procedure Send(var Queue: Record "PIMX Publication Queue");
var
restClient: HttpClient;
headers: HttpHeaders;
request: HttpRequestMessage;
response: HttpResponseMessage;
begin
//Get Authentication information
GetAuth(Queue.Auth);
//Read Data
request.Content.WriteFrom(Queue.GetData());
//Set Headers
request.Content.GetHeaders(headers);
headers.Remove('Content-Type');
headers.Add('Content-Type', 'application/json');
case method of
method::PATCH:
request.Method('PATCH');
method::POST:
request.Method('POST');
method::PUT:
request.Method('PUT');
else
request.Method('GET');
end;
// Url and client identification
request.SetRequestUri(url);
restClient.SetBaseAddress(url);
restClient.DefaultRequestHeaders.Add('User-Agent', 'Pimics');
restClient.DefaultRequestHeaders.Add('Accept', '*/*');
// Set Basic Auth
restClient.DefaultRequestHeaders().Add('Authorization', STRSUBSTNO('Basic %1', Base64Convert.ToBase64(STRSUBSTNO('%1:%2', ApiUser, ApiPassword))));
// Send the request
if restClient.Send(request, response) then begin
Queue.SetProcessed(response)
else
Queue.SetError(SendFail_Err);
end;
local procedure GetAuth(AuthId: Guid)
begin
if Auth.SystemId <> AuthId then
Auth.GetBySystemId(AuthId);
end;
}
```