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
tsalbtsalb 

Retrieve All Contacts from same Account into picklist

I'm looking for some help, either code samples or documentation to point me towards that can help me achieve this. On an existing VF page, we are currently dislpaying Proposal__c record data and we are exposing and collecting data already.

 

What I need now is to be able to select a Contact, whose Account is associated to that record - in a picklist form or list form (most of our accounts for this purpose are <50 contacts).

 

On each Proposal__c is a Vendor__c (Contacts lookup). So basically, i need some help constructing the SOQL query to return the list of other Vendors (Contact lookup) associated to that Account. The user will basically be able to save to the record, one other contact. I'm not sure if a picklist or lookup is going to be easier in this scenario. 

 

Relevant code below:

<apex:page standardController="Proposal__c" extensions="ProposalExtension" tabStyle="Proposal__c" cache="false">
    <apex:pageMessages id="pageErrors"></apex:pageMessages>
    <apex:sectionHeader title="New Award"/>
    <apex:form id="awardForm">
        <apex:pageBlock title="Bid Detail" id="awardBlock" mode="edit">          
            <apex:pageBlockButtons >
                <apex:commandButton value="Send LOE" action="{!sendLOE}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Bid Information" columns="2" collapsible="false" showHeader="false">
                <apex:outputField value="{!bid.Vendor__r.Name}"/>        <apex:outputField value="{!bid.Bid_Date__c}"/>  
                <apex:outputField value="{!bid.Bid_Amount__c}"/>         <apex:outputField value="{!bid.Bid_Deliver_By__c}"/>  
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Award Information" id="results" columns="2" collapsible="false">     
                <apex:inputField value="{!bid.Award_Amount__c}" required="true"/>        <apex:inputField value="{!bid.Awarded_Date__c}" required="true"/> 
                <apex:inputField value="{!bid.Award_Deliver_By__c}" required="true"/>    <apex:inputField value="{!bid.Award_Comments__c}"/> 
            </apex:pageBlockSection>          
        </apex:pageBlock>      
    </apex:form>
</apex:page>

 

 

Best Answer chosen by Admin (Salesforce Developers) 
tsalbtsalb

I couldn't figure out how to use those queries...but i did figure it out in a different way (probably not best practice). Here's how I did it - but if anyone could look over my controller / VF page and use the method described above i would appreciate it

 

 

 

public with sharing class ProposalExtension {
	
    private final Proposal__c p;
    public String rfpId = ApexPages.currentPage().getParameters().get('rfpId');
    public String vendorId = ApexPages.currentPage().getParameters().get('vendorId');
    public String orderId = ApexPages.currentPage().getParameters().get('orderId');
    public String orderType = ApexPages.currentPage().getParameters().get('orderType');
    public String vendorAccId = ApexPages.currentPage().getParameters().get('vendorAccId'); // Added this for SPOCS method below
    //Constructor for ProposalExtension	
    public ProposalExtension(ApexPages.StandardController pCon) {
        this.p = (Proposal__c)pCon.getRecord();
    }
    
	//Proposal used for vendor pages 
    Proposal__c rfp;
    
    public Proposal__c getRFP(){
    	
	    rfp = [SELECT Id, Name, BPN__c, Vendor__r.Name, Vendor__r.Account.Name, Vendor__r.MailingStreet, 
		Vendor__r.MailingCity, Vendor__r.MailingState, Vendor__r.MailingPostalCode, Vendor__r.Average_Vendor_Overall_Rating__c, 
		Service_Request__r.Name, Service_Request__r.Property_Name__c,
		FROM Proposal__c WHERE Id = :rfpId AND Vendor__c = :vendorId];
    	
    	return rfp;
    }
    //Code for Contact Picklist
    public List<selectOption> getSPOCS(){
    	List<selectOption> options = new List<selectOption>();
    	options.add(new selectOption('', '- None -'));
    	for (Contact con: [SELECT Id, Name FROM Contact WHERE Contact.Account.Id = :vendorAccId]) {
    		options.add(new selectOption(con.Id, con.Name));
    	}
    	return options;
    }
 
    //Proposal used for internal pages
    Proposal__c bid;
    
    public Proposal__c getBid(){
    	bid = [SELECT Id, Name, Vendor__r.Name, Vendor__r.Account.Id, Vendor__r.Account.Name, Vendor__r.Average_Vendor_Overall_Rating__c,
    		Vendor__r.Average_of_Vendor_Future_Use__c, Service_Request__r.Address__c, Service_Request__r.Type__c, 
    		Sent_Date__c, Bid_Amount__c, Bid_Deliver_By__c, Bid_Date__c, Bid_Comments__c, Award_Amount__c, Awarded_Date__c,  
    		Award_Deliver_By__c, Award_Comments__c, Status__c, Vendor__r.Update_Request_Sent__c, SPOCL__c
    		FROM Proposal__c WHERE Id = :rfpId];
    	bid.Awarded_Date__c = system.today();
    	return bid;
    }
    
}

 

My relevant VF page codeblock:

<apex:pageBlockSection columns="1" showHeader="false">
	<apex:pageBlockSectionItem >
		<apex:outputLabel value="Single Point of Contact" for="spoc"></apex:outputLabel>
		<apex:selectList id="spoc" value="{!rfp.SPOCL__c}" size="1" title="Single Point of Contact Select">
			<apex:selectOptions value="{!SPOCS}"></apex:selectOptions>
		</apex:selectList>
	</apex:pageBlockSectionItem>
</apex:pageBlockSection>     

 

All Answers

Ronak PatelRonak Patel

In Controller you can use subquery on Account Query

Ex.

 

Account a=[Select id,name,(select id,name from contacts) from account];

list<Contact> lstcon=a.Contacts;

From this list you can make pick list whichh one you want?

Navatar_DbSupNavatar_DbSup

Hi,

 

Try below sample code

 

 

List<Contact> s =[Select Contact.Account.Name,Contact.Account.id from Contact where Contact.Name='asha aas' ];

set<string> stids= new set<string>();

for(Contact d : s)

{

    stids.add(d.Account.id);

}

List<Contact> c =[SELECT Contact.FirstName,Contact.LastName,Contact.Id, Contact.Account.Name from Contact where Contact.Account.Id in :stids];

system.debug(c);

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

tsalbtsalb

I couldn't figure out how to use those queries...but i did figure it out in a different way (probably not best practice). Here's how I did it - but if anyone could look over my controller / VF page and use the method described above i would appreciate it

 

 

 

public with sharing class ProposalExtension {
	
    private final Proposal__c p;
    public String rfpId = ApexPages.currentPage().getParameters().get('rfpId');
    public String vendorId = ApexPages.currentPage().getParameters().get('vendorId');
    public String orderId = ApexPages.currentPage().getParameters().get('orderId');
    public String orderType = ApexPages.currentPage().getParameters().get('orderType');
    public String vendorAccId = ApexPages.currentPage().getParameters().get('vendorAccId'); // Added this for SPOCS method below
    //Constructor for ProposalExtension	
    public ProposalExtension(ApexPages.StandardController pCon) {
        this.p = (Proposal__c)pCon.getRecord();
    }
    
	//Proposal used for vendor pages 
    Proposal__c rfp;
    
    public Proposal__c getRFP(){
    	
	    rfp = [SELECT Id, Name, BPN__c, Vendor__r.Name, Vendor__r.Account.Name, Vendor__r.MailingStreet, 
		Vendor__r.MailingCity, Vendor__r.MailingState, Vendor__r.MailingPostalCode, Vendor__r.Average_Vendor_Overall_Rating__c, 
		Service_Request__r.Name, Service_Request__r.Property_Name__c,
		FROM Proposal__c WHERE Id = :rfpId AND Vendor__c = :vendorId];
    	
    	return rfp;
    }
    //Code for Contact Picklist
    public List<selectOption> getSPOCS(){
    	List<selectOption> options = new List<selectOption>();
    	options.add(new selectOption('', '- None -'));
    	for (Contact con: [SELECT Id, Name FROM Contact WHERE Contact.Account.Id = :vendorAccId]) {
    		options.add(new selectOption(con.Id, con.Name));
    	}
    	return options;
    }
 
    //Proposal used for internal pages
    Proposal__c bid;
    
    public Proposal__c getBid(){
    	bid = [SELECT Id, Name, Vendor__r.Name, Vendor__r.Account.Id, Vendor__r.Account.Name, Vendor__r.Average_Vendor_Overall_Rating__c,
    		Vendor__r.Average_of_Vendor_Future_Use__c, Service_Request__r.Address__c, Service_Request__r.Type__c, 
    		Sent_Date__c, Bid_Amount__c, Bid_Deliver_By__c, Bid_Date__c, Bid_Comments__c, Award_Amount__c, Awarded_Date__c,  
    		Award_Deliver_By__c, Award_Comments__c, Status__c, Vendor__r.Update_Request_Sent__c, SPOCL__c
    		FROM Proposal__c WHERE Id = :rfpId];
    	bid.Awarded_Date__c = system.today();
    	return bid;
    }
    
}

 

My relevant VF page codeblock:

<apex:pageBlockSection columns="1" showHeader="false">
	<apex:pageBlockSectionItem >
		<apex:outputLabel value="Single Point of Contact" for="spoc"></apex:outputLabel>
		<apex:selectList id="spoc" value="{!rfp.SPOCL__c}" size="1" title="Single Point of Contact Select">
			<apex:selectOptions value="{!SPOCS}"></apex:selectOptions>
		</apex:selectList>
	</apex:pageBlockSectionItem>
</apex:pageBlockSection>     

 

This was selected as the best answer
tsalbtsalb

Extra Question - If i wanted to output this as a searchable lookup - how would i do that? 

 

Right now i'm using apex:selectList - but i'd rather use the component that gives me the standard lookup dialogue like when i hit the magnifying glass on standard layout pages.

ohmohm

Can anyone tell me why this code might work for me while in development but not in production?

 

In particular, the code for the contact picklist:

//Code for Contact Picklist
    public List<selectOption> getSPOCS(){
    	List<selectOption> options = new List<selectOption>();
    	options.add(new selectOption('', '- None -'));
    	for (Contact con: [SELECT Id, Name FROM Contact WHERE Contact.Account.Id = :vendorAccId]) {
    		options.add(new selectOption(con.Id, con.Name));
    	}
    	return options;
    }