Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

How to build a SOAP response (in scripted WebServices) like getRecords response

LucaP5957190930
Mega Sage

Good evening,

I have made a scripted webservice that should give response with more than one records, like getRecords d. I tried several ways, but nothing work like I whould like it to work.

I appreciate any help

Here   is my scripted web service:

(function scriptedWebServiceOperation(request, response) {

var singolo=(request.numero==""||request.numero==undefined)?true:false;//request.numero.nil();//Discriminante per sapere se si vogliono più risultati oppure uno solo
var multipli=(request.numeri_multipli==""||request.numeri_multipli==undefined)?true:false;//request.numeri_multipli.nil();
//When there is only a record to query incident table with
if(!singolo||multipli){//Un solo record richiesto
  var xmldoc = new XMLDocument("<Risposta></Risposta>");
    var number=request.numero;
    var gr= new GlideRecord('incident');
      gr.addQuery('number',number);
      gr.query();
    if(gr.next()){
      xmldoc.createElement("sys_id",gr.sys_id);
      xmldoc.createElement("state",gr.state);
      xmldoc.createElement("resolved_at",gr.resolved_at);
      xmldoc.createElement("closed_at",gr.closed_at);
      response.soapResponseElement = xmldoc.getDocumentElement();
    }      
    else {
 
        response.Esito= 'KO';
        response.CodiceEsito = '1';
        response.status_message = 'Chiamata in errore,numero inesistente o errato';
    }
}
//When there are more than one record to query incident table with
else if(singolo||!multipli) {//Più records richiesti
 
    var xmldoc1 = new XMLDocument("<Risposta></Risposta>");
    var numeri_multipli= request.numeri_multipli;
    var gr1= new GlideRecord('incident');
      gr1.addQuery('number','IN',numeri_multipli);
      gr1.query();
  while(gr1.next()){
      var ris=xmldoc1.createElement("Prova");
      xmldoc1.setCurrent(ris);
      xmldoc1.createElement("sys_id",gr1.sys_id);
      xmldoc1.createElement("state",gr1.state);
      xmldoc1.createElement("resolved_at",gr1.resolved_at);
      xmldoc1.createElement("closed_at",gr1.closed_at);
     
    }  
      response.soapResponseElement = xmldoc1.getDocumentElement();
  }
})(request, response);

This is the response at the moment:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <SOAP-ENV:Body>

          <Risposta>

                <Prova>

                      <sys_id>717bc559370b6600ab75fa7b34990ebd</sys_id>

                      <state>1</state>

                      <resolved_at/>

                      <closed_at/>

                      <Prova>

                            <sys_id>f4fbc9190f87a200f39538b362050e93</sys_id>

                            <state>1</state>

                            <resolved_at/>

                            <closed_at/>

                      </Prova>

                </Prova>

          </Risposta>

    </SOAP-ENV:Body>

</SOAP-ENV:Envelope>

I whould   like it to be like this:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<SOAP-ENV:Body>

  <Risposta>

    <Prova>

      <sys_id>717bc559370b6600ab75fa7b34990ebd</sys_id>

      <state>1</state>

      <resolved_at/>

      <closed_at/>

    </Prova>

    <Prova>

      <sys_id>f4fbc9190f87a200f39538b362050e93</sys_id>

      <state>1</state>

      <resolved_at/>

      <closed_at/>

    </Prova>

  </Risposta>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

Best regards

Luca

1 ACCEPTED SOLUTION

LucaP5957190930
Mega Sage

I found the solution!


Thanks to these two links:


How to append multiple child root nodes in an XML file while fetching data from MySQL Database - Sta...


and


http://wiki.servicenow.com/index.php?title=Level_Platforms_Integration#gsc.tab=0


the appendChild is the key!



This is my new code:



var xmldoc1 = new XMLDocument();


var nonno=xmldoc1.createElement("getRecordsResponse");


var numeri_multipli= request.numeri_multipli;


var gr1= new GlideRecord('incident');


gr1.addQuery('number','IN',numeri_multipli);


gr1.query();


if(!gr1.hasNext()){


  response.Esito= 'KO';


  response.CodiceEsito = '1';


  response.status_message = 'Chiamata in errore,numero inesistente o errato';


}


else{


  while(gr1.next()){



  var padre=xmldoc1.createElement("getRecordsResult");


  var sys_id=xmldoc1.createElement("sys_id",gr1.sys_id);


  padre.appendChild(sys_id);


  var state=xmldoc1.createElement("state",gr1.state);


  padre.appendChild(state);


  var resolved_at=xmldoc1.createElement("resolved_at",gr1.resolved_at);


  padre.appendChild(resolved_at);


  var closed_at=xmldoc1.createElement("closed_at",gr1.closed_at);


  padre.appendChild(closed_at);


  nonno.appendChild(padre);


  }



  response.soapResponseElement = xmldoc1.getDocumentElement();


}


View solution in original post

5 REPLIES 5

Mohammed Lais1
Mega Guru

Hi luca, try adding var ris=""; on line 27. and change line 34 to ris=xmldoc1.createElement("Prova");


Thanks for your reply, but unfortunatelly it doesen't work for me.


LucaP5957190930
Mega Sage

I found the solution!


Thanks to these two links:


How to append multiple child root nodes in an XML file while fetching data from MySQL Database - Sta...


and


http://wiki.servicenow.com/index.php?title=Level_Platforms_Integration#gsc.tab=0


the appendChild is the key!



This is my new code:



var xmldoc1 = new XMLDocument();


var nonno=xmldoc1.createElement("getRecordsResponse");


var numeri_multipli= request.numeri_multipli;


var gr1= new GlideRecord('incident');


gr1.addQuery('number','IN',numeri_multipli);


gr1.query();


if(!gr1.hasNext()){


  response.Esito= 'KO';


  response.CodiceEsito = '1';


  response.status_message = 'Chiamata in errore,numero inesistente o errato';


}


else{


  while(gr1.next()){



  var padre=xmldoc1.createElement("getRecordsResult");


  var sys_id=xmldoc1.createElement("sys_id",gr1.sys_id);


  padre.appendChild(sys_id);


  var state=xmldoc1.createElement("state",gr1.state);


  padre.appendChild(state);


  var resolved_at=xmldoc1.createElement("resolved_at",gr1.resolved_at);


  padre.appendChild(resolved_at);


  var closed_at=xmldoc1.createElement("closed_at",gr1.closed_at);


  padre.appendChild(closed_at);


  nonno.appendChild(padre);


  }



  response.soapResponseElement = xmldoc1.getDocumentElement();


}


Hi @Lucap

 

I tried this with xmldocument2 , its not working , Do we have a work around for xmldocument2?