You need to sign in to do that
Don't have an account?

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:
I am getting the following errors:

Any ideas?
Thanks!
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:
Any ideas?
Thanks!
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!!
Any idea on the issue with line 4? It's just weird that is expecting Account and also receiving Account ...
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...
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.