Skip to Content
avatar image
Former Member

UDF

Hi,

I need one udf to add quantities of material.

Input is:

BELNR QUANTITY

1001 100

1004 300

1001 50

1002 40

1001 50

1002 100

1007 500

Output should be:

1001 200

1004 300

1002 140

1007 500

Required UDF to get the above result.

Regards,

Rams.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    May 12, 2010 at 07:37 AM

    I have done this for another purpose. You need two UDFs, one for the BELNR, one for the QUANTITY.

    In my samples the parameters are named different, but I hope the principle is clear. You can just change the parameter names to your needs.

    first: input parameter = taxCode

    Vector taxCodeVector = new Vector();
    for (int i = 0; i < taxCode.length; i++ ){
       if (!taxCodeVector.contains(taxCode<i>)){
          taxCodeVector.addElement(taxCode<i>);
          result.addValue(taxCode<i>);
       }  
    }

    second: input parameter = taxCode, amount

    Vector taxCodeVector = new Vector();
    Vector amountVector = new Vector();
    // create vector from input array
    for (int i = 0; i < taxCode.length; i++ ){
      int vectorIndex = taxCodeVector.indexOf(taxCode<i>);
    // if tax code not in vector so far
       if (vectorIndex == -1){
    // add tax code and amount to vecors
          taxCodeVector.addElement(taxCode<i>);
          amountVector.addElement(new BigDecimal(amount<i>));
       }  else {
    // add the amount to the vector element corresponding to tax code
         BigDecimal currentAmount = (BigDecimal) amountVector.get(vectorIndex);
         amountVector.set(vectorIndex,currentAmount.add(new BigDecimal(amount<i>)));
       }
    }
    // output amount values
    for (int i = 0; i < amountVector.size(); i++ ){
      result.addValue( ((BigDecimal) amountVector.get(i)).toString());
    }

    the UDFs are of type "value of a context", but you have to set the context if the incoming parameters to the highest level, so there is only one context.

    Edited by: Stefan Grube on May 12, 2010 9:39 AM

    Add comment
    10|10000 characters needed characters exceeded