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
snapssnaps 

Running a query to pull email addresses from an object related to opportunity

Hey guys,

 

We are trying to run a SOQL query to pull a list of email addresses, and then want to be able to draft and send an email to them.

 

We are basically copying the info from the visualforce developer guide and trying to adjust it to our data model, but are running into some issues in compiling the list of emails.(http://www.salesforce.com/us/developer/docs/pages/Content/pages_email_intro.htm)

 

 

Data Model:

We are trying to send this email from an opportunity.  Opportunities have a lookup to an object called the Panel Tool.  There is a junction object called "panelists" that links the panel tool to contacts, and so we are pulling the contact's emails via the soql query, and then trying to send to the list of emails.

 

This SOQL Query seems to work, but there could well be a better way.  We pulled the contact's email to the junction object via a formula field to try to simplify things, but the query works either way.

 

SELECT Name,(SELECT Name,Panelists_Email__c FROM Panelists__r) FROM Panel__c WHERE Id IN (Select Assigned_to_Panel__c FROM Opportunity WHERE Id= :ApexPages.currentPage().getParameters().get('id'))];

 

Here is the link to our code:

http://pastebin.com/fWEji9ry

 

Currently we are getting a compilation error: Invalid foreign key relationship: Panel__c.Assigned_to_Panel__r at line 22 column 25

 

Right now we don't really know how else to proceed, and would like some tips and/or feedback with our query/code.

Best Answer chosen by Admin (Salesforce Developers) 
Puja_mfsiPuja_mfsi

Hi,

Please change "panel.Assigned_to_Panel__r.Panelists[0].Panelists_Email__c" to 

 

panel.Panelists__r[0].Panelists_Email__c.

 

And change "panel.Assigned_to_Panel__r.Panelists[0]" to "panel.Panelists__r[0]" in all places.

 

 

 

 

All Answers

souvik9086souvik9086

Hi,

 

try this by breaking the query like this

 

Opportunity oppObj = [Select Assigned_to_Panel__c FROM Opportunity WHERE Id= :ApexPages.currentPage().getParameters().get('id')];

 

List<Panel> panList = [SELECT Name,(SELECT Name,Panelists_Email__c FROM Panelists__r) FROM Panel__c WHERE Id IN: oppObj.id];

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

Thanks

 

Puja_mfsiPuja_mfsi

Hi,

Please change "panel.Assigned_to_Panel__r.Panelists[0].Panelists_Email__c" to 

 

panel.Panelists__r[0].Panelists_Email__c.

 

And change "panel.Assigned_to_Panel__r.Panelists[0]" to "panel.Panelists__r[0]" in all places.

 

 

 

 

This was selected as the best answer
snapssnaps

Thank you both for your answers. Souvik, I appreciate you showing me an alternative way to approach my problem - it'll come in handy one day. Puja, your answer was exactly what I needed.