+ 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 : trigger.new)
    string accountOwner = accs.OwnerId;

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

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

I am getting the following errors:
User-added image

Any ideas?

Thanks!
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...
 
jigarshahjigarshah
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.isAfter){
	
		if(Trigger.isInsert || Trigger.isUpdate){
		
			Map<Id, Id> ownerIdAccountIdMap = new Map<Id, Id>();
			
			for(Account acc :Trigger.new){
			
				//Capture only protected accounts
				if(accs.Protected_Accounts__c == true){
					accountOwnerIdCountMap.put(acc.OwnerId, acc.Id);
				}
			}//for
			
			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 && 
					   accountOwnerIdCountMap.containsKey(accOwnershipAggResult.get('OwnerId'))){
					   
						errorAccountIdSet.add(accountOwnerIdCountMap.get(accOwnershipAggResult.get('OwnerId')));
					}
				}
			}//if
			
			//Flag Account records where ownership exceeds 3
			if(errorAccountIdSet.size() > 0){
			
				for(Id accountId :errorAccountIdSet){
				
					if(Trigger.newMap.containsKey(accountId)){
					
						Trigger.newMap.get(accountId).addError('You are at your limit of Account ownership.');
					}
				}
			}//if
		}
	}
}

Moreover, I would recommend taking the Trailhead module on Apex Triggers (https://trailhead.salesforce.com/en/modules/apex_triggers/units/apex_triggers_intro) 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.