Labels

Tuesday, June 21, 2011

Interaction with Web service from NAV client-side

So, next task is to send the request from NAV Client. To complete this task I used  'Microsoft XML, v6.0'.XMLHTTP automation object.


SendRequest()
IF ISCLEAR(XMLDoc) THEN 
  CREATE(XMLDoc);
IF ISCLEAR(XMLResponse) THEN
  CREATE(XMLResponse);


RequestText := SOAPBegin + '<Request method="GetBalance" ' + SOAPSchema + '>' +
'<CardID>' + CardID + '</CardID>' +
'</Request>' + SOAPEnd;


IF NOT XMLDoc.loadXML(RequestText) THEN ERROR(RequestFailed);


IF ISCLEAR(HTTPL) THEN
  CREATE(HTTPL);
HTTPL.open('POST',CRMSetup."Bonus Server Url",FALSE);
HTTPL.setRequestHeader('Content-Type','text/xml');
HTTPL.setRequestHeader('SOAPAction','');
HTTPL.send(RequestText);


XMLResponse := HTTPL.responseXML;
IF HTTPL.status <> 200 THEN ERROR(ConnectionFailed);
IF ISCLEAR(XMLResponse) THEN ERROR(ConnectionFailed);


DocRoot := XMLResponse.documentElement;
XMLDocNode := DocRoot.selectSingleNode('Error');
IF NOT ISCLEAR(XMLDocNode) THEN ERROR(XMLDocNode.text);
XMLDocNode := DocRoot.selectSingleNode('TotalBalance');
IF ISCLEAR(XMLDocNode) THEN ERROR(ProtocolError);
EVALUATE(TotalBalance, CONVERTSTR(XMLDocNode.text,'.',','));



Global constants for sending SOAP request are:

SOAPBegin <?xml version="1.0" encoding="UTF-8" standalone="no" ?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XML/XMLSchema-instance"> <SOAP-ENV:Body>
SOAPEnd </SOAP-ENV:Body></SOAP-ENV:Envelope>
SOAPSchema xmlns="http://bonus.local/Bonus" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" refSchema="BRequest.xsd" version="1.0"

When the web-service was turned off the exception was thrown, that XMLResponse variable was not initialized. It comes with not user-friendly message for C\AL programmers. I've tried several ways to test the variable initialized or not, but no one fits my task. That's why I wrote small codeunit Test HTTP Connection for it:


OnRun()
CRMSetupL.GET;
CREATE(HTTPL);
HTTPL.open('GET', CRMSetupL."Bonus Server Url", FALSE);
HTTPL.send('<hello></hello>');
IF HTTPL.status <> 200 THEN;


And include it's call in the beginning of SendRequest trigger:

SendRequest()
IF NOT TestHTTPConnection.RUN() THEN ERROR(ConnectionFailed);



2 comments:

Anonymous said...

nice code... I've problem by reading the response...
can you give me the variables definition?
thanx a lot
Chris

GRIZZLY said...

I've used HTTPL Automation 'Microsoft XML, v6.0'.ServerXMLHTTP60.
What's problem you've got when reading response?