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

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
}
}
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
}
}
<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>