Synch data from BC(Item Unit of Measure Table) to CRM(Customized Table same structure like BC table)

Hi Experts,

I need to synch data from Business Central ("Item Unit of Measure" Table) to CRM (New Customized Table same structure like BC table).
I used CDS setup for this but not able to synch this because of this table is not showing in Table Mapping in CDS.

So, I go with AL Code level but NAV integration concept is not working in Business Central OnCloud because of DotNet variable not supported in Business Central AL code.

So, I need your help/suggestion how I can achieve this, I have CRM webservice/api having data form in JSON, below are API url from CRM...

1) https://[ORG URI].api.crm4.dynamics.com/XRMServices/2011/Organization.svc?wsdl (this have all webservices inside the CRM)

2) https://[ORG URI].crm4.dynamics.com/api/data/v9.1/beg_itemunitofmeasures (this is a particular table access url from CRM called as Item Unit of Measure in CRM this is customized table and having same fields and structure same like BC table of Item Unit Of Measure)

I need to understand how to make above API Authentication in Business Central in AL code and how to synch data from BC to CRM using these API.



Thanks in Advance.

Answers

  • txerifftxeriff Member Posts: 500
    edited 2020-10-20
    Hi,

    dotnets are not supported anymore but in AL you can connect to WS.


    You can use AL "functions", use for example "waldo´s pack":
    https://www.waldo.be/2019/12/05/waldo-restapp/

    Also you can use this piece I use.
    I just the case 0: but it is handy to have the other methods. You just need request url formed and username/pwd

    CallService('myproject', vWSURL, RequestType, payload, T50049.UserName, T50049.Password);
     procedure CallService(ProjectName: Text; RequestUrl: Text; RequestType: Integer; payload: Text; Username: Text; Password: Text): Text
        var
            Client: HttpClient;
            RequestHeaders: HttpHeaders;
            RequestContent: HttpContent;
            ResponseMessage: HttpResponseMessage;
            RequestMessage: HttpRequestMessage;
            ResponseText: Text;
            contentHeaders: HttpHeaders;
        begin
            RequestHeaders := Client.DefaultRequestHeaders();
            RequestHeaders.Add('Authorization', CreateBasicAuthHeader(Username, Password));
            case RequestType of
                0:
                    Client.Get(RequestURL, ResponseMessage);
                1:
                    begin
                        RequestContent.WriteFrom(payload);
                        RequestContent.GetHeaders(contentHeaders);
                        contentHeaders.Clear();
                        contentHeaders.Add('Content-Type', 'application/json');
                        RequestMessage.Content := RequestContent;
    
                        RequestMessage.SetRequestUri(RequestURL);
                        // RequestMessage.Method := 'PATCH';
                        RequestMessage.Method := 'PUT';
                        client.Put(RequestURL, RequestContent, ResponseMessage);
                    end;
                2:
                    begin
                        RequestContent.WriteFrom(payload);
    
                        RequestContent.GetHeaders(contentHeaders);
                        contentHeaders.Clear();
                        contentHeaders.Add('Content-Type', 'application/json');
    
                        Client.Post(RequestURL, RequestContent, ResponseMessage);
                    end;
                3:
                    Client.Delete(RequestURL, ResponseMessage);
            end;
    
            ResponseMessage.Content().ReadAs(ResponseText);
    
            if ResponseMessage.HttpStatusCode <> 200 then begin
                if StrPos(ResponseText, '"message"') <> 0 then
                    ResponseText := CopyStr(ResponseText, StrPos(ResponseText, '"message"') + 10, StrLen(ResponseText));
    
                Error(strsubstno(Text50004, ResponseMessage.HttpStatusCode, ResponseText, payload));
            end else
                exit(ResponseText);
        end;
    
    procedure CreateBasicAuthHeader(UserName: Text; Password: Text): Text
        var
            TempBlob: Record TempBlob;
        begin
            TempBlob.WriteAsText(StrSubstNo('%1:%2', UserName, Password), TextEncoding::UTF8);
            exit(StrSubstNo('Basic %1', TempBlob.ToBase64String()));
        end;
    
    

    And this is an example to write JSON, (I cleared a bit the names because points to real work). To read it I haven´t used but you will find it: vJsonObject.ReadFrom()
    vJsonObject: JsonObject;
    vJsonValue: JsonValue;
     vJsonArray: JsonArray;
    
      vJsonObject.Add('path', '/app:company_home/cm:System/');
                                        vJsonObject.Add('type', 'linkedValue');
                                        vJsonObject.Add('lkvValue', Format(vFieldRef.Value));
                                        vJsonObject.Add('erpCode', Format(vFieldRef.Value));
                                        vJsonValue.ReadFrom('1');
                                        vJsonObject.Add('depthLevel', vJsonValue.AsInteger());
                                        vJsonObject.WriteTo(vpayload);
    

    Then, if you want to happen every time they change something in 5404 table, just create an event subscriber.

    I hope all this is useful to whatever you are trying to do (send/receive data from nav).
  • Pradeep1611Pradeep1611 Member Posts: 8
    edited 2020-11-15
    Thank you @txeriff :smile:
Sign In or Register to comment.