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
iSqFt_ADiSqFt_AD 

What mistake am I making in editing this trigger?

Disclaimer - I have no development experiement. I have been using the canned trigger that is floating around the internet that reassigns Contacts and Opportunities to the new owner when an Account Owner is changed. I have been getting feedback from representatives in my organization that want the transfers to be for the Contacts only. I attempted to pull out the pieces of the code that involve the Opportunity but I get the following error:

 

Error: Compile Error: unexpected token: { at line 17 column 75

 

(Note I have put asteriks around the bracket the error is referring to for reference:

trigger reassignRelatedContacts on Account (after update) {
   try {
      Set<Id> accountIds = new Set<Id>(); //set for holding the Ids of all Accounts that have been assigned to new Owners
      Map<Id, String> oldOwnerIds = new Map<Id, String>(); //map for holding the old account ownerId
      Map<Id, String> newOwnerIds = new Map<Id, String>(); //map for holding the new account ownerId
      Contact[] contactUpdates = new Contact[0]; //Contact sObject to hold OwnerId updates
      
      for (Account a : Trigger.new) { //for all records
         if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId) {
            oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId); //put the old OwnerId value in a map
            newOwnerIds.put(a.Id, a.OwnerId); //put the new OwnerId value in a map
            accountIds.add(a.Id); //add the Account Id to the set
         }
      }
      
      if (!accountIds.isEmpty()) { //if the accountIds Set is not empty
         for (Account act : [SELECT Id, (SELECT Id, OwnerId FROM Contacts) **{** //SOQL to get Contacts for updated Accounts
            String newOwnerId = newOwnerIds.get(act.Id); //get the new OwnerId value for the account
            String oldOwnerId = oldOwnerIds.get(act.Id); //get the old OwnerId value for the account
            for (Contact c : act.Contacts) { //for all contacts
               if (c.OwnerId == oldOwnerId) { //if the contact is assigned to the old account Owner
                  Contact updatedContact = new Contact(Id = c.Id, OwnerId = newOwnerId); //create a new Contact sObject
                  contactUpdates.add(updatedContact); //add the contact to our List of updates
               }
          }
         update contactUpdates; //update the Contacts
      }
   } catch(Exception e) { //catch errors
      System.Debug('reassignRelatedContacts failure: '+e.getMessage()); //write error to the debug log
   }
}

 

The bracket in question has an end bracket, which is what is confusing. Where am I making a mistake?

Best Answer chosen by Admin (Salesforce Developers) 
Orn IngvarOrn Ingvar
for (Account act : [SELECT Id, (SELECT Id, OwnerId FROM Contacts) **{** 
Should be :
for (Account act : [SELECT Id, (SELECT Id, OwnerId FROM Contacts) FROM Account] ) {

All Answers

Orn IngvarOrn Ingvar
for (Account act : [SELECT Id, (SELECT Id, OwnerId FROM Contacts) **{** 
Should be :
for (Account act : [SELECT Id, (SELECT Id, OwnerId FROM Contacts) FROM Account] ) {
This was selected as the best answer
iSqFt_ADiSqFt_AD

Thank you very much for your help.