AdditionalPropertiesCreation_Updated 1 2 3 code on 5th march 2025
AdditionalPropertiesCreation_Updated
import com.sap.gateway.ip.core.customdev.util.Message;
def Message processData(Message message) {
// Retrieve the body, though it's not used in this case
def body = message.getBody();
def headers = message.getHeaders();
def map = message.getProperties();
// Retrieve necessary headers
def sourceType = headers.get('sourceType');
def destinationType = headers.get('destinationType');
def sourceLocation = headers.get('sourceLocation');
def destinationLocation = headers.get('destinationLocation');
def despatchDeliveryId = headers.get('despatchDeliveryId');
def postingDate = headers.get('postingDate');
def externalUniqueReference = headers.get('externalUniqueReference');
def customerPurchaseOrderNumber = headers.get('customerPurchaseOrderNumber');
def ssccNumber = headers.get('ssccNumber');
def materialReference = headers.get('materialReference');
def batch = headers.get('batch');
def sourceGlobalLocationNumber = headers.get('sourceGlobalLocationNumber');
def destinationGlobalLocationNumber = headers.get('destinationGlobalLocationNumber');
// Additional fields
def Scope = headers.get("Scope");
def Direction = headers.get("Direction");
def TerminalState = headers.get("TerminalState");
def Destination = headers.get("Destination");
def ProcessingState = headers.get("ProcessingState");
def PartnerID = map.get("FieldProperty").split("\\|")[2];
def Country = map.get("FieldProperty").split("\\|")[0];
// Create a combined header string
def combinedHeader = "";
// Helper function to add properties to combined header
def addHeaderProperty = { String key, String value ->
if (value != null && value != "") {
combinedHeader += "\"additionalProperty.${key}\": \"${value}\", ";
} else {
combinedHeader += "\"additionalProperty.${key}\": \"\", "; // Add empty string if null or empty
}
}
// Add properties to combined header
addHeaderProperty("Scope", Scope);
addHeaderProperty("Direction", Direction);
addHeaderProperty("TerminalState", TerminalState);
addHeaderProperty("Destination", Destination);
addHeaderProperty("ProcessingState", ProcessingState);
addHeaderProperty("sourceType", sourceType);
addHeaderProperty("destinationType", destinationType);
addHeaderProperty("sourceLocation", sourceLocation);
addHeaderProperty("destinationLocation", destinationLocation);
addHeaderProperty("despatchDeliveryId", despatchDeliveryId);
addHeaderProperty("postingDate", postingDate);
addHeaderProperty("externalUniqueReference", externalUniqueReference);
addHeaderProperty("customerPurchaseOrderNumber", customerPurchaseOrderNumber);
addHeaderProperty("ssccNumber", ssccNumber);
addHeaderProperty("materialReference", materialReference);
addHeaderProperty("sourceGlobalLocationNumber", sourceGlobalLocationNumber);
addHeaderProperty("destinationGlobalLocationNumber", destinationGlobalLocationNumber);
addHeaderProperty("PartnerID", PartnerID);
addHeaderProperty("Country", Country);
// Trim any trailing comma and space
combinedHeader = combinedHeader.trim();
if (combinedHeader.endsWith(",")) {
combinedHeader = combinedHeader[0..-1]; // Remove trailing comma
}
// If combinedHeader is empty, set the default value to the new header
if (combinedHeader.isEmpty()) {
message.setHeader("combinedProperties", "");
} else {
message.setHeader("combinedProperties", combinedHeader);
}
return message;
}
-------------
AdditionalPropertiesCreation_Updated1
import com.sap.gateway.ip.core.customdev.util.Message;
def Message processData(Message message) {
// Retrieve the body, though it's not used in this case
def body = message.getBody();
// Get the headers from the message
def headers = message.getHeaders();
def map = message.getProperties();
// Retrieve the values for 'destinationType' and 'ssccNumber' from headers
/*
message.setHeader('sourceType', json.despatchStock.sourceType)
message.setHeader('destinationType', json.despatchStock.destinationType)
message.setHeader('sourceLocation', json.despatchStock.source.location)
message.setHeader('destinationLocation', json.despatchStock.destination.location)
message.setHeader('despatchDeliveryId', json.despatchStock.despatchDeliveryId)
message.setHeader('postingDate', json.despatchStock.postingDate)
message.setHeader('externalUniqueReference', json.despatchStock.externalUniqueReference)
// Setting item-related headers for the first item (assuming single-item processing)
def firstItem = json.despatchStock.items.item[0] // Accessing the first item
message.setHeader('customerPurchaseOrderNumber', firstItem.customerPurchaseOrderNumber)
message.setHeader('ssccNumber', firstItem.ssccNumber)
message.setHeader('materialReference', firstItem.materialReference)
message.setHeader('batch', firstItem.batch)
// Global location numbers for source and destination
message.setHeader('sourceGlobalLocationNumber', json.despatchStock.source.globalLocationNumber)
message.setHeader('destinationGlobalLocationNumber', json.despatchStock.destination.globalLocationNumber)
*/
def sourceType = headers.get("sourceType")
def destinationType = headers.get("destinationType")
def sourceLocation = headers.get("sourceLocation")
def destinationLocation = headers.get("destinationLocation")
def despatchDeliveryId = headers.get("despatchDeliveryId")
def postingDate = headers.get("postingDate")
def externalUniqueReference = headers.get("externalUniqueReference")
// Setting item-related headers for the first item (assuming single-item processing)
//def firstItem = headers.get("items").item[0] // Accessing the first item
def customerPurchaseOrderNumber = headers.get("customerPurchaseOrderNumber")
def ssccNumber = headers.get("ssccNumber")
def materialReference = headers.get("materialReference")
def batch = headers.get("batch")
// Global location numbers for source and destination
def sourceGlobalLocationNumber = headers.get("sourceGlobalLocationNumber")
def destinationGlobalLocationNumber = headers.get("destinationGlobalLocationNumber")
def Scope = headers.get("Scope");
def Direction = headers.get("Direction");
def TerminalState = headers.get("TerminalState");
def Destination = headers.get("Destination");
def ProcessingState = headers.get("ProcessingState");
def PartnerID = map.get("FieldProperty").split("\\|")[2];
def Country = map.get("FieldProperty").split("\\|")[0];
message.setProperty("PartnerID", PartnerID);
message.setProperty("Country", Country);
// Create a combined header string
def combinedHeader = ""
// Add destinationType to combined header if not null
if (Scope != null && Scope != "") {
combinedHeader += "\"additionalProperty.Scope\": \"" + Scope + "\", ";
} else {
combinedHeader += "\"additionalProperty.Scope\": \"\", "; // Add empty string if null or empty
}
if (Direction != null && Direction != "") {
combinedHeader += "\"additionalProperty.Direction\": \"" + Direction + "\", ";
} else {
combinedHeader += "\"additionalProperty.Direction\": \"\", "; // Add empty string if null or empty
}
if (TerminalState != null && TerminalState != "") {
combinedHeader += "\"additionalProperty.TerminalState\": \"" + TerminalState + "\", ";
} else {
combinedHeader += "\"additionalProperty.TerminalState\": \"\", "; // Add empty string if null or empty
}
if (Destination != null && Destination != "") {
combinedHeader += "\"additionalProperty.Destination\": \"" + Destination + "\", ";
} else {
combinedHeader += "\"additionalProperty.Destination\": \"\", "; // Add empty string if null or empty
}
if (ProcessingState != null && ProcessingState != "") {
combinedHeader += "\"additionalProperty.ProcessingState\": \"" + ProcessingState + "\", ";
} else {
combinedHeader += "\"additionalProperty.ProcessingState\": \"\", "; // Add empty string if null or empty
}
if (sourceType != null && sourceType != "") {
combinedHeader += "\"additionalProperty.sourceType\": \"" + sourceType + "\", ";
} else {
combinedHeader += "\"additionalProperty.sourceType\": \"\", "; // Add empty string if null or empty
}
if (destinationType != null && destinationType != "") {
combinedHeader += "\"additionalProperty.destinationType\": \"" + destinationType + "\", ";
} else {
combinedHeader += "\"additionalProperty.destinationType\": \"\", "; // Add empty string if null or empty
}
if (sourceLocation != null && sourceLocation != "") {
combinedHeader += "\"additionalProperty.sourceLocation\": \"" + sourceLocation + "\", ";
} else {
combinedHeader += "\"additionalProperty.sourceLocation\": \"\", "; // Add empty string if null or empty
}
if (destinationLocation != null && destinationLocation != "") {
combinedHeader += "\"additionalProperty.destinationLocation\": \"" + destinationLocation + "\", ";
} else {
combinedHeader += "\"additionalProperty.destinationLocation\": \"\", "; // Add empty string if null or empty
}
if (despatchDeliveryId != null && despatchDeliveryId != "") {
combinedHeader += "\"additionalProperty.despatchDeliveryId\": \"" + despatchDeliveryId + "\", ";
} else {
combinedHeader += "\"additionalProperty.despatchDeliveryId\": \"\", "; // Add empty string if null or empty
}
if (postingDate != null && postingDate != "") {
combinedHeader += "\"additionalProperty.postingDate\": \"" + postingDate + "\", ";
} else {
combinedHeader += "\"additionalProperty.postingDate\": \"\", "; // Add empty string if null or empty
}
if (externalUniqueReference != null && externalUniqueReference != "") {
combinedHeader += "\"additionalProperty.externalUniqueReference\": \"" + externalUniqueReference + "\", ";
} else {
combinedHeader += "\"additionalProperty.externalUniqueReference\": \"\", "; // Add empty string if null or empty
}
/*
addHeaderProperty("customerPurchaseOrderNumber", customerPurchaseOrderNumber);
addHeaderProperty("ssccNumber", ssccNumber);
addHeaderProperty("materialReference", materialReference);
addHeaderProperty("sourceGlobalLocationNumber", sourceGlobalLocationNumber);
addHeaderProperty("destinationGlobalLocationNumber", destinationGlobalLocationNumber);
addHeaderProperty("PartnerID", PartnerID);
addHeaderProperty("Country", Country);*/
// Add externalUniqueReference
if (externalUniqueReference != null && !externalUniqueReference.isEmpty()) {
combinedHeader += "\"additionalProperty.externalUniqueReference\": \"" + externalUniqueReference + "\", ";
} else {
combinedHeader += "\"additionalProperty.externalUniqueReference\": \"\", "; // Add empty string if null or empty
}
// Add customerPurchaseOrderNumber
if (customerPurchaseOrderNumber != null && !customerPurchaseOrderNumber.isEmpty()) {
combinedHeader += "\"additionalProperty.customerPurchaseOrderNumber\": \"" + customerPurchaseOrderNumber + "\", ";
} else {
combinedHeader += "\"additionalProperty.customerPurchaseOrderNumber\": \"\", "; // Add empty string if null or empty
}
// Add ssccNumber
if (ssccNumber != null && !ssccNumber.isEmpty()) {
combinedHeader += "\"additionalProperty.ssccNumber\": \"" + ssccNumber + "\", ";
} else {
combinedHeader += "\"additionalProperty.ssccNumber\": \"\", "; // Add empty string if null or empty
}
// Add materialReference
if (materialReference != null && !materialReference.isEmpty()) {
combinedHeader += "\"additionalProperty.materialReference\": \"" + materialReference + "\", ";
} else {
combinedHeader += "\"additionalProperty.materialReference\": \"\", "; // Add empty string if null or empty
}
// Add sourceGlobalLocationNumber
if (sourceGlobalLocationNumber != null && !sourceGlobalLocationNumber.isEmpty()) {
combinedHeader += "\"additionalProperty.sourceGlobalLocationNumber\": \"" + sourceGlobalLocationNumber + "\", ";
} else {
combinedHeader += "\"additionalProperty.sourceGlobalLocationNumber\": \"\", "; // Add empty string if null or empty
}
// Add destinationGlobalLocationNumber
if (destinationGlobalLocationNumber != null && !destinationGlobalLocationNumber.isEmpty()) {
combinedHeader += "\"additionalProperty.destinationGlobalLocationNumber\": \"" + destinationGlobalLocationNumber + "\", ";
} else {
combinedHeader += "\"additionalProperty.destinationGlobalLocationNumber\": \"\", "; // Add empty string if null or empty
}
// Add PartnerID
if (PartnerID != null && !PartnerID.isEmpty()) {
combinedHeader += "\"additionalProperty.PartnerID\": \"" + PartnerID + "\", ";
} else {
combinedHeader += "\"additionalProperty.PartnerID\": \"\", "; // Add empty string if null or empty
}
// Add Country
if (Country != null && !Country.isEmpty()) {
combinedHeader += "\"additionalProperty.Country\": \"" + Country + "\", ";
} else {
combinedHeader += "\"additionalProperty.Country\": \"\", "; // Add empty string if null or empty
}
// Add ssccNumber to combined header, explicitly put value in quotes if it exists
// Trim any trailing comma and space
combinedHeader = combinedHeader.trim();
if (combinedHeader.endsWith(",")) {
combinedHeader = combinedHeader[0..-1]; // Remove trailing comma
}
// If combinedHeader is empty, set the default value to the new header
if (combinedHeader.isEmpty()) {
message.setHeader("combinedProperties", "");
} else {
message.setHeader("combinedProperties", combinedHeader);
}
return message;
}