cancel
Showing results for 
Search instead for 
Did you mean: 

Output array in UDF

Former Member
0 Kudos

Experts;

below is code: i want to return back array;

it doesnt throw any error but it gives output as [Ljava.lang.String;@4a4cd40b..


public void collectSICCODE(String[] cod, ResultList result, Container container) throws StreamTransformationException{

       String[] myArray = new String[cod.length];

        for( int i=0; i<cod.length; i++)
       {
            myArray<i> = cod<i>; // assign the value to index i, replace ""+i with your value.
        }
   result.addValue(myArray);

}

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

You have to add value after value to the resultset & not an arraylist.

As you can see from result.AddValue i/p param is String & not an array.

http://help.sap.com/saphelp_nw04/helpdata/en/b1/83a09f668320419dbe00a741e0fe6a/content.htm

Move result.AddValue() into the loop & provide the value.

-SM

Former Member
0 Kudos

Siva,

actually i have to pass the whole array to arrayType of webservice..

my scenarios is IDOC>PI>Webservice

I have imported WSDL for the target message, which has the type as arrayType for an element.

i read some of the threads, they say arrayType is not supported..

Answers (2)

Answers (2)

Former Member
0 Kudos

Try the below UDF and see if it works


public void collectSICCODE(String[] cod, ResultList result, Container container) throws StreamTransformationException{
 
       String myArray ;
 
        for( int i=0; i<cod.length; i++)
       {
            myArray = myArray + cod<i>; // assign the value to index i, replace ""+i with your value.
        }
   result.addValue(myArray);
 
}

Thanks

Aamir

Former Member
0 Kudos

Aamir,

it throws me error

at +

myArray = myArray + cod<i>;

Function collectSICCODE, Line 10:

incompatible types found : java.lang.String required: java.lang.String[] myArray = myArray + cod<i>; // assign the value to index i, replace ""+i with your value. ^ Note: /usr/sap/XID/DVEBMGS00/j2ee/cluster/server0/./temp/classpath_resolver/Map4588815056ca11debc3200212814d176/source/com/sap/xi/tf/_CreateCustomerMaster_to_CreateCustomerMaster_.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. 1 error

Former Member
0 Kudos

could you please replace

>>String myArray ;

with

>>String myArray = " ";

Since its an attribute,you will be able to assign only one value to it,so you need to concatenate all values of the sender node and then pass it on to the receiver side.

The UDF given by Amit looks good to me as well.

Thanks

Aamir

Former Member
0 Kudos

there was some syntax error.. that got resolved..

the solution you provided is also provided by Amit, also i have tried that before..

but here it is returning one string as result, it is not an array right?

how do i assisgn this to an array? the whole result is going to be first one value of an array..

Former Member
0 Kudos

See the problem is since you have an attribute on the receiver side,you can not have an array there.it will be a single string

the other options can be to change the occurrence of SIC_Codes to 0-unbounded or make p2:ArrayType as an Element instead of an attribute.

Thanks

Aamir

Former Member
0 Kudos

Please try the following code


public void collectSICCODE(String[] cod, ResultList result, Container container) throws StreamTransformationException{
 
       String[] myArray = new String[cod.length];
 
        for( int i=0; i<cod.length; i++)
       {
            myArray<i> = cod<i>; // assign the value to index i, replace ""+i with your value.
           result.addValue(myArray<i>);   
     }
   
 
}

You can not return an array from a UDF.UDF's only returns output in the form of a string(single value or whole queue).

Thanks

Aamir

Former Member
0 Kudos

Aamir,

if UDF return single values, then how to send an array to Webservice which has the type arrayType ?

Former Member
0 Kudos

>>if UDF return single values

It does not returns only single value

It return String values.this can be a single value or it can be multiple values( kind of like an array)

the statement result.addValue in the UDF will create a String array like structure which will keep on appending values to itself after every iteration.once the end of UDF is reached,the variable "result" will contain a list of all the values,you can simply map the output of UDF to the node of receiver side which is expecting arrayType value.

Thanks

Aamir

Former Member
0 Kudos

Aamir,

thanks for the clarifications..

I tried with same solution but the value assigned to the array is single value, which is first value from the list..

ex: if the UDF returns

23

24

25

then the arrayType has the value 23 , but it should have been all..

Former Member
0 Kudos

what is the occurence of the node which is of type arrayType?

also what is the occurce of its parent node?

Thanks

Aamir

Former Member
0 Kudos

While sending Webservice request it dosen't carry info whether the data is string or float or int or arraylist... These types are used only during programming & defining the structure. (I don't think you can define an arraylist as your element type.)

So you should really not worry about arraylist. Add the values in UDF to resultlist & test your map. Check in the test tab if the output XML is as per what you wanted....

-SM

Former Member
0 Kudos

Hi,

The code looks correct. It should populate the array with all the values. It looks like in input string array (cod) has only one value and so one value is getting pouplated in result. Just for debugging purpose, can you try the code given below and see what is the value you can see in ResultList


public void collectSICCODE(String[] cod, ResultList result, Container container) throws StreamTransformationException{
 
       String[] myArray = new String[cod.length];
 
        for( int i=0; i<cod.length; i++)
       {
            myArray<i> = cod<i>; // assign the value to index i, replace ""+i with your value.
           
     }
    result.addValue(""+cod.length); 
 }

Amit

Former Member
0 Kudos

Aamir,

the occurence of this node is unbounded..

and parent node occurence is 1..

actually the elemnt shows an Icon of variable (solid circle with down point arrow on circle) and shows type as p6:arrayType this is imported WSDL as an External Defination.

Former Member
0 Kudos

 (I don't think you can define an arraylist as your element type.)

Siva,

i have not defined this, but it came out of WSDL as arraytype when imported..

i did so but the result is one value..

Former Member
0 Kudos

Amit,


result.addValue(""+cod.length); 

gives me the value 3

Former Member
0 Kudos

>>solid circle with down point arrow on circle

This is an attribute,not a node element.

Please look at the below link and see if you are mapping it to an attribute or an actual node

http://help.sap.com/saphelp_nw04s/helpdata/en/8a/7bc84841f14b47aa769c9219452229/frameset.htm

Thanks

Aamir

Former Member
0 Kudos

strange! can you try the code given below and tell me the output. I just want to narrow down the issue.


public void collectSICCODE(String[] cod, ResultList result, Container container) throws StreamTransformationException{
 
       String[] myArray = new String[cod.length];
	 String tempStr ="";
        for( int i=0; i<cod.length; i++)
       {
            tempStr = tempStr + cod<i>+ ",";
     }
    result.addValue(tempStr); 
 }

Former Member
0 Kudos

yes amit, that is correct its an attribute..

This is the part of WSDL:


         <complexType name="ArrayOfT_SIC_Codes">
            <complexContent>
               <restriction base="soapenc:Array">
                  <attribute ref="soapenc:arrayType" wsdl:arrayType="impl:T_SIC_Codes[]" />
               </restriction>
            </complexContent>
         </complexType>

Former Member
0 Kudos

aha,that's the issue

you can not have multiple occurrences of an attribute within the same data element.

Attributes are either optional or Required,but they cant occur multiple times within the same payload.

One thing you can do is concatenate all values from the UDF and send then send them to the receiver system.but you can't send them separately as long as its defined as an attribute.

Thanks

Aamir

Former Member
0 Kudos

oops i was editing the message..

Hierarchy of the nodes is


- customers - occurence 1..1 (node)
  -  SIC_Codes - occurence 0..unbounded (node)
     -  p2:arrayType -  is an attribute
 

Former Member
0 Kudos

amit,

i have lready tried this and gives me 23,24,25, as result.

Former Member
0 Kudos

Ok,here is what you need to do.

you need to create multiple occurences of SIC_Codes and within each occurence you need to populate one arrayType.

Thanks

Aamir

Former Member
0 Kudos

Aamir,

sorry for the confusion

Source


- customers - occurence 1..1 (node)
  -  SIC_Codes - occurence 0..unbounded (node)
     -  Customers_SIC_code - occurence 0..1 (string)  

to get

target


- customers - occurence 1   (node)
  -  SIC_Codes - occurence 1  (node)
     -  p2:arrayType -  is an attribute

i want all Customers_SIC_code to be sent as an array in p2:arrayType that is why the occurence of the SIC_Codes is 1 at the target and unbounded at the source..