cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy Script Parse Error

jbesl
Discoverer
0 Kudos

Can you help me with a SAP CPI groovy script ?
Objective : I want to merge the content of an exchange property with XML content and the incoming payload by referencing a key <userId> in my case.

the sample format of the xml in the exchange property is this

 

<?xml version="1.0" encoding="UTF-8"?>
<rootA>
	<cust_A_WFM>
		<cust_ADPWFMStatus>I</cust_ADPWFMStatus>
		<externalCodeNav>
			<User>
				<userId>123456</userId>
			</User>
		</externalCodeNav>
		<lastModifiedDateTime>*</lastModifiedDateTime>
		<externalCode>*</externalCode>
		<effectiveStartDate>*</effectiveStartDate>
	</cust_ADP_WFM>
	<cust_ADP_WFM>
		<cust_ADPWFMStatus>I</cust_ADPWFMStatus>
		<externalCodeNav>
			<User>
				<userId>987654</userId>
			</User>
		</externalCodeNav>
		<lastModifiedDateTime>*</lastModifiedDateTime>
		<externalCode>*</externalCode>
		<effectiveStartDate>2*</effectiveStartDate>
	</cust_A_WFM>
</rootA>                        

 

While the incoming payload, is this 

 

 

I am using this script

 

import com.sap.gateway.ip.core.customdev.util.Message
import groovy.xml.XmlUtil
import groovy.xml.XmlParser

Message processData(Message message) {
    // Get the incoming payload
    def body = message.getBody(java.lang.String) as String

    // Get the exchange property containing cust_A_WFM
    def custADPWFMPayload = message.getProperty('backup_call1') as String

    // Remove the XML declaration if present
    body = body.replaceFirst(/\<\?xml.*?\?\>/, "").trim()
    custAWFMPayload = custAWFMPayload.replaceFirst(/\<\?xml.*?\?\>/, "").trim()

    // Parse both XMLs
    def parser = new XmlParser(false, false) // Disable namespace aware parsing
    def rootEmpJob = parser.parseText(body)
    def rootADP = parser.parseText(custAWFMPayload)

    // Convert the rootADP to a map for easy lookup
    def adpMap = rootADP.cust_A_WFM.collectEntries { adp ->
        [adp.externalCodeNav.User.userId.text(), adp]
    }

    // Iterate over each EmpJob element and merge, or remove if no match
    def empJobsToRemove = []
    rootEmpJob.EmpJob.each { empJob ->
        def userId = empJob.userId.text()
        def correspondingA = aMap[userId]
        if (correspondingA) {
            // Create a new node for externalCodeOfcust_A_WFMNav if it doesn't exist
            def userNav = empJob.userNav[0]
            if (userNav) {
                def externalCodeOfcust_A_WFMNav = userNav.externalCodeOfcust_A_WFMNav ?: userNav.appendNode('externalCodeOfcust_A_WFMNav')
                // Append the corresponding ADP data
                externalCodeOfcust_A_WFMNav.append(correspondingA)
            }
        } else {
            // Add to the list of nodes to remove
            empJobsToRemove << empJob
        }
    }

    // Remove the unmatched EmpJob nodes
    empJobsToRemove.each { empJob ->
        rootEmpJob.remove(empJob)
    }

    // Convert the merged XML back to string and set it as the message body
    def mergedXml = XmlUtil.serialize(rootEmpJob)
    message.setBody(mergedXml)

    return message
}

 

But i get this error 

 

javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
ADP_mergeXML_custADP_empData__Script.groovy: 3: unable to resolve class groovy.xml.XmlParser
@ line 3, column 1.
import groovy.xml.XmlParser
^

1 error
, cause: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
ADP_mergeXML_custADP_empData__Script.groovy: 3: unable to resolve class groovy.xml.XmlParser
@ line 3, column 1.
import groovy.xml.XmlParser
^

1 error

 

 

View Entire Topic
Ryan-Crosby
Active Contributor
0 Kudos

Change your import on line 3 to - 

import groovy.util.XmlParser

 

Regards,

Ryan Crosby 

jbesl
Discoverer
0 Kudos
Hello Ryan, this is my first time raising a question here, glad to have very quick response. Thank you it worked actually but i am getting a different error now
Ryan-Crosby
Active Contributor
0 Kudos
@jbesl best to post a new question outlining your new problem if you don't understand the issue or what to do next. There are quite a few folks on here that can assist you.