function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andrew Dahlkemper 12Andrew Dahlkemper 12 

Invalid loop variable type expected Account was Account

I am trying to create a Trigger to limit the number of Account a User can own.
Here is my code:
trigger Accounts75Limit on Account (after insert, after update) {

    //List<Account> acc = new List<Account>();
	for(Account acc :
    string accountOwner = accs.OwnerId;

    if (accs.Protected_Accounts__c == true) {
        Integer accounts = [ SELECT COUNT()FROM Account WHERE Account.OwnerId = : accountOwner]; 

        if (accounts >3 ) {
            accs.addError('You are your limit of Accounts.');

I am getting the following errors:
User-added image

Any ideas?

Marcelo CostaMarcelo Costa

Several issues here :)
On line 5 you are trying to access the accs variable, but the variable created on line 4 is actually acc...

Line 5 would be:
string accountOwner = acc.OwnerId;
On line 8 you have a SOQL inside a loop. That will eventually give you a Limit exception.

lastly, according to best practices, no logic should be inside a trigger but Context identification... you should probably use a TriggerHandler to specify the logic...

Good Luck!!  

Andrew Dahlkemper 12Andrew Dahlkemper 12
Ah thanks Marcelo, I am clearly inexperienced!

Any idea on the issue with line 4? It's just weird that is expecting Account and also receiving Account ...
Marcelo CostaMarcelo Costa
No worries Andrew!
We all began inexperienced :)
Did you fix all the references to accs to acc(Line 5 and Line 7?) the error for line 4 persists??
You can try to cast the variable, but in theory there should be no need...
The trigger code below will help you accomplish your purpose. You will also need to write the test code for this.
trigger Accounts75Limit on Account (after insert, after update) {

		if(Trigger.isInsert || Trigger.isUpdate){
			Map<Id, Id> ownerIdAccountIdMap = new Map<Id, Id>();
			for(Account acc{
				//Capture only protected accounts
				if(accs.Protected_Accounts__c == true){
					accountOwnerIdCountMap.put(acc.OwnerId, acc.Id);
			Set<Id> errorAccountIdSet = new Set<Id>();
			if(accountOwnerIdCountMap.keySet().size() > 0){
				for(AggregateResult accOwnershipAggResult :[SELECT OwnerId, COUNT(Name) 
															FROM Account 
															WHERE OwnerId IN :accountOwnerIdCountMap.keySet() 
															GROUP BY OwnerId]){

					//Capture Account Ids where ownership count exceeds 3
					if(accOwnershipAggResult.get('expr0') > 3 && 
			//Flag Account records where ownership exceeds 3
			if(errorAccountIdSet.size() > 0){
				for(Id accountId :errorAccountIdSet){
						Trigger.newMap.get(accountId).addError('You are at your limit of Account ownership.');

Moreover, I would recommend taking the Trailhead module on Apex Triggers ( to get yourself acquainted with writing Apex Triggers.

Please mark the thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.