web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Recreate code in NAV 2018

(0) ShareShare
ReportReport
Posted on by 131

Hello Community, first of all thank you very much for taking the time to read my question


I have a small problem with a development in Nav 2018, I have to consume a web service, I know how to do it by programming in Visual Studio Code with the AL extension


But right now I am developing with C / AL and I see that HttpClient variables are used as .NET variables

In AL I do it in the following way and the consumption of the web service works correctly for me

codeunit 50600 "Test Web Service"
{
    procedure TestWebService(PBARFCEmisor: Text; PBARFCReceptor: Text; PBATotal: Text; PBAUUID: Text)              //procedure TestWebServiceDoBrasil(var PBAXMLinTEXT: Text)
    var
        XMLDomMgt: Codeunit "XML DOM Mgt.";
        PBAXML: Text;
        PBADOCUMETXML: XmlDocument;

        uri: Text;
        RequestHeaders: HttpHeaders;
        RequestMessage: HttpRequestMessage;
        RequestContent: HttpContent;
        HttpClient: HttpClient;
        ResponseMessage: HttpResponseMessage;
        Content: HttpContent;
        text2Send: Text;
        PushHttpAddress: Text[80];
        lSuccess: Boolean;
        ResponseInText: Text;


        Pos: Integer;
        Length: Integer;
        PBAEstado: Text;
    begin

        ///////////////////// Construccion XML exitosa //////////
        PBAXML := '?re='   PBARFCEmisor   '&rr='   PBARFCReceptor   '&tt='   PBATotal   '&id='   PBAUUID   '';
        XMLDomMgt.LoadXMLDocumentFromText(PBAXML, PBADOCUMETXML);
        PBADOCUMETXML.WriteTo(PBAXML);
        //PBAXMLinTEXT := PBAXML;
        //Message(PBAXML);




        ///Construccion de la Llamada Web Service///

        content.Clear();
        HttpClient.Clear();
        RequestHeaders.Clear();
        RequestMessage.Content().Clear();
        ResponseMessage.Content().Clear();
        ResponseMessage.Headers().Clear();

        lSuccess := False;
        PushHttpAddress := 'https://consultaqr.facturaelectronica.sat.gob.mx/ConsultaCFDIService.svc?wsdl';

        uri := pushHttpAddress;

        RequestMessage.SetRequestUri(uri);
        RequestMessage.Method := 'POST';


        text2Send := PBAXML;
        RequestContent.writefrom(Text2Send);
        RequestContent.GetHeaders(RequestHeaders);

        RequestHeaders.Remove('Content-Type');
        RequestHeaders.Add('Content-Type', 'text/xml; charset=utf-8');
        RequestHeaders.Add('SOAPAction', 'http://tempuri.org/IConsultaCFDIService/Consulta');

        RequestMessage.Content(RequestContent);

        if not HttpClient.Send(RequestMessage, ResponseMessage) then begin
            Error('Error en el Web Service');
        end;

        Content := ResponseMessage.Content();
        Content.ReadAs(ResponseInText);
        lSuccess := ResponseMessage.IsSuccessStatusCode();
        //Message(ResponseInText);

        Pos := StrPos(ResponseInText, '')   10;
        Length := StrPos(ResponseInText, '');
        Length := Length - Pos;
        PBAEstado := CopyStr(ResponseInText, Pos, Length);

        Message('CFDI: '   PBAEstado);


    end;

}

My question is how can I do the above code in C / AL using .NET variables


Thanks again for your time.

I have the same question (0)
  • Suggested answer
    Suresh Kulla Profile Picture
    50,243 Super User 2025 Season 2 on at

    There are several examples in standard NAV, check codeunit 7820 or 2020 there is a invoke rest api function which uses HttpClient.

  • Yael Suarez Profile Picture
    131 on at

    Before anything, thank you for taking the time to read my question.

    Seeing the code units you mentioned, I managed to code the following:

    PBAXML := TxtXML;   //// It is the XML that I copy and paste from SoapUI to verify the response of the web service, save it in a constant text variable
    
    HttpClient:=HttpClient.HttpClient();
    HttpClient.DefaultRequestHeaders.Add('SOAPAction', 'http://tempuri.org/IConsultaCFDIService/Consulta');
    HttpClient.BaseAddress := uri.Uri('https://consultaqr.facturaelectronica.sat.gob.mx/ConsultaCFDIService.svc?wsdl');
    
    ResponseMessage := HttpClient.PostAsync(PBAXML,content).Result;
    
    Result := ResponseMessage.Content.ReadAsStringAsync.Result;
    MESSAGE(Result);

    But I still can't get the answer that I get with the code that I made in visual studio code with the extension AL


    Do you have any other point of view that you could share with me your knowledge to consume the web service?


    Thanks again for your time and I send you my regards

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,917

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,161 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,025 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans