Monday, 11 October 2021

Groovy script for picking the field from xml in array and select the particular value

 

Groovy script

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*;
import groovy.util.logging.*;
import org.codehaus.*;


def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;


def XmlDataObject = new XmlSlurper().parseText(body)


def xmldata = []
def s="";
XmlDataObject.'**'.findAll { it.name()== 'UserServiceTransactionProcessingTypeCode'}.each { a ->
xmldata << a.text() }

message.setProperty("StatusCode",xmldata);
def newxmldata = xmldata as String[]
newxmldata = newxmldata.contains("ZCW1")

message.setProperty("StatusCode1",newxmldata);

return message;


}




Friday, 1 October 2021

Pick the xml field from the huge payload and remove square brackets

 Pick the xml field from the huge payload and remove square brackets.

Groovy Script:


import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

import groovy.json.*;

import groovy.util.logging.*;

import org.codehaus.*;


def Message processData(Message message) {

def body = message.getBody(java.lang.String) as String;


map = message.getProperties();

requestid = map.get("ReqId");

taskid = map.get("TaskId");


requestid= requestid.toString().replaceAll("\\[", "");

requestid= requestid.toString().replaceAll('\\]', "");


def XmlDataObject = new XmlSlurper().parseText(body)


def xmldata = []

def s="";

XmlDataObject.'**'.findAll { it.name()== 'request_id'}.each { a ->

xmldata << a.text() }



def last = xmldata.last()

def xmldata2 = []

XmlDataObject.'**'.findAll { it.name()== 'task_id'}.each { a ->

xmldata2 << a.text() }

def last2 = xmldata2.last()

message.setProperty("requestid1",last);

taskid= taskid.toString().replaceAll("\\[", "");

taskid= taskid.toString().replaceAll('\\]', "");

message.setProperty("taskid1",last2);

message.setProperty("taskid2",taskid);

return message;

}