You need to sign in to do that
Don't have an account?
bpl3792
Objects normalized. How do you reference an id to a name across 2 different objects?
I'm still learning apex so this might look odd...
In the following visualforce code I'm pulling a name and an id from 1 object. In a different object that same id is associated with a name. How would I be able to do a "join" in soql so that they can display the data properly?
<apex:page standardController="Contact" extensions="NL_Filter" action="{!doLoadAttachments}" id="page"> <apex:form id="form"> <apex:outputPanel id="list" layout="block"> <apex:pageBlock id="pb" title="Attachments" mode="view"> <apex:pageBlockTable id="pbt" value="{!AttachmentList}" var="Attachment" > <apex:facet name="header"> <apex:outputPanel layout="block"> </apex:outputPanel> </apex:facet> <apex:column headerValue="Title"> <apex:outputText value="{0}"> <apex:param value="{!Attachment.Name}"/> </apex:outputText> </apex:column> <apex:column headerValue="Created By"> <apex:outputText value="{0}"> <apex:param value="{!Attachment.CreatedById}" /> </apex:outputText> </apex:column> </apex:pageBlockTable> </apex:pageBlock> </apex:outputPanel> </apex:form> </apex:page>
public with sharing class NL_Filter{ public Contact provider {get;set;} public List<Attachment> AttachmentList {get;set;} public List<Contact> ContactList {get;set;} public Map<Contact,Attachment> ContactAttachment {get;set;} public NL_Filter(ApexPages.StandardController controller) { provider=(Contact)controller.getRecord(); } public PageReference doLoadAttachments(){ final List<Attachment> Val=new List<Attachment>(); String query ='SELECT Name, LastModifiedDate, CreatedById FROM Attachment WHERE ParentId=\'0034000000KsrvZAAR\''; String query2='Select Name, id from Contact where id=\'0034000000KsrvZAAR\''; AttachmentList=Database.query(query); ContactList=Database.query(query2); for(Contact Contacts:ContactList){ for(Attachment Attachments:AttachmentList){ } } return null; } }
Do you want 1 query result or 2?
For 1 Query Result you would:
Account[] lAccts = [Select ID, (SELECT Name, LastModifiedDate, CreatedById FROM Attachments__r),(Select Name, id from Contacts__r) From Account Where ID = 'YOURIDHERE IF NEEDED'];
Then to access it you would:
For(Attachment a : lAccts.Attachments__r){
//do attachment stuff here
}
For(Contact c : lAccts.Contacts__r){
//do attachment stuff here
}
For 2 seperate queries
Contact[] c = [Select Name, id from Contacts where AccountID = 'YOURACCTIDHERE'];
Attachment[] a = [SELECT Name, LastModifiedDate, CreatedById FROM Attachment Where parentID = 'YOURACCTIDHERE'];
Then access directly from the variable (i.e. a.CreatedById or c.Name)
Hmm with my current code the screen shot shows what's returned. The "Created By" id shown in the screen shot is the id for the contacts object. The data being displayed is from the Attachment object...I'm trying to figure out how I can get the names from the Contacts object to show up in place of the "Created By" id.
Use the first method I posted and set the return the value of lAccts.Attachments__r to the pageBlock Table Value.
I get
"Save error: Didn't understand relationship 'Attachment__r' 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."
When trying to use that.
Perhaps because it is supposed to be
Attachments__r
I was able to get the result I needed by doing String query ='SELECT Name, LastModifiedDate, CreatedBy.Name FROM Attachment WHERE ParentId=\'0034000000KsrvZAAR\'';
I did try the method you recommended with "Attachments__r" I just changed it thinking the spelling was wrong. I guess that's how you're supposed to do relationships though.
Thanks for the help!