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
john dsouzajohn dsouza 

child to parent query

Hello
I have two custom objects , fan__c    and   subscription__c

Fan object has fields (ID, email__c)

subscription object has fields(ID,fan_ID__c,mailing_list_name)

subscription object has a lookup to fan object as  : Fan_ID__cLookup(Fan)

pls help in framing the subscription query from child to parent where child is subscription object and parent is fan object.

I want to frame a query on subscription object that will fetch the related email of fan object

pls help me out
thanks
JohnD
 
Best Answer chosen by john dsouza
ManojjenaManojjena
Hi JohnD,

Please try to modify your query as below it will help !!
List<subscription__c> subList=[SELECT Id,Fan_ID__r.email__c,Mailing_List_ID__r.Name FROM subscription__c
                               LIMIT 50];
Let me know if it helps
Thanks
Manoj
 

All Answers

ManojjenaManojjena
HI john dsouza,

You can frame you rquery like below .
List<subscription__c > subList=[SELECT Id,Fan_ID__r.email__c FROM subscription__c]; 
You can add additional fields inside the query with comma separated .

Let me know if it helps !!
Thanks

Manoj

Tanuj TyagiTanuj Tyagi
Hi John ,

If you want to query email of fan object from subscription object , you should write your query like this :
Select Name, fan__r.Email__c from subscription__c

Hope it helps !!
Cheers !!
john dsouzajohn dsouza
Hi Manoj
Thanks for the query.
the query doesnt fetch the emails. 

I created a loop to fetch email. But its not showing up.
for(subscription__c sub:subList)
{
  system.debug(sub);
}

can u le tme know how to improvise this?

thanks
JohnD
john dsouzajohn dsouza
Hi Manoj
OK, I improvised this as foolows and got the emails.
List<subscription__c> subList=[SELECT Id,Fan_ID__r.email__c FROM subscription__c
                               LIMIT 50];
for(subscription__c sub:subList)
{
    system.debug(sub.fan_ID__r.email__c);
}

There is one more point where I require a clarification to close this post.

In addition to the mentioned custom objects, I have another custom object as Mailing_List__c which has the field as Mailing_List_Name__c.

Now Mailing_List__c is a master object and related child object is subscription__c, so there is a master detail relaitonship between
Mailing_List__c and subscription__c .

so Mailing_List_ID__c      is the  api name in Master-Detail(Mailing List)

Now I need to fetch mailing_list_Name__c from the subscription query I mentioned above.

any help in this will be appreciated.

Rather than storing the return in an list variable can it be a Map?
I am novice in using map so I am asking.

thanks
JohnD
 
ManojjenaManojjena
Hi JohnD,

Please try to modify your query as below it will help !!
List<subscription__c> subList=[SELECT Id,Fan_ID__r.email__c,Mailing_List_ID__r.Name FROM subscription__c
                               LIMIT 50];
Let me know if it helps
Thanks
Manoj
 
This was selected as the best answer
john dsouzajohn dsouza
Hi Manoj
If I remove the LIMIT clause from above query it throws 50001 rows error.
How can I avoid this?

Thanks
JohnD

 
ManojjenaManojjena
HI john dsouza,

You can add more filter to the query as per your requirment ,else you need to go for asyncronous apex .

Thanks
Manoj
john dsouzajohn dsouza
Hi Manoj
Batch apex sounds gr8. I think I have mo other option left.
Do you mind If I keep this post open for a couple of days?

Thanks
John
ManojjenaManojjena
its ok John ,No problem .

Thanks
Manoj
john dsouzajohn dsouza
Thanks Manoj
My issue is resolved.

Thanks
John