Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Dynamic SOAP message

Nitesh Kumar4
Tera Contributor

Hello Friends,

Need your help, below code is not working dynamic, any mistake in request body. Creating the record in external system but caller and short_description is not dynamic it is inserting ${sd}, ${caller_id}.

 

var sp = new sn_ws.SOAPMessageV2();
    var user = "inc.integration";
    var pass = "xxxxxxxxxxxxxx";
    sp.setBasicAuth(user, pass);
    sp.setStringParameterNoEscape('sd', current.short_description);
    sp.setStringParameterNoEscape('cid', current.caller_id);

    sp.setRequestBody('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:inc="http://www.service-now.com/incident"><soapenv:Header/><soapenv:Body><inc:insert><caller_id>${cid}</caller_id><short_description>${sd}</short_description></inc:insert></soapenv:Body></soapenv:Envelope>');

    var response = sp.execute();
    var responseBody = response.getBody();

    var xmlDoc = new XMLDocument2();
    xmlDoc.parseXML(responseBody);
    gs.addInfoMessage(xmlDoc.getNodeText('//sys_id'));
    gs.addInfoMessage(xmlDoc.getNodeText('//number'));
6 REPLIES 6

@Nitesh Kumar4 

Thank you for marking my response as helpful.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

MaxMixali
Kilo Sage

// Configurazione centralizzata
var config = {
endpoint: 'https://xxxxxx.service-now.com/incident.do?SOAP',
soapAction: 'http://www.service-now.com/incident/insert',
username: 'inc.integration',
password: 'xxxxxxxxxxxxxx'
};

// Funzione per costruire il body SOAP dinamicamente
function buildSOAPBody(params) {
var body = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ' +
'xmlns:inc="http://www.service-now.com/incident">' +
'<soapenv:Header/>' +
'<soapenv:Body>' +
'<inc:insert>';

// Aggiungi dinamicamente tutti i parametri
for (var key in params) {
if (params.hasOwnProperty(key) && params[key]) {
body += '<' + key + '>' + GlideStringUtil.escapeForHomePage(params[key]) + '</' + key + '>';
}
}

body += '</inc:insert>' +
'</soapenv:Body>' +
'</soapenv:Envelope>';

return body;
}

// Funzione principale per creare l'incident
function createIncidentSOAP(incidentData) {
try {
var sp = new sn_ws.SOAPMessageV2();
sp.setEndpoint(config.endpoint);
sp.setSOAPAction(config.soapAction);
sp.setBasicAuth(config.username, config.password);

// Costruisci il body SOAP dinamicamente
var soapBody = buildSOAPBody(incidentData);
sp.setRequestBody(soapBody);

// Esegui la richiesta
var response = sp.execute();
var httpStatus = response.getStatusCode();

if (httpStatus != 200) {
gs.error('SOAP Request failed with status: ' + httpStatus);
return null;
}

// Parse della risposta
var responseBody = response.getBody();
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(responseBody);

var result = {
sys_id: xmlDoc.getNodeText('//sys_id'),
number: xmlDoc.getNodeText('//number')
};

// Log dei risultati
if (result.sys_id && result.number) {
gs.addInfoMessage('Incident creato con successo');
gs.addInfoMessage('Sys ID: ' + result.sys_id);
gs.addInfoMessage('Number: ' + result.number);
} else {
gs.error('Risposta SOAP non valida');
}

return result;

} catch (ex) {
gs.error('Errore durante la chiamata SOAP: ' + ex.message);
return null;
}
}

// Esempio di utilizzo con dati dinamici
var incidentData = {
caller_id: current.caller_id.toString(),
short_description: current.short_description.toString(),
// Aggiungi altri campi dinamicamente secondo necessità
// urgency: current.urgency.toString(),
// impact: current.impact.toString(),
// category: current.category.toString()
};

// Esegui la chiamata
var result = createIncidentSOAP(incidentData);

 

 

 

 

Modifiche effettuate

  1. Configurazione centralizzata - parametri facilmente modificabili
  2. Costruzione dinamica del body SOAP - accetta qualsiasi numero di campi
  3. Gestione errori robusta - try/catch e controllo status HTTP
  4. Escape automatico dei dati - prevenzione di injection XML
  5. Funzioni riutilizzabili - codice più manutenibile
  6. Logging migliorato - messaggi di errore e successo più chiari
  7.