// Helper Class
public class Helper_trigger_acc {
public static void insertAcc(List<Account> listaAccount){
id owner = userinfo.getUserId();
AggregateResult[] CountAccount =[select count(id) contatore, ownerid from account where ownerid !=:owner group by ownerid order by count(id)];
list <account> acc_app = [select id, ownerid from account where ownerid=: owner];
for(Account acc: listaAccount)
{
if(acc_app.size()>=20)
acc.ownerid = (ID)CountAccount[0].get('ownerid'); //CHANGE OWNER
else
acc_app.add(acc);
}
}
}
//Classe di test
@istest
private class Classe_TestHelperTrigger {
@istest
static void m1()
{
String uniqueUserName = 'standarduser' + DateTime.now().getTime() + '@testorg.com';
list<Account> acc = new list<Account>();
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles',
UserName=uniqueUserName);
for(integer i=0;i<25;i++)
{
acc.add(new Account(name='Account '+i));
}
system.runAs(u){
account a1 = new Account(name ='Utente fittizio');
insert a1;
}
test.startTest();
insert acc;
Helper_trigger_acc.insertAcc(acc);
test.stopTest();
AggregateResult[] CountAccount = [select count(id) contatore, ownerid from account group by ownerid order by count(id)];
system.debug(CountAccount);
}
}