cancel
Showing results for 
Search instead for 
Did you mean: 

Substring is not working in message mapping

vijender_p
Active Participant
0 Kudos

Hi All
while am doing the mapping am not able to get the exact value from substring and also when it is blank it is failing . can you let me know what is the wrong am doing here?

"2019-04-24 12:52:30" -- substring - starts with 0 to 10 for field map with A and substring -starts with 12 to 19 for field map with B.

Regards,

Vij



MortenWittrock
Active Contributor
0 Kudos

Hi Vijender

Please show a screenshot of the relevant parts of your mapping, and show your input and expected output.

Regards,

Morten

vijender_p
Active Participant
0 Kudos

HI Morten,
Please find the screen shot of mapping below and in which source and target fields are determine.
image1.jpg

and from the source this the data which is coming source system.
image2.jpg

For which target system value should be divided into 2 values.

CLCIN_DATE --2019-04-24
CLCIN_TIME -- 12:52:30

So I have used substring for this doing this for the first value it is substring(0 to 10) and then next value(12 to 19). But it is failing due to String index out of range.

Regards,
Vij

Accepted Solutions (1)

Accepted Solutions (1)

MortenWittrock
Active Contributor

Hi Vijender

The substring function needs two parameters: The start index and the character count. You're configuring it as if the second parameter was the end index, which is not the case. Also, the start index is zero-based, so for the time, the start index is 11.

Configure the first substring with 0 and 10, and the second with 11 and 8. Then map like this:

That gives me this result:

Regards,

Morten

vijender_p
Active Participant
0 Kudos

Hi Morten,
Thanks for the reply...
When the data is blank from the source side the logic is still failing.Can you let me know why?

Regards,
Vij

MortenWittrock
Active Contributor
0 Kudos

Hi Vijender

It fails on an empty string, because you're asking it to return the 10 first characters of a string of length 0. That's why you're seeing the "index out of range" error.

Since your target elements are mandatory (1..1), it's more of a validation issue to make sure that the source CLCIN_DATE field is not empty. Alternatively, if there are meaningful default date and time values, you can map those if CLCIN_DATE is empty.

Regards,

Morten

MortenWittrock
Active Contributor
0 Kudos

The source field will always exist, if the input document is valid (the element is mandatory). Checking for the empty string, and replacing it with a default date like 1970-01-01 00:00:00 is probably easier to do in a simple user-defined function. But otherwise use the built-in length function to check the length of the string. If it's zero, replace with a constant, otherwise use the contents of the source field.

Regards,

Morten

MortenWittrock
Active Contributor
0 Kudos

Add a function like the following:

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

def String contentsOrDefault(String val) {
    val == "" ? "1970-01-01 00:00:00" : val
}

Then wire it up like this:

The function assumes that the source element is present, which it will be according to your schema.

I think it's reasonable by now to leave any remaining adjustments to your particular scenario to you. I have provided you with all the pieces you need.

Regards,

Morten

Answers (1)

Answers (1)

rakeshdamera
Participant
0 Kudos

Hi Vijay,

The second argument on the substring function is the character count and not the end position.

Please ensure you are using (12,8) and not (12,19) in the mapping.

Hope that helps.

Rakesh