cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CPI timer in groovy Script to check incoming XML

ritu2507
Explorer
0 Kudos

Hi Everyone ,

I have a scenario where I need to fetch all the stored data using Select step and then based on that fetched XML message we need to check the Date segment . If that date segment time value is > current date - time +6 min then we need to select all the document Id for them .. But using my Groovy I am getting error cannot convert undefined or null to object .

Can some one help me with it

My inbound XML look like --

<?xml version='1.0' encoding='UTF-8'?><messages> <message id="1234575"> <AMC> <documentId>1234575</documentId> <details>1 - Litmos Dojo - WHITE BELT</details> <sign>Checked</sign> <date>2023-08-09T20:28:47</date> <product>XYAjhgpx0-M1</product> </AMC> </message> <message id="1234576"> <AMC> <documentId>1234576</documentId> <details>1 - Litmos Dojo - WHITE BELT</details> <sign>Checked</sign> <date>2023-08-09T20:28:47</date> <product>XYAjhgpx0-M1</product> </AMC> </message> <message id="1234577"> <AMC> <documentId>1234577</documentId> <details>1 - Litmos Dojo - WHITE BELT</details> <sign>Checked</sign> <date>2023-08-09T20:28:47</date> <product>XYAjhgpx0-M1</product> </AMC> </message></messages> 
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.xml.XmlUtil;
import groovy.xml.StreamingMarkupBuilder;
import groovy.xml.*;

def currentDateTime = new Date()
def futureDateTime = currentDateTime + (6 * 60 * 1000) // Adding 6 minutes in milliseconds

def xmlRoot = new XmlSlurper().parseText(inboundXml)

xmlRoot.message.each { msg ->
    def dateStr = msg.AMC.date.text()
    def messageDateTime = Date.parse("yyyy-MM-dd'T'HH:mm:ss", dateStr)

    if (messageDateTime > futureDateTime) {
        def documentId = msg.AMC.documentId.text()
        println("Document ID: $documentId")
 }
}

Reagrds

Ritu

AntonioJVaz
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi,

I believe you are doing the wrong calculation on the futureDateTime, try changing it to:

def futureDateTime = new Date(currentDateTime.toInstant().toEpochMilli() + (6 * 60 * 1000)) // Adding 6 minutes in milliseconds

Accepted Solutions (0)

Answers (0)