How to return a value from a script step in flow designer.

Sonu Singh
Mega Expert

Hi All,

I need to fetch a value from the response body which i have received it from the SOAP step:

Fig:

find_real_file.png

Can some help me how do i fetch any value from the xml response which i am getting in the responsebody input variable?

 

Thanks,

Ak

1 ACCEPTED SOLUTION

So the problem is the body which is returned from integration.

See formatted XML

find_real_file.png

Below works but it's not good for long time solution.

var response = inputs.response_body.toString();

var xmldoc = new XMLDocument(response);

if(xmldoc.toString().indexOf('name=') > -1){
	var nm1 = (xmldoc.toString().split('name="')[1]);
	var nm2 = (nm1.toString().split('"')[0]);
	outputs.groupname = nm2;
}

View solution in original post

41 REPLIES 41

Hi,

the xml doesn't have proper starting and closing tags i.e. < and >

Regards
Ankur

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

Hi,

If the response is proper as below; you should be able to get the value as below

Please update code as below

(function execute(inputs, outputs) {
// ... code ...
var responseBody = inputs.response_body;

var xmldoc = new XMLDocument(responseBody);

var name = xmldoc.getAttribute("//row", "name");
var email = xmldoc.getAttribute("//row", "email");

outputs.groupname = name;
})(inputs, outputs);

Sample script which I ran in background script

var xml = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetSupportSpecialistByLocationResponse xmlns="http://nda.sys.com/"><GetSupportSpecialistByLocationResult><nda-response id="getassociations" status="OK"><association><people></people><groups><row name="India - Banaglore" email="indbgl@xyz.com" /></groups></association></nda-response></GetSupportSpecialistByLocationResult></GetSupportSpecialistByLocationResponse></soap:Body></soap:Envelope>';

var xmldoc = new XMLDocument(xml);

var name = xmldoc.getAttribute("//row", "name");
var email = xmldoc.getAttribute("//row", "email");

gs.info(name);
gs.info(email);

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hi @Ankur Bawiskar 

I am getting the below response from the soap.

I copy pasted the rendered HTML in background script and  execute it with the below script, where i got the output.

var response = inputs.response_body;
var xmldoc = new XMLDocument(response);

var name = xmldoc.getNodeText("//row/@name")

gs.print(name);

below are the same response which i got from soap step.

so i question is how can i pass Rendered HTML thing to script step so that i can fetch any node.

 

@Ankur Bawiskar ,

 

on passing the soap response to xml parser i am getting the response as below:

{
"Envelope": {
"Body": {
"name$": "Body",
"GetSupportSpecialistByLocationResponse": {
"name$": "GetSupportSpecialistByLocationResponse",
"GetSupportSpecialistByLocationResult": "&lt;nda-response id=\"getassociations\" status=\"OK\"&gt;&lt;association from_entity_guid=\"123456789\" business_process_role_guid=\"123456789\"&gt;&lt;people&gt;&lt;/people&gt;&lt;groups&gt;&lt;row group_guid=\"123456789\" name=\"IND - BGL\" email=\"indbgl@xyz.com\" ordinal=\"0\" description=\"\" association_guid=\"123456789\"/&gt;&lt;/groups&gt;&lt;/association&gt;&lt;/nda-response&gt;"
}
}
}
}

 

if i pass this response to Script step, can i fetch those values?

regards,

Ak

Hi,

so now you are converting xml to json?

Regards
Ankur

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