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
}*/