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

 

 

 

View Entire Topic
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.