How to parse soap response body

Anil kumar Sha2
Mega Expert

Hello Team,

i have received the record using soap getMethod but i am not able to get the value of description and short description  to create the incident in servicenow.

i tried from below method  but value is not coming of description and short description. Plsl help

Var obj=new XMLHelper().toObject(responseBody); 

gs.log('objeInfo=' +obj);
assetDescription=obj["SOAP-ENV:Body"].getRecordsResponse.getRecordsResult.description;
gs.log("assetDescription=" +assetDescription)

 

 

 

scriptinclude=:<?xml version='1.0' encoding='UTF-8'?><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><getRecordsResponse><getRecordsResult><active>0</active><activity_due></activity_due><additional_assignee_list/><approval>not requested</approval><approval_set></approval_set><assigned_to>9ee1b13dc6112271007f9d0efdb69cd0</assigned_to><assignment_group>287ee6fea9fe198100ada7950d0b1b73</assignment_group><business_duration>1970-01-22 14:03:10</business_duration><business_service/><business_stc>1864990</business_stc><calendar_duration>1970-04-01 21:03:10</calendar_duration><calendar_stc>7851790</calendar_stc><caller_id>5137153cc611227c000bbd1bd8cd2005</caller_id><category>database</category><caused_by/><child_incidents>0</child_incidents><close_code>Closed/Resolved by Caller</close_code><close_notes>Closed before close notes were made mandatory
</close_notes><closed_at>2020-02-18 22:54:55</closed_at><closed_by>9ee1b13dc6112271007f9d0efdb69cd0</closed_by><cmdb_ci/><comments_and_work_notes></comments_and_work_notes><company>31bea3d53790200044e0bfc8bcbe5dec</company><contact_type>phone</contact_type><contract/><correlation_display/><correlation_id/><delivery_plan/><delivery_task/><description>Currently running 10GR1 and need to upgrade to 10GR2.</description><due_date></due_date><escalation>0</escalation><expected_start></expected_start><follow_up></follow_up><group_list/><hold_reason>0</hold_reason><impact>2</impact><incident_state>7</incident_state><knowledge>0</knowledge><location>108752c8c611227501d4ab0e392ba97f</location><made_sla>0</made_sla><notify>1</notify><number>INC0000010</number><opened_at>2020-02-13 22:53:02</opened_at><opened_by>9ee1b13dc6112271007f9d0efdb69cd0</opened_by><order>0</order><parent/><parent_incident/><priority>4</priority><problem_id/><reassignment_count>2</reassignment_count><reopen_count>0</reopen_count><reopened_by/><reopened_time></reopened_time><resolved_at>2020-05-14 19:56:12</resolved_at><resolved_by>6816f79cc0a8016401c5a33be04be441</resolved_by><rfc/><service_offering/><severity>3</severity><short_description>Geeta rani From Bareilly </short_description><skills/><sla_due></sla_due><sn_esign_document/><sn_esign_esignature_configuration/><sn_hr_le_activity/><state>7</state><subcategory/><sys_class_name>incident</sys_class_name><sys_created_by>don.goodliffe</sys_created_by><sys_created_on>2020-02-25 22:54:41</sys_created_on><sys_domain>global</sys_domain><sys_domain_path>/</sys_domain_path><sys_id>46b9490da9fe1981003c938dab89bda3</sys_id><sys_mod_count>10</sys_mod_count><sys_updated_by>incidentAlert</sys_updated_by><sys_updated_on>2020-09-27 06:40:49</sys_updated_on><time_worked></time_worked><u_incident_alert/><u_sap_number/><u_ts_danc/><upon_approval/><upon_reject/><urgency>3</urgency><user_input></user_input><watch_list/><work_end></work_end><work_notes_list/><work_start></work_start></getRecordsResult><getRecordsResult><active>1</active><activity_due></activity_due><additional_assignee_list/><approval>not requested</approval><approval_set></approval_set><assigned_to/><assignment_group/><business_duration></business_duration><business_service/><business_stc>0</business_stc><calendar_duration></calendar_duration><calendar_stc>0</calendar_stc><caller_id>cc6bba63079310104d05f16c7c1ed0e9</caller_id><category>database</category><caused_by/><child_incidents>0</child_incidents><close_code/><close_notes/><closed_at></closed_at><closed_by/><cmdb_ci/><comments_and_work_notes></comments_and_work_notes><company/><contact_type/><contract/><correlation_display/><correlation_id/><delivery_plan/><delivery_task/><description>Testing999999999</description><due_date></due_date><escalation>0</escalation><expected_start></expected_start><follow_up></follow_up><group_list/><hold_reason>0</hold_reason><impact>3</impact><incident_state>1</incident_state><knowledge>0</knowledge><location/><made_sla>1</made_sla><notify>1</notify><number>INC0010722</number><opened_at>2020-09-23 08:35:37</opened_at><opened_by>cc6bba63079310104d05f16c7c1ed0e9</opened_by><order>0</order><parent/><parent_incident/><priority>5</priority><problem_id/><reassignment_count>0</reassignment_count><reopen_count>0</reopen_count><reopened_by/><reopened_time></reopened_time><resolved_at></resolved_at><resolved_by/><rfc/><service_offering/><severity>3</severity><short_description>Geeta rani From Bareilly </short_description><skills/><sla_due></sla_due><sn_esign_document/><sn_esign_esignature_configuration/><sn_hr_le_activity/><state>1</state><subcategory>db2</subcategory><sys_class_name>incident</sys_class_name><sys_created_by>incidentAlert</sys_created_by><sys_created_on>2020-09-23 08:35:37</sys_created_on><sys_domain>global</sys_domain><sys_domain_path>/</sys_domain_path><sys_id>eabe4faf079310104d05f16c7c1ed0cd</sys_id><sys_mod_count>2</sys_mod_count><sys_updated_by>incidentAlert</sys_updated_by><sys_updated_on>2020-09-27 06:41:39</sys_updated_on><time_worked></time_worked><u_incident_alert/><u_sap_number/><u_ts_danc/><upon_approval>proceed</upon_approval><upon_reject>cancel</upon_reject><urgency>3</urgency><user_input></user_input><watch_list/><work_end></work_end><work_notes_list/><work_start></work_start></getRecordsResult><getRecordsResult><active>1</active><activity_due></activity_due><additional_assignee_list/><approval>not requested</approval><approval_set></approval_set><assigned_to/><assignment_group>5f63e48fc0a8010e00eeaad81cd4dd37</assignment_group><business_duration></business_duration><business_service/><business_stc>0</business_stc><calendar_duration></calendar_duration><calendar_stc>0</calendar_stc><caller_id>cc6bba63079310104d05f16c7c1ed0e9</caller_id><category>database</category><caused_by/><child_incidents>0</child_incidents><close_code/><close_notes/><closed_at></closed_at><closed_by/><cmdb_ci/><comments_and_work_notes></comments_and_work_notes><company/><contact_type/><contract/><correlation_display/><correlation_id/><delivery_plan/><delivery_task/><description>HanuMaansdsasdasd22222</description><due_date></due_date><escalation>0</escalation><expected_start></expected_start><follow_up></follow_up><group_list/><hold_reason>0</hold_reason><impact>3</impact><incident_state>2</incident_state><knowledge>0</knowledge><location/><made_sla>1</made_sla><notify>1</notify><number>INC0010755</number><opened_at>2020-09-24 11:29:51</opened_at><opened_by>cc6bba63079310104d05f16c7c1ed0e9</opened_by><order>0</order><parent/><parent_incident/><priority>5</priority><problem_id/><reassignment_count>3</reassignment_count><reopen_count>0</reopen_count><reopened_by/><reopened_time></reopened_time><resolved_at></resolved_at><resolved_by/><rfc/><service_offering/><severity>3</severity><short_description>Geeta rani From Bareilly </short_description><skills/><sla_due></sla_due><sn_esign_document/><sn_esign_esignature_configuration/><sn_hr_le_activity/><state>2</state><subcategory>sql server</subcategory><sys_class_name>incident</sys_class_name><sys_created_by>incidentAlert</sys_created_by><sys_created_on>2020-09-24 11:29:51</sys_created_on><sys_domain>global</sys_domain><sys_domain_path>/</sys_domain_path><sys_id>3030c948076350104d05f16c7c1ed07d</sys_id><sys_mod_count>22</sys_mod_count><sys_updated_by>incidentAlert</sys_updated_by><sys_updated_on>2020-09-27 06:41:00</sys_updated_on><time_worked></time_worked><u_incident_alert>142009042fa3501030e8532a2799b655</u_incident_alert><u_sap_number/><u_ts_danc/><upon_approval>proceed</upon_approval><upon_reject>cancel</upon_reject><urgency>3</urgency><user_input></user_input><watch_list/><work_end></work_end><work_notes_list/><work_start></work_start></getRecordsResult></getRecordsResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

1 ACCEPTED SOLUTION
14 REPLIES 14

Following will get description and short_description in pairs.

var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(scriptinclude); 

var recordsResult = xmlDoc.getFirstNode("//getRecordsResult");
while (recordsResult) {
  if (recordsResult) {
    var iter = recordsResult.getChildNodeIterator();
      while(iter.hasNext()) {
        var n = iter.next();
        if (n.getNodeName() == 'description' || n.getNodeName() == 'short_description') {
          //gs.info(n.getNodeName());
          gs.info(n.getTextContent());
        }
      }
  recordsResult = xmlDoc.getNextNode(recordsResult);
  }
}

Lot of thanks Sir for your effort:

description and short description  value is coming in below mention format. but i want to create the incident based on

every description and short description value.

ex:

description value 123

short description value testing

  then  incident should be create. if we have multiple value for description and short description then incident should be create.

Pls let  provide the solution ?

 

 

find_real_file.png

The second sample above get all the getRecordsResult and iterate through that to get the description and subdescription in a set. Creating an incident within the outer while loop should create a record for each row from the application.

Hi Sir,

Pls help to create an incident, I updated the incident creation code outer of while loop but the incident is not being created. Pls help

 

var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(responseBody);

var recordsResult = xmlDoc.getFirstNode("//getRecordsResult");
while (recordsResult) {
if (recordsResult) {
var iter = recordsResult.getChildNodeIterator();
while(iter.hasNext()) {
var n = iter.next();
if (n.getNodeName() == 'description' || n.getNodeName() == 'short_description') {
//gs.info(n.getNodeName());
gs.info(n.getTextContent());
gs.log("GetDetail=" +n.getTextContent());

}
var grinc= new GlideRecord('incident');
grinc.gr.initialize();
grinc.description=n.getTextContent();
grinc.short_description=n.getTextContent();
grinc.insert();
}
recordsResult = xmlDoc.getNextNode(recordsResult);
}

}

Try the following code.

var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(responseBody);

var recordsResult = xmlDoc.getFirstNode("//getRecordsResult");
while (recordsResult) {
    if (recordsResult) {
        var description = '';
        var short_description = '';
        var iter = recordsResult.getChildNodeIterator();
        while (iter.hasNext()) {
            var n = iter.next();
            switch (n.getNodeName()) {
                case 'description':
                    description = n.getTextContent();
                    break;
                case 'short_description':
                    short_description = n.getTextContent();
                    break;
            }
        }

        var grinc = new GlideRecord('incident');
        grinc.gr.initialize();
        grinc.description = description;
        grinc.short_description = short_description;
        grinc.insert();
    }
    recordsResult = xmlDoc.getNextNode(recordsResult);
}