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
ChubbyChubby 

display one child record id on another child record

Hi All,
I have the below requirement. I have one parent and 2 childs. i want to get ids of two child records and update a field with id of child 2 on child1 and vice versa. How can i do this?

Thanks.
FARSANA PSFARSANA PS
Hi,
You can use inner query for querring child record from parent
for eg:
consider this scenario,
account as parent , contact and opportunity as child
select name,id,(select id,name from contacts),(select id,name from opportunities) from account
list<account> ac=[select name,id,(select id,name from contacts),(select id,name from opportunities) from account];
for(account a:ac)
{
    for(contact c:a.contacts)
    {
     for(opportunity o:a.opportunities)
     {
        //o.field1__c=c.id
         //c.field2__c=o.id  
     }
    }
}
Hope this helps..
 
Ajay K DubediAjay K Dubedi
Hi Chubby,

Nested SOQL : 

In Nested SOQL, we can retrieve the data of the related object using the SOQL query. We can query on the parent object and get details about parent and child records.

For example, following SOQL query retrieves Accounts, related Contacts, and related Cases associated with each Account:

Select Id, Name, (Select Id From Contacts), (Select Id From Cases) from Account LIMIT 10

Please mark as best answer if it helps you.

Thank You, 
Ajay Dubedi
ChubbyChubby
Hi,
 Thanks for your thoughts.
 Here I have Account and contact1, contact2. I need to query and then update Id in contact1 with Id of contact2. I am getti the records in query but how to assign values? Please help.

Thanks.