Thursday, 21 November 2024

Groovy Script to use the mod and divide on the count in sap cpi

Groovy script

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

import groovy.xml.*;

import java.text.SimpleDateFormat;

import groovy.json.*;

import groovy.util.*;

import groovy.xml.MarkupBuilder;

def Message processData(Message message) {

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



def headers = message.getHeaders();

def count = headers.get("count");

count = count.toInteger()

// def count = 340;

int ct = count / 100

int remain = count % 100



StringWriter writer = new StringWriter()

MarkupBuilder xml = new MarkupBuilder(writer)



xml.Nodes {

(1..ct).each { index ->

Node {



Value(100)

}

}

if(remain > 0 ){

Node{

Value(remain)

}

}

}


def resultXML = writer.toString()

message.setBody(resultXML)

return message;

}

Input:

CM - header

 count = 340;

Output:



<Nodes>

<Node>

<Value>100</Value>

</Node>

<Node>

<Value>100</Value>

</Node>

<Node>

<Value>100</Value>

</Node>

<Node>

<Value>40</Value>

</Node>

</Nodes>





No comments:

Post a Comment