Problem parsing single record in CSV file

We are using WmEDI package to parse CSV files, convertToValues returns a recordList for the CSV records - this works fine when we have 2 or more records in the CSV file.

The problem occurs when we have one record in the CSV file, convertToValues returns a record instead of recordList with one record hence the LOOP fails -

Any suggestions how to resolve this ?

abhatia,

I encountered a similar problem in wm visually shows a recordList in the pipe but internally treats it as a record if there is only one record in the list. Therefore methods such as loop fails because there is no list to loop through.

The way I worked around it was to use a Java service to test if the record was in fact a list. If the service returned true the you can loop and use all the methods that work on lists. If the service returned false I treated is as just a record and not a list.

I don’t know if this a bug or it’s the way wm is intended to work. Seems like a bug to me.

Anyway here’s the Java code for the service.
input object obj
output string isList

IDataCursor idc = pipeline.getCursor();

idc.first(“obj”);

Object obj = idc.getValue();

String isLst = “false”;

if (obj != null && obj instanceof Object) {
isLst = “true”;
}

idc.last();
idc.insertAfter(“isList”, isLst);


HTH

Chris,

Thanks for the quick response.

For some reason this didn’t work for us -

Here is what we mapped:

CustomerInfo (Record Ref)
CustData (Record Ref List) —mapped—> obj (input to your service)

we would always get the response as “false”

any suggestions ?

Abhatia,

I’ve seen this problem before and you’ll experience the same problem when
using templates as well. I wrote my own java service to read a csv file
into a record list. I just pass in an initialised record and the full csv
file path. There’s a free CSVReader class out there you can use to develop
this service. Just use google to search for this class. Also this class can
handle embedded spaces and commas.

I have been seeing this posting go back and force I think I have a simple solution for you. WM has this well known problem turning any single element into a records instead of list. I created a service that ensures that no mater what record object passed the output will be a list or array even with single element so you Loop will work anyway. Here is a source:

public static final void guarantee_list (IData pipeline)
throws ServiceException
{
// — > —
// @sigtype java 3.5
// [i] record:1:required INPUT
// [o] record:1:required OUTPUT
try {
IDataHashCursor curs = pipeline.getHashCursor();

curs.first(“INPUT”);
Object obj = curs.getValue();

if (obj == null) {
curs.last();
curs.insertAfter(“OUTPUT”, obj);
}
else {
Class c = obj.getClass();

 if ( !c.isArray() ) { 
     // avoid class cast exceptions this way 
  Object output [] = (Object [])java.lang.reflect.Array.newInstance(c, 1); 
  output[0] = obj; 
  curs.last(); 
  curs.insertAfter("OUTPUT", output); 
 } else { 
  curs.last(); 
  curs.insertAfter("OUTPUT", obj); 
 } 

}

} catch (Exception e) {
throw new ServiceException(e.getMessage());
}
// — > —

}

Good luck