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
aferreira1.391718960195048E12aferreira1.391718960195048E12 

When Account Owner is changed, update owner of a custom object

Hello,
I am trying to write a trigger for Accounts so that when an Account Owner is changed, the owner of the custom object (in this case "Pricings" or Pricing__c) is changed. I was successfully able to do this for contacts and opportunities, but something about this code and the custom object are not meshing. I know it has to do with that SOQL query, but I'm at my wits end trying to fix it. Can someone please advise?
Thank you,

trigger reassignRelatedPricings on Account (after update) {
      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
      Pricing__c[] PricingUpdates = new Pricing__c[0]; //Pricing 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 acct : [Select id,Account__c, Account_Owner__c from Pricing__c where Account__c in:accIds]) { //SOQL to get Pricing 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 (Pricing__c p : act.Pricing__cs) { //for all pricings
               if (p.Account_owner__c == oldOwnerId) { //if the pricing is assigned to the old account Owner
                  Pricing__c updatedPricing = new Pricing__c(Id = p.Id, Account_owner__c = newOwnerId); //create a new Contact sObject
                  PricingUpdates.add(updatedPricing__c); //add the pricing to our List of updates
               }
            }
         }
         update PricingUpdates; //update the Pricings
   }
}
crop1645v2crop1645v2
Did you actually paste in code that compiles?
  1. The for loop variable is acct yet you refer to it as act in the body of the loop
  2. The SOQL IN statement refers to accids yet I don't see this declared and probably should be accountIds
  3. You have a reference to act.Pricing__cs but that is an invalid variable token.

<pre>
if (!accountIds.isEmpty()) { //if the accountIds Set is not empty
         for(Account acct : [Select id,Account__c, Account_Owner__c from Pricing__c where Account__c in:accIds]) { //SOQL to get Pricing 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 (Pricing__c p : act.Pricing__cs) { //for all pricings
               if (p.Account_owner__c == oldOwnerId) { //if the pricing is assigned to the old account Owner
                  Pricing__c updatedPricing = new Pricing__c(Id = p.Id, Account_owner__c = newOwnerId); //create a new Contact sObject
                  PricingUpdates.add(updatedPricing__c); //add the pricing to our List of updates
               }
            }
         }
</pre>