VALUE MAPPING: EXCEPTION HANDLING FROM GROOVY SCRIPT,

 



https://community.sap.com/t5/technology-q-a/hci-groovy-try-catch-not-working/qaq-p/12176181

-------

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

import com.sap.it.api.ITApiFactory

import com.sap.it.api.mapping.ValueMappingApi


def Message processData(Message message) {

    // Properties

    def map = message.getProperties()

    def sourceValue = map.get("sourceValue")

    def senderAgency = map.get("senderAgency")

    def senderId = map.get("senderId")

    def receiverAgency = map.get("receiverAgency")

    def receiverId = map.get("receiverId")


    // Get ValueMappingApi service

    def valueMapApi = ITApiFactory.getService(ValueMappingApi.class, null)

    

    if (valueMapApi == null) {

        message.setHeader("Error1", "ValueMappingApi service is not available.")

        throw new RuntimeException("ValueMappingApi service is not available.")

    }


    // Get mapped value

    def targetValue

    try {

        targetValue = valueMapApi.getMappedValue(senderAgency, senderId, sourceValue, receiverAgency, receiverId)

        if (targetValue == null || targetValue.isEmpty()) {

            throw new RuntimeException("Mapped value is null or empty.")

        }

    } catch (Exception e) {

        message.setHeader("Error1", e.getMessage())

        throw e

    }


    // Set the mapped value to a message property

    message.setProperty("targetValue", targetValue)


    return message

}

/*


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

import com.sap.it.api.ITApiFactory

import com.sap.it.api.mapping.ValueMappingApi


def Message processData(Message message) {


    // Properties

    def map = message.getProperties()

    def sourceValue = map.get("sourceValue")

    

    def senderAgency = map.get("senderAgency")

    def senderId = map.get("senderId")

    def receiverAgency = map.get("receiverAgency")

    def receiverId = map.get("receiverId")


    // Get ValueMappingApi service

    def valueMapApi = ITApiFactory.getService(ValueMappingApi.class, null)

    

    if (valueMapApi == null) {

        def camelException = message.getProperty('CamelExceptionCaught')

        def exceptionMessage = "ValueMappingApi service is not available."

        if (camelException) {

            exceptionMessage += " Exception: ${camelException}"

        }

        throw new Exception(exceptionMessage)

    }


    // Get mapped value

    def targetValue = valueMapApi.getMappedValue(senderAgency, senderId, sourceValue, receiverAgency, receiverId)


    if (targetValue == null || targetValue.isEmpty()) {

        def camelException = message.getProperty('CamelExceptionCaught')

        def exceptionMessage = "Mapped value is null or empty."

        if (camelException) {

            exceptionMessage += " Exception: ${camelException}"

        }

        throw new Exception(exceptionMessage)

    }


    // Set the mapped value to a message property

    message.setProperty("targetValue", targetValue)


    return message

}*/



Popular posts from this blog

pss book : శ్రీకృష్ణుడు దేవుడా, భగవంతుడా completed , second review needed. 26th April 2024

pss book: గురు ప్రార్థనామంజరి . completed 21st july 2024

pss book: కధల జ్ఞానము read review pending. 25th june 2024