Get a value from XML Request body

Dinesh90
Tera Contributor

Hello Developers :

My requirement is to fetch the specific value from the XML Request body.

Below is the code written. 

It is not giving the result of line 331, I think something with the line - 330.

It is executing well till 328

Dinesh90_0-1747935023390.png

Below is the attached xml request - from this I want to get the value of Pending Until i.e. 

<ReferenceValue>2025-04-29T10:00:00</ReferenceValue> I want to fetch this date & time.

Dinesh90_1-1747935203420.png

 

Please help to correct my code or suggest something new line of code to get the desired value from XML.

 

Thanks in advance

8 REPLIES 8

Zach N
Tera Guru

Why not try converting it to JSON? Based on KB0957116 that might make it easier to parse. Also, unless I'm mistaken since all nodes are named "ReferenceType," you'll need to loop through them. Try this:

var pendingdate = '';
var SoapRequestBody = source.u_message_xml.toString();

gs.log('soap body is' + SoapRequestBody);

if (JSUtil.notNil(SoapRequestBody)) {

    var jsonObj = gs.xmlToJSON(SoapRequestBody); // Convert to JSON Object

    try {

        gs.log('inside try');

        // Loop through the jsonObj looking for "Pending Until" and pull the 
        // ReferenceValue when found.
        //
        for (var i = 0; i < jsonObj.length; i++) {
            if (jsonObj[i].ReferenceType == 'Pending Until')
                pendingdate = jsonObj[i].ReferenceValue;
            
        }

    } catch (e) {

    }

}

 

Apologies if the above doesn't work. I don't have access to my PDI so I can't verify the code.

Hope that helps!

Dinesh90
Tera Contributor

Thanks for your response, code is not working..
It is giving the undefined length

Dinesh90_0-1747938562724.png

 

Sorry about that, I wasn't able to test before. Try updating the for loop as follows:

for (var i = 0; i < jsonObj.DocumentReference.length; i++) {
	if (jsonObj.DocumentReference[i].ReferenceType == 'Pending Until')
		pendingdate = jsonObj.DocumentReference[i].ReferenceValue;
}

 

Dinesh90
Tera Contributor

Still the length is undefined... 

Dinesh90_0-1747975991882.pngDinesh90_1-1747976010684.pngDinesh90_2-1747976044517.png