1

I am relatively new to building Apex Triggers and need some help.

Trigger's Purpose: The Account Triggers purpose is to throw an error message if there is no contact associated to the Account upon an edit to the Account Record.

Problem I am getting an error message that says "unexpected token: for" on the line that has the for loop for Contact. What is wrong here? Thank you everyone ahead of time.

Below is my Trigger

trigger Account_Contact_Required on Account (before update, after update) {
Set<Id> allAccountIds = new Set<Id>();
Set<Id> AccountwithContactsIds = new Set<Id>();

 // fetch all the Account Id's in SFDC and throw in list
 for(Account acc : trigger.new)
 {
            allAccountIds.add(acc.Id);
      }
 }

 // fetch Account Id's that have a Contact associated and throw in list
 for(Contact co : [Select AccountId From Contact Where AccountId IN : allAccountIds ])
 {
      AccountwithContactsIds.add(co.AccountId);
 }

 // compare the two sets (one has all Account Id's and other has just Account Id's with at least one Contact)
 // Left out Accounts should not be allowed to be edited until a Contact is associated with the Account, so we throw an error.
 for(Account a : trigger.new)
 {
       if(allAccountIds.contains(a.Id) && !AccountwithContactsIds.contains(a.Id))
            a.addError('At least one contact is required on the Account.');
 }
 }
0

1 Answer 1

1

You have an extra "}" on line 9 that leaves the compiler thinking that its at the end of the trigger and that there should be no more text - hence the "for" is unexpected.

Delete that line.

1
  • @Nate No just the sort of error that we all make and just can't spot ourselves.
    – Keith C
    Commented Nov 17, 2014 at 17:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .