cancel
Showing results for 
Search instead for 
Did you mean: 

Need Help to extract value from message with groovy script in CPI

Jeevitha_04
Explorer
0 Kudos

Hi All,

Please help me in groovy script to extract the highlighted value from below message and set it as property"DOCNUM"DOCNUM.jpg

 

<?xml version='1.0' encoding='UTF-8'?>
<RootNode>
<Payload>
EDIXX3SSIP2000131 0000131 DELIN2.002.OD.02.S03 DELSCHD031 0000000000039067
EDIMSG
MID 1
MID 20231221
MID 0000
</Payload>
<MsgHeader>
            <EndPoint>ABCDE</EndPoint> 
            <EntryID>XXXYYY</EntryID>   
            <RetryCount>2</RetryCount>          
        </MsgHeader>
</RootNode>

 

Your help would be highly appreciated !

Thanks in advance

Jeevitha

View Entire Topic
ag3silaus
Participant
0 Kudos

Hello @Jeevitha_04 ,

I hope this solution aligns with your needs. I came across this suggestion while using ChatGPT.

Considering that your ID is always 16 digits long, I believe this solution might be suitable for your case. Could you please provide more details or clarify if this fits your requirements?

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

def Message processData(Message message) {
    //Body
    def body = message.getBody();
    def rootNode = new XmlSlurper().parseText(body);
    def payload = rootNode.Payload.text();
    def pattern = ~/(\d{16})/
    def matcher = (payload =~ pattern)
    if (matcher.find()) {
        def matchedDigits = matcher.group(1)
    } else {
        println "No match found"
    }
    def properties = message.getProperties();
    message.setProperty("newProperty", matcher.group());
    return message;
}

ag3silaus_0-1712096366744.png

 

Best regards,

Burak

Jeevitha_04
Explorer
0 Kudos
Thank you Burak,