jueves, 26 de noviembre de 2015

Consumir Web Service / Servicio Web SOAP II (RTC)


Hace un tiempo publicamos un post indicando como consumir un web service desde navision(Consumir Web service SOAP ), está funcionalidad se podía utilzar en el cliente clásico y hasta la version 2009. Ahora con el cambio de platafarma y hasta la version 2016 en la que se implementa una funcionalidad específica para realizar esto, la solución que hemos encontrado es la siguiente.

En resumen, lo que se hace es crear el fichero Xml directamente luego lo enviamos al web service y leemos la respuesta. En este caso enviamos dos parámetros y recogemos un valor booleano que nos devuelve una codeunit.


Variables.

Name                  DataType      Length
URL                    Text              250
Metodo               Text              1024
NameSpace         Text              80
NodeList             Automation    'Microsoft XML, v6.0'.IXMLDOMNodeList  
xmlhttp                Automation    'Microsoft XML, v6.0'.XMLHTTP  
xmldoc                Automation    'Microsoft XML, v6.0'.DOMDocument  
soapEnvelope      Automation    'Microsoft XML, v6.0'.IXMLDOMElement  
soapBody            Automation    'Microsoft XML, v6.0'.IXMLDOMElement  
soapMethod        Automation    'Microsoft XML, v6.0'.IXMLDOMElement  
soapAtributo        Automation    'Microsoft XML, v6.0'.IXMLDOMElement  
node                    Automation    'Microsoft XML, v6.0'.IXMLDOMNode  
parametersXmlDoc    Automation    'Microsoft XML, v6.0'.DOMDocument  
xmldoc2               Automation    'Microsoft XML, v6.0'.DOMDocument   


Función LlamarWS(Producto : Code[20];TipoMod : Code[20]) Result : Boolean

Result := FALSE;

//iniciar llamada al web service
URL:='http://localhost:7047/DynamicsNAV/ws/company/Codeunit/nombreCodeunit';
Metodo:='PruebasWS';
NameSpace:='urn:microsoft-dynamics-schemas/codeunit/nombreCodeunit';

// Crear el documento xml
CREATE(xmldoc,TRUE,TRUE);

// Crear el envelope SOAP
soapEnvelope := xmldoc.createElement('Soap:Envelope');
soapEnvelope.setAttribute('xmlns:Soap', 'http://schemas.xmlsoap.org/soap/envelope/');
xmldoc.appendChild(soapEnvelope);

// Crear el cuerpo del SOAP
soapBody := xmldoc.createElement('Soap:Body');
soapEnvelope.appendChild(soapBody);

// Crear el método del body
soapMethod := xmldoc.createElement(Metodo);
soapMethod.setAttribute('xmlns', NameSpace);
soapBody.appendChild(soapMethod);

// Crear los parametros del metodo.
soapAtributo:= xmldoc.createElement('oarametro1');
soapAtributo.text(textoParametro1);
soapMethod.appendChild(soapAtributo);

soapAtributo:= xmldoc.createElement('parametro2');
soapAtributo.text(textoParametro1);
soapMethod.appendChild(soapAtributo);

//Enviar fichero al web service
CREATE(xmlhttp, TRUE, TRUE);
xmlhttp.open('POST', URL, FALSE);
xmlhttp.setRequestHeader('Content-type', 'text/xml; charset=utf-8');
xmlhttp.setRequestHeader('SOAPAction', Metodo);
xmlhttp.send(xmldoc);

//si la respeusta es Ok devuelve valor 200
IF xmlhttp.status=200 THEN
BEGIN
  xmldoc := xmlhttp.responseXML;
  xmldoc.setProperty('SelectionLanguage','XPath');
  xmldoc.setProperty('SelectionNamespaces','xmlns:tns="'+NameSpace+'"');
  NodeList := xmldoc.selectNodes('//tns:'+'return_value');
  Result := TRUE;
  if NodeList.item(0).text = 'true' then
    Exit(TRUE);
End;
Exit(FALSE);


Tambien se podria realizar la lectura del Xml devuelto pero eso ya depende de los gustos de cada uno.

Variables
xmldoc2                Automation    'Microsoft XML, v6.0'.DOMDocument   

IF ISCLEAR(xmldoc2) THEN
  CREATE(xmldoc2,TRUE,TRUE);
xmldoc2.load(xmlhttp.responseXML);
xmldoc2.save('ruta\fichero.xml');