You need to sign in to do that
Don't have an account?
sami 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
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:
All Answers
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?
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.
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;
Also what is mapnew?
I am getting account id when just say:
select id,account__c from customobject__c where account__c=:setaccountIds;
What am i missing?
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: