How to read data in xml tags

preethigovi
Tera Contributor

Hi Team,

 

Iam getting data from workday integration on workers API. I need to built xpath so it provides us the data.

 

wd:Worker/wd:Worker_Data/wd:Transaction_Log_Corrected_And_Rescinded_Data/wd:Corrected_Or_Rescinded_Transaction_Log_Data/wd:Main_Transaction_Data/wd:Transaction_Log_Data[wd:Is_Rescind_Or_Rescinded='1' and wd:Transaction_Log_Type_Reference/wd:ID[@wd:type='Business_Process_Type']='Terminate Employee']");
 
Above is the xpath of node that gives the output as:
Terminate: AAAC(00443221) (Rescinded)
                                        2024-06-30T00:00:00.000-07:00
                                        2024-05-30T02:21:44.787-07:00

                                            04845bae8a1e42e2ab6da831fef34fb1
                                            Terminate Employee


                                            79e02808d8b31001049bb695db3c0001
                                            38111714


                                            d3ed83aaea99010a390dd7e0ed004c7c
                                            00460131


                                            149748d319111025a59d54cb84843633
                                            48000774

                                        1
                                        0
 
From this i need to read the value 1 and Terminate employee.
How we can achieve this using servicenow logic.
6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@preethigovi 

convert that to XML to JSON and then parse the JSON

something like this

var xmlString = '<wd:Worker><wd:Worker_Data><wd:Transaction_Log_Corrected_And_Rescinded_Data><wd:Corrected_Or_Rescinded_Transaction_Log_Data><wd:Main_Transaction_Data><wd:Transaction_Log_Data><wd:Is_Rescind_Or_Rescinded>1</wd:Is_Rescind_Or_Rescinded><wd:Transaction_Log_Type_Reference><wd:ID wd:type="Business_Process_Type">Terminate Employee</wd:ID></wd:Transaction_Log_Type_Reference></wd:Transaction_Log_Data></wd:Main_Transaction_Data></wd:Corrected_Or_Rescinded_Transaction_Log_Data></wd:Transaction_Log_Corrected_And_Rescinded_Data></wd:Worker_Data></wd:Worker>';
var jsonObj = gs.xmlToJSON(xmlString); // Converts XML to JSON object

//gs.info(JSON.stringify(jsonObj));

var txnData = jsonObj["wd:Worker"]["wd:Worker_Data"]["wd:Transaction_Log_Corrected_And_Rescinded_Data"]
  ["wd:Corrected_Or_Rescinded_Transaction_Log_Data"]["wd:Main_Transaction_Data"]["wd:Transaction_Log_Data"];

var isRescind = txnData["wd:Is_Rescind_Or_Rescinded"]; // "1"
var processType = txnData["wd:Transaction_Log_Type_Reference"]["wd:ID"]["content"]; // "Terminate Employee"

// Output (for ServiceNow, use gs.info)
gs.info("Is Rescind: " + isRescind); // Output: Is Rescind: 1
gs.info("Process Type: " + processType); // Output: Process Type: T

Output:

AnkurBawiskar_0-1750866991038.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@preethigovi 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi @Ankur Bawiskar ,

But in OOB flow Look up employment data stream we use xpath to read the xml

 

eg: if i take rehire we hit the workday and use this to get the value for rehire(1/0)

var Rehire= xmlDoc.getNodeText("wd:Worker/wd:Worker_Data/wd:Employment_Data/wd:Worker_Status_Data/wd:Rehire");
 
Use case now is to get the Is rescind and business process type value. If i traverse single xpath i get the value.
challenge is that an employee can have multiple block of xml with this pair Is rescind and Business process type. Like his bonus/pay/service anything can be rescinded,
In this we need to find out block/node where is rescind is 1 and business process type is Terminate. And further divide those fields.
 
var Rescind_Terminations = xmlDoc.getNode("wd:Worker/wd:Worker_Data/wd:Transaction_Log_Corrected_And_Rescinded_Data/wd:Corrected_Or_Rescinded_Transaction_Log_Data/wd:Main_Transaction_Data/wd:Transaction_Log_Data[wd:Is_Rescind_Or_Rescinded='1' and wd:Transaction_Log_Type_Reference/wd:ID[@wd:type='Business_Process_Type']='Terminate Employee']");
 
When i run this data stream I get null value and error as;preethigovi_0-1750915705606.png

 

@preethigovi 

so you are saying there could be multiple objects and each might contain those 2 keys?

you can pass that XML to a script and handle the parsing there

Sorry but I am not knowing your current setup and can only help with what information has been shared.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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