public class HelperClass {
public static void insertAcc(List<Account> listaAccount){
AggregateResult[] CountAccount =[select count(id) contatore, ownerid from account group by ownerid order by count(id)];
list <account> acc_app = new list <account>();
for(Account acc: listaAccount)
{
integer contatore=0;
for(integer i=0;i<CountAccount.size();i++){
if(acc.OwnerId == CountAccount[i].get('owneriD'))
contatore = (integer)CountAccount[i].get('contatore');
}
if(contatore+listaAccount.size()>20)
{
integer differenza = 20-contatore;
for(integer i=0;i<differenza;i++)
{
acc_app.add(listaAccount.get(i));
}
if(differenza <=0)
acc.ownerid = (ID)CountAccount[0].get('ownerid'); //change owner
}
}
}
}