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
Balu_devBalu_dev 

Getting Child Account object records from Parent Account..

There is an One-to-Many Parent-child relationships in Account object.. I am retrieving all the Parent Accounts from trigger.new and putting it in a set..now for every parent  i have to retrive its child accounts and update both parent and its child,,please help me as soon as possible...

Starz26Starz26

To get ther child records:

 

Map<id,Account> mAccChildren = New Map<id,Account> ([Select ID, (Select CHILDFIELDS From CHILDRELATIONSHIPNAME) From Account WHERE YOUCONDITIONS]);

 

To get the values out use the account id from the curred record you are processing in the trigger:

 

mAccChildren.get(AccountID).CHILDRELATIONSHIPNAME__r.FIELDNAME

 

Example:

 

Parent = Account

Child = cObject

Relationship on cObject = childObjects__r

Fields on child = fieldA and fieldB

 

Map<id,Account> mAccChildren = New Map<id,Account> ([Select ID, (Select fieldA, FieldB From childObjects__r) From Account WHERE id in :setAccID);

 

To update fieldA and fieldB:

 

mAccChildren.get(AccID).childObjects__r.fieldA = xxxxx

mAccChildren.get(AccID).childObjects__r.fieldB = xxxxx

 

There are other ways using lists, loops, etc

 



Balu_devBalu_dev

Hi,

 

Thanks for replying..but i have a question here..

 

will this work if the child is also an account record(standard object).??

 

 

 

Starz26Starz26

Yes, just use the child relationship name from the child object.

 

 

Balu_devBalu_dev

i think this is right  but iam getting an error and couldnt find whats the reason :

 

([Select ID,Name(Select Id, name, Account_Class__c From account__r) From Account WHERE id in:Parentaccs]);

 

if i understand this correctly this should give all the child  accounts, is it right ?   

Starz26Starz26

you forgot the , after the name field:

 

([Select ID,Name, (Select Id, name, Account_Class__c From account(s)__r) From Account WHERE id in:Parentaccs]);

 

Also, typically the child api name is pleural so check to ensure it sould not be Accounts__r instead of Account__r. I put the (s) in the spot in question.....

Balu_devBalu_dev

Hi,

 

I did try the below soql query but I am getting the error as mentioned :

 

Error: Compile Error: Didn't understand relationship 'accounts__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure append the '__r' after the custom relationship name.

 

Map<id,Account> mAccChildren = New Map<id,Account> 
   ([Select ID,name,Account_Class__c,(Select Id, name,Account_Class__c From account__r)From Account WHERE id in :Parentaccs]);

Map<id,Account> mAccChildren = New Map<id,Account> 
   ([Select ID,name,Account_Class__c,(Select Id, name,Account_Class__c From accounts__r)From Account WHERE id in :Parentaccs]);

 

Any idea of how to proceed further?