cancel
Showing results for 
Search instead for 
Did you mean: 

Problems with summing segments

Former Member
0 Kudos

Hi experts,

I have an idoc as source, and I would like to sum the value of a specific segment under a condition:

my idoc is as following:

IDOC

>E1EDP01 0..unbounded

>>E1EDP04 0..unbounded

>>>MSWK

>>>MSWB

for each segment of E1EDP04, if the value of MSWK is the same, then I have to map the sum of all MSWB for the same value of MSWK.

Do you know how to do that? do I have to do an UDF? if yes, how should I start since i'm new at xi and java?

for example, i can have:

IDOC

>E1EDP01

>>E1EDP04

>>>MSWK = code1

>>>MSWB = 5

>E1EDP01

>>E1EDP04

>>>MSWK = code1

>>>MSWB = 7.5

>E1EDP01

>>E1EDP04

>>>MSWK = code1

>>>MSWB = 5

>E1EDP01

>>E1EDP04

>>>MSWK = code2

>>>MSWB = 4

>E1EDP01

>>E1EDP04

>>>MSWK = code2

>>>MSWB = 4.5

then I have to map the sum of MSWB to 2 segments:

>tax

>>type = code1

>> value = 17.5

>tax

>>type = code2

>> value = 8.5

Thanks a lot,

best regards,

jamal

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

is this your intended result?

exaclty Iddo, thanks, how did you manage to sum the right way?

Thanks also suddhir, the problem is I always get 0 as result. In debug, it seems that it never goes into the condition

 if ((MWSKZ[j].equals(MWSKZ))){ 

kind regards,

jamal

Edited by: ba_da_boom on Sep 19, 2008 1:47 PM

Former Member
0 Kudos

Here's my solution:

step 1: create a UDF called getUniqueValues (queue function)

public void getUniqueValues(String[] MSWK,ResultList result,Container container){
import java.lang.*;

HashSet uniqueSet = new HashSet();

for (int i = 0 ; i < MSWK.length; i++ ) {
                if (uniqueSet.add(MSWK<i>))
	result.addValue(MSWK<i>);
}
}

This function returns the unique codes. You'll need this to determine how many tax segments you need and to sum up the values.

step 2: create a UDF called addValues (queue function)

public void addValues(String[] uniqueCodes,String[] allCodes,String[] addVals,ResultList result,Container container){

double sum = 0;

for (int i = 0; i < uniqueCodes.length; i++) {
	for (int j = 0; j < allCodes.length; j++) {
		if (uniqueCodes<i>.equals(allCodes[j])) {
			sum = sum + Double.parseDouble(addVals[j]);
		}
	}
	result.addValue(""+sum);
	sum = 0;
}
}

This function adds the values for each unique code as and returns them in a single context.

Step 3: the mapping of the tax segment: [http://www.xs4all.nl/~iddor/tax.jpg]

Set the context of MSWK to the parent segment of EDP01, in this case that is MT_SDN

Step 4: map field type like this: [http://www.xs4all.nl/~iddor/type.jpg]

Step 5: map the value field: [http://www.xs4all.nl/~iddor/value.jpg]

Set the contexts to the parent segment of EDP01, in this case that is MT_SDN. Add a context change between each value and you're done.

Answers (4)

Answers (4)

SudhirT
Active Contributor
0 Kudos

Hi,

That condition will never execute, sorry for that, I just gave you the idea you can modify that code like

Modify your code like below and try again

int sum = 0;

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

{

if( !(i.equals(MWSKZ.length-1) {

for (int j = i1; j < MWSKZ.length; j+){

if ((MWSKZ<i>.equals(MWSKZ[j]))){

int i_MWSBT = Integer.parseInt(MWSBT[j]));

sum = sum + i_MWSBT ;

}

}

}

}

result.addValue(""+sum);

Note that input fields to this UDF should be MSWK and MSWB. Thanks!

Try this this should solve ur mapping.

Former Member
0 Kudos

oh my god Iddo, THANKS SO SO MUCH. I've been struggling for 2 days! thanks a million!

Actually i didnt used your first function (i have an error with the import javalang*). I manageed anyway to get all the unique values, and it works perfecly fine.

THANKS, thanks and thanks

SudhirT
Active Contributor
0 Kudos

Hi,,

Modify your code like below and try again

int sum = 0;

{

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

{

for (int j = 0; j < MWSBT.length; j++){

if ((MWSKZ[j].equals(MWSKZ<i>))){

int i_MWSBT = Integer.parseInt(MWSBT[j]));

sum = sum + i_MWSBT ;

}

}

}

result.addValue(""+sum);

}

Set your context accordingly by right clicking on the segment.

I have not tested this code just an idea how you can implement it, correct if there are any syntax errors.

Thanks!

Former Member
0 Kudos

I tried java mapping:

try
		{
			for (int i = 0; i < MWSKZ.length; i++) 
			{
				for (int j = 0; j < MWSBT.length; j++){
					if ((MWSKZ[j].equals(MWSKZ<i>))){
						result.addValue(MWSBT[j]);
					}
				}
				result.addContextChange();
			}
	}

I manage to regroup the value but not to sum them, plus I get to many contexts:

[7, 7.5, 5, __cC_, 7, 7.5, 5, __cC_, 7, 7.5, 5, __cC_, 4, 4.5, __cC_, 4, 4.5, __cC_]

any ideas?

Former Member
0 Kudos

Hi ba_da_boom,

is [this|http://www.xs4all.nl/~iddor/intended_result.jpg] your intended result? I'll take some time entering the solution here's that's why I ask you first.