cancel
Showing results for 
Search instead for 
Did you mean: 

Udf for addition

Former Member
0 Kudos

Hi

I will get the values in a field where i need to,

1. add if the value has duplication(ex: 18 occurs twice - add it and get 36)

2. in addition to 36, find the maximum of values in the queues and pass to target..

Regards

Jansi    

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member182412
Active Contributor
0 Kudos

Hi Jansi,

Can you give us an example?

Regards,

Praveen.

Former Member
0 Kudos

Hi Praveen

my idoc is structure is,

E1EDL24

     Field1

     FIeld2

     ZZEMARAM

          Qty

          Stock

my requirement is,

Field2 and wty will come one time for each e1edl24,

but consider Field2 value and we need to count, and sum the corresponding qty..

Ex,

Field2 - 20

qty     - 50

Field2 - 40

Qty    - 60

Field2 - 30

Qty     - 40

Fied2 - 40

Qty    - 40

Here 40 in field2 comes many times when compare to other Field2 fields..

So based on Field2(40) i need to sum the qty like(60+40 = 100) and pass it to target.

Please help..

Regards

Jansi

former_member190293
Active Contributor
0 Kudos

Hi Jansirani!

Look at this thread with similar requirement:

http://scn.sap.com/message/16860717#16860717

Regards, Evgeniy.

anupam_ghosh2
Active Contributor
0 Kudos

Hi Jansi,

                what happens if Field2 comes with same value 3 or four times?

secondly what happens if same value for field2 is never repeated?

Regards

Anupam

former_member182412
Active Contributor
0 Kudos

Hi Jansirani,

Mapping:

UDF:

Execution Type : All Value Of Queue


public void sumByKey(String[] key, String[] value, ResultList keyResult, ResultList valueResult, Container container)

  throws StreamTransformationException {

  double qty = 0, valueDouble = 0;

  boolean isFirst = true;

  Map<String, Double> map = new HashMap<String, Double>();

  for (int i = 0; i < key.length; i++) {

  if (!key[i].equals(ResultList.CC)) {

  valueDouble = Double.parseDouble(value[i]);

  qty = (map.containsKey(key[i])) ? map.get(key[i]) + valueDouble : valueDouble;

  map.put(key[i], qty);

  }

  }

  for (Map.Entry<String, Double> mapEntry : map.entrySet()) {

  if (isFirst)

  isFirst = false;

  else {

  keyResult.addContextChange();

  valueResult.addContextChange();

  }

  keyResult.addValue(mapEntry.getKey());

  valueResult.addValue(mapEntry.getValue());

  }

  }

Test:

Regards,

Praveen.