Returning a list from a java action

0
I am using a java action to replace some objects in a list. As parameters I have a list of accounts and some account objects that I use for input, as return type I have Type: List and Entity CoinRegister.Account. After replacing the Account objects in the list AccountList I return the list using : return AccountList;. Eclipse gives the error message: Type mismatch: cannot convert from List<Account> to List<IMendixObject>. I have been trying to convert the List to an IMendixObject, but no luck so far. Anyone help me out?   This is part of the code in the java action:     public UpdateBalance(IContext context, java.util.List<IMendixObject> AccountList, IMendixObject Debtor, IMendixObject Lender, IMendixObject Temp_Account, java.math.BigDecimal Amount)     {         super(context);         this.__AccountList = AccountList;         this.__Debtor = Debtor;         this.__Lender = Lender;         this.__Temp_Account = Temp_Account;         this.Amount = Amount;     }     @Override     public java.util.List<IMendixObject> executeAction() throws Exception     {         this.AccountList = new java.util.ArrayList<coinregister.proxies.Account>();         if (__AccountList != null)             for (IMendixObject __AccountListElement : __AccountList)                 this.AccountList.add(coinregister.proxies.Account.initialize(getContext(), __AccountListElement));         this.Debtor = __Debtor == null ? null : coinregister.proxies.Account.initialize(getContext(), __Debtor);         this.Lender = __Lender == null ? null : coinregister.proxies.Account.initialize(getContext(), __Lender);         this.Temp_Account = __Temp_Account == null ? null : coinregister.proxies.Account.initialize(getContext(), __Temp_Account);  //rest of my code where I replace the objects.                  return AccountList;    
asked
2 answers
3
Hi Martin,

try something like:

return AccountList.stream().map(e -> e.getMendixObject()).collect(Collectors.toList());

Hope this helps,
Andrej

 

answered
3

It's actually quite simple. Mendix always expects a list of List<IMendixObject> as a return value. 

This gives you two options (that I know of). 

1) Work with the input parameter list of IMendixObjects directly, and return this (probably better performance-wise, but you'd have to work with .getValue() / .setValue(). 

2) Work with the initialized list (as you are currently doing) and convert back to IMendixobjects for your return. This you'd do by creating a new list of IMendixObjects, iterating over your existing list, getting the IMendixObject for each, and returning your new list. 

It'd look something like:

List<IMendixObject> returnList = new ArrayList<IMendixObject>();

for (Account a : AccountList) {
returnList.add(a.getMendixObject());
}
return returnList;

(Or using streams, for performance)

answered