Call a NAV Exposed Web Service in NAV using CU1290 in NAV2017

jordi79jordi79 Member Posts: 277
Hi All,

I have created a CU that just returns UPPERCASE of a supplied parameter, and I have exposed this CU as a web service.

The problem is that I am having problems in calling this web service, and using it in NAV. I have taken a look at CU248 "VAT Lookup Ext. Data Hndl.", and have adapted this to call my own exposed Web Service. But I seem to hit into endless errors. I have taken a look at some of the examples in this site and in the web, but when I try them, all seem to hit into errors.

When running code below, I hit into the error:
The expected data was not received from the web service

When I debugged the xml result, it is just the whole xml file similar to entering the Web service linke which is --> 'http://aws01:7047/DynamicsNAV100/WS/CRONUS International Ltd./Codeunit/WS_Exposed_CU' in the browser. But it should return a smaller xml file with the result.

My new CU adapted from CU248. By the way, I tested CU248, and it is working. But this CU calls an external web service (EU VAT validation web service), and not a NAV exposed web service. Also, I had errors coming from CU1290 with regards to 'multipart/form-data; charset=utf8', but I made some changes to CU1290 to bypass this error.
OBJECT Codeunit 99998 WS Test 4
{
  OBJECT-PROPERTIES
  {
    Date=21-12-16;
    Time=[ 1:18:40 AM];
    Modified=Yes;
    Version List=NAVW110.0;
  }
  PROPERTIES
  {
    OnRun=BEGIN
            //VATRegistrationLog := Rec;

            ExecWebService(TRUE);

            //Rec := VATRegistrationLog;
          END;

  }
  CODE
  {
    VAR
      NamespaceTxt@1003 : TextConst 'ENU=urn:microsoft-dynamics-schemas/codeunit/WS_Exposed_CU';
      VATRegistrationLog@1000 : Record 249;
      VATRegistrationLogMgt@1002 : Codeunit 249;

    LOCAL PROCEDURE ExecWebService@7(ShowErrors@1000 : Boolean);
    VAR
      RequestBodyTempBlob@1002 : Record 99008535;
    BEGIN
      RequestBodyTempBlob.INIT;

      SendRequestToVatRegistrationService(RequestBodyTempBlob,ShowErrors);

      MessageXMLOutput(RequestBodyTempBlob);

      //COMMIT;
    END;

    LOCAL PROCEDURE SendRequestToVatRegistrationService@1(VAR BodyTempBlob@1004 : Record 99008535;ShowErrors@1003 : Boolean);
    VAR
      GeneralLedgerSetup@1005 : Record 98;
      SOAPWebServiceRequestMgt@1001 : Codeunit 1290;
      ResponseInStream@1002 : InStream;
      InStream@1006 : InStream;
      ResponseOutStream@1008 : OutStream;
    BEGIN
      PrepareSOAPRequestBody(BodyTempBlob);

      BodyTempBlob.Blob.CREATEINSTREAM(InStream);

      GeneralLedgerSetup.GET;
      //IF GeneralLedgerSetup."VAT Reg. No. Validation URL" = '' THEN
      //  ERROR(MissingWebServiceURLErr);
      SOAPWebServiceRequestMgt.SetGlobals(
        InStream,
        'http://aws01:7047/DynamicsNAV100/WS/CRONUS%20International%20Ltd./Codeunit/WS_Exposed_CU','','');
      SOAPWebServiceRequestMgt.DisableHttpsCheck;
      SOAPWebServiceRequestMgt.SetTimeout(60000);

      IF SOAPWebServiceRequestMgt.SendRequestToWebService THEN BEGIN
        SOAPWebServiceRequestMgt.GetResponseContent(ResponseInStream);

        BodyTempBlob.Blob.CREATEOUTSTREAM(ResponseOutStream);
        COPYSTREAM(ResponseOutStream,ResponseInStream);
      END ELSE
        IF ShowErrors THEN
          SOAPWebServiceRequestMgt.ProcessFaultResponse('');
    END;

    LOCAL PROCEDURE PrepareSOAPRequestBody@12(VAR BodyTempBlob@1000 : Record 99008535);
    VAR
      XMLDOMMgt@1006 : Codeunit 6224;
      BodyContentInputStream@1004 : InStream;
      BodyContentOutputStream@1005 : OutStream;
      BodyContentXmlDoc@1003 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument";
      EnvelopeXmlNode@1012 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode";
      CreatedXmlNode@1001 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode";
    BEGIN
      BodyTempBlob.Blob.CREATEINSTREAM(BodyContentInputStream);
      BodyContentXmlDoc := BodyContentXmlDoc.XmlDocument;

      //XMLDOMMgt.AddRootElementWithPrefix(BodyContentXmlDoc,'checkVat','',NamespaceTxt,EnvelopeXmlNode);
      XMLDOMMgt.AddRootElementWithPrefix(BodyContentXmlDoc,'ReturnCaps','',NamespaceTxt,EnvelopeXmlNode);
      XMLDOMMgt.AddElement(EnvelopeXmlNode,'inStr','this should be in caps',NamespaceTxt,CreatedXmlNode);
      //XMLDOMMgt.AddElement(EnvelopeXmlNode,'vatNumber',VATRegistrationLog.GetVATRegNo,NamespaceTxt,CreatedXmlNode);

      CLEAR(BodyTempBlob.Blob);
      BodyTempBlob.Blob.CREATEOUTSTREAM(BodyContentOutputStream);
      BodyContentXmlDoc.Save(BodyContentOutputStream);
      //>> to debug output xml
      MESSAGE(FORMAT(BodyContentXmlDoc.InnerXml));
      //<< to debug output xml
    END;

    LOCAL PROCEDURE MessageXMLOutput@4(ResponseBodyTempBlob@1000 : Record 99008535);
    VAR
      XMLDOMManagement@1001 : Codeunit 6224;
      XMLDocOut@1012 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument";
      InStream@1009 : InStream;
    BEGIN
      ResponseBodyTempBlob.Blob.CREATEINSTREAM(InStream);
      XMLDOMManagement.LoadXMLDocumentFromInStream(InStream,XMLDocOut);
      MESSAGE(FORMAT(XMLDocOut.InnerXml));
      //VATRegistrationLogMgt.LogVerification(VATRegistrationLog,XMLDocOut,NamespaceTxt);
    END;

    BEGIN
    END.
  }
}

As usual, thanks for taking the time to look into this.

Answers

Sign In or Register to comment.