cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

CPI Message Mapping Based on Attribute Value

jonaswang
Product and Topic Expert
Product and Topic Expert
0 Likes
3,608

Hi

How can map a data field based on its attribute? For example, I would to like to map a custom-attribute (10083 in this case) with attribute-id="color" to a target data field.

Accepted Solutions (0)

Answers (1)

Answers (1)

MortenWittrock
SAP Mentor
SAP Mentor

Hi Jonas

You can do this with a short function:

import com.sap.it.api.mapping.*

def void filterAttributes(String[] attributes, String[] values, String[] attributeToLocate, Output output, MappingContext context) {
    def searchFor = attributeToLocate[0]
    def foundIndex = Arrays.asList(attributes).indexOf(searchFor)
    def value = foundIndex == -1 ? "default value" : values[foundIndex]
    output.addValue(value)
}

Wire it up like this:

Set the context of @attribute-id and custom-attribute to be the parent element (custom-attributes). The script assumes that the first two queues do not contain context changes.

The last parameter is the value of the attribute you are looking for.

Note that the script inserts a default value, if the attribute value could not be found. You can change this behaviour as needed.

Regards,

Morten

jonaswang
Product and Topic Expert
Product and Topic Expert
0 Likes

Hi Morten,

Updates: I modified the cardinality to 0..*. it's still giving me the same error.

Thanks for the script. I have one more question. What if I want to map short-description field based on lang what kind of change do I need to make? I have tried your script with the mapping, values of short-description are not passed to the custom function correctly.

MortenWittrock
SAP Mentor
SAP Mentor
0 Likes

Hi jonas.wang

That wouldn't work. The cardinality of the short-description element is 0..1, so there's no list of descriptions to filter, based on the language attribute.

Regards,

Morten