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
sami amisami ami 

Trigger to change owner of custom object when account owner changes

Hi,

 

I  need a trigger when account owner changes,my customobject owner(Look up relationship) also changes .

 

Trigger updateowner on Account (after insert,after update) {

    set<Id> setOfAccoutOwnerChanged = new set<Id>();
    for(Account a : Trigger.new){
    if(a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId)
    setOfAccoutOwnerChanged.add(a.Id);
    }
    if(!setOfAccoutOwnerChanged.isEmpty()) {
    List<Customobject__c> listOfCandidatesToUpdate = new List<Customobject__c>();
    
    Map<Id,Account> accountMap = new Map<Id,Account>();
//It error at this line.   
accountmap=[SELECT ID,OwnerId,(Select id,name,ownerid from Customobject__c) FROM Account WHERE Id =:setOfAccoutOwnerChanged]; } }

 

I am failing with my SOQL query...What am i missing.I feel this is similar to account-contact 

Best Answer chosen by Admin (Salesforce Developers) 
JWykelJWykel

Likely to answer your question:
CustomObject__c is not a field on Account, nor is it a list of related objects to Account. Change 'from CustomObject__c' to 'from CustomObject__r' and give that a try. Referencing the list of related objects is similar to accessing a parent related object. Child.Parent__c is the Id of the parent, Child.Parent__r is the actual related parent object.

A question I would have is why are you selecting via Account? You have the setOfAccountOwnerChanged, I would do something like this:

...
for(CustomObject__c co : [SELECT Id, Account__r.OwnerId FROM CustomObject__c WHERE Account__c IN :setOfAccountOwnerChanged]){
    listOfCandidatesToUpdate.add(co);
    co.OwnerId = co.Account__r.OwnerId;
}

update listOfCandidatesToUpdate;

 

All Answers

HaroldHarold

Is the lookup relationship on the custom object or the account?  I would think that the custom object contains the lookup field to account and that's why your soql isn't working.  Can you post the actual error you are getting?

sami amisami ami
Yes look up is on custom object. one to many relation.
My error:

Error: Compile Error: Didn't understand relationship 'Customobject__c' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
HaroldHarold

Then you can't query the account object.  Your query should be something like

 

list<customobject> lObj = new list<customobject>();

 

for(CustomObj o: [Select, id, accountLookupField ownerid from customObject where acccountLookupfield = :setAccountIds]{

    o.ownerId = mapNew.get(accountlookupfield).ownerid;

     lobj.add(o);

}

 

if(lobj != null && lobj.size() > 0) update lobj;

sami amisami ami
I didnt get it.Its same as Account and contact relation right.?
Also what is mapnew?
sami amisami ami
I still get the same error...select id,account__c.ownerId from customobject__c where account__c=:setaccountIds;

I am getting account id when just say:

select id,account__c from customobject__c where account__c=:setaccountIds;

What am i missing?
JWykelJWykel

Likely to answer your question:
CustomObject__c is not a field on Account, nor is it a list of related objects to Account. Change 'from CustomObject__c' to 'from CustomObject__r' and give that a try. Referencing the list of related objects is similar to accessing a parent related object. Child.Parent__c is the Id of the parent, Child.Parent__r is the actual related parent object.

A question I would have is why are you selecting via Account? You have the setOfAccountOwnerChanged, I would do something like this:

...
for(CustomObject__c co : [SELECT Id, Account__r.OwnerId FROM CustomObject__c WHERE Account__c IN :setOfAccountOwnerChanged]){
    listOfCandidatesToUpdate.add(co);
    co.OwnerId = co.Account__r.OwnerId;
}

update listOfCandidatesToUpdate;

 

This was selected as the best answer