cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CPI - UDF Mapping Context Issue

PramilaR
Explorer
0 Kudos

Hello Experts of CPI,

I have a requirement of mapping and below is the requirement which i am unable to get the output, please review and provide the best CPI UDF solution

I used create copies udf but it is not creating as shown in target value needed

1 source context values should repeat again and again by given count value

Source single context values     context number of copies             Target Value Needed

101.22                                                   3                                             101.22

202.22                                                   2                                            202.22

303.22                                                   1                                            303.22

                                                                                                             ------------

                                                                                                             101.22

                                                                                                             202.22

                                                                                                             ------------

                                                                                                              101.22

 

CPI Context Mapping.png

 

 

 

Accepted Solutions (1)

Accepted Solutions (1)

Dinu
Contributor

You have to convert the second parameter, so that it has the same number of context changes as the first one. Then the UDF will be called with both parameters as arrays. Each call will get matching contexts for each parameter one set at a time. Then you can loop on the second parameter and make copies of the first parameter.  You won't get a cross product of the arguments. 

You can change the second parameter to a list either by changing the context, by going one level up or using the function removeContext. 

Here is for illustration a function that copies a value several times as specified by the second parameter. Your requirement is different. 

def void copyManyTimes(String[] value, String[] count, Output output, MappingContext context) {
    
    count.eachWithIndex { it,i ->
        (it as Integer).times { output.addValue( value[i] ) }
        output.addContextChange()
    }
    
}​

 

preddy_19059381
Newcomer
0 Kudos
Hi Dinu, Thank you for the reply when i tested it is not giving me the expected results
Dinu
Contributor
0 Kudos
The code snippet does not do what you want. It copies values from first param as many times as is mentioned in the second. It is an illustration of how you could do what you want to do.
PramilaR
Explorer
0 Kudos
Hi Dinu, I have removed the context change in second queue but i get diffeernt values as shown is screen shot, but the output sequence should be as per earlier requested
PramilaR
Explorer
0 Kudos
Thank you. I already had this solution, inner loop was not working for me, appricate if you share it
Dinu
Contributor
0 Kudos
def void copyManyElements(String[] elements, String[] counts, Output output, MappingContext context) { 
  counts.each { it -> 
   def n = it as Integer; 
   if( n > 0 ) (elements[0..(n-1)]).each { e -> output.addValue( e ) } 
   output.addContextChange() 
  } 
}

This might be what does what you want.

Answers (2)

Answers (2)

PramilaR
Explorer
0 Kudos

Hi Dinu,

 

When i remove the second context change the values are not as expected

 

CPI Context Mapping2.png

Dinu
Contributor
0 Kudos
This is the expected results for the sample function I had shared. You have to change the inner loop to do what you want to do.
PramilaR
Explorer
0 Kudos

Hi Dinu,

Thank you for the reply,

When i tested with your code I am getting a different result, please see the screen shot

First context should repeat it self based on second input count number

if count value in second context is 1 then repeat 1 time

if count value in second context is 5 then repeat 5 times

if count value in second context is 3 then repeat 3 times

CPI Context Mapping1.png

Thanks

Dinu
Contributor
0 Kudos
If you have context changes in the second argument, you will not get what you want. All the counts (1,2,3) should be in one context.