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
astroastro 

Adding Account lookup on customController

Hey Guys,

 

The scenario I have is this:

 

I need a visualforce page which will allow a user to select an account.  The standardObject for this page is for a custom object which does not have an existing lookup to an account therefore I cannot use the <apex:inputField />.

 

I am wondering how to approach this problem, how do I go about creating a lookup to the account object without going over governor limits?  I can only bring back 1000 records and the environment I am in has well over that.

 

Any thoughts would be greatly appreciated.  Thank you to all that post.

 

snugglessnuggles
why do you need to bring back over 1000?  Can't you have the user enter some search text then filter your query based on that.  Ideally, your query should only be returning matches, not all records?
astroastro

your absolutely correct snuggles.  

 

So now I'm thinking having an <apex:inputText /> field have the user enter somthing then use a contains keyword in my soql to retrieve what they selected and either tell them to keep searching or that they found an account.

 

is that what is usually done in these scenarios? 

TehNrdTehNrd

Usually you would use a dummy contact in the controller and bind the inputfield to the AccountId on the contact. This will maintain all of the standard lookup functionality.

 

 

public Contact c {get; set;}

//in contructor initiate c

public className(){

c = new Contact();

}

 

//When you want to reference the Account Id value use c.AccountId //Page: <apex:inputfield value="{!c.accountId}"/>

 

 

 

astroastro

Thank you for all your help guys, I went with a combination of all solutions.  Feel like a dolt now since it was so easy.

 

//Controller

public Account acc{set; get;}

 

//VF Page

<apex:inputField  value="{!acc.parentID}" />

 

works exactly like the lookup functionality in sfdc.  I 'stumbled' into this solution so it's kinda sad that it wasn't easy to find (well for me anyways hah).

 

Hope this helps any others who get stuck with the same issue.

 

Thank's again! 

Jimmy_YangJimmy_Yang
the following code segments are for your reference, you can add some elements on it to complish your requriments:

VF page:




Custom Name:





Controller:
public class LookupFieldInputController{

private Contact con;

public void setCon(Contact c) {
con = c;
}

public Contact getCon() {
return con;
}

}
Jimmy_YangJimmy_Yang

the following code segments are for your reference, you can add some elements based on it to complish your requriments, hope it help

VF page:

<apex:page controller="LookupFieldInputController"> <apex:form > <apex:panelGrid columns="2"> <apex:outputLabel>Custom Name:</apex:outputLabel> <apex:inputField value="{!con.AccountId}"/> </apex:panelGrid> </apex:form> </apex:page>

 

 

Controller:

public class LookupFieldInputController{ private Contact con; public void setCon(Contact c) { con = c; } public Contact getCon() { return con; } }

 

astroastro

seems I may have spoke too soon.  The lookup functionality is all there on the front end, but even after choosing an account from the lookup and the text field getting populatd with the name....... the backend never receieves the ID.

 

Anyone run into this as well? 

snugglessnuggles
this depends on what you are trying to do with the account after the user selects it.  remember that you are using a bit of a work around, where you create a dummy contact, and use the input field on that contact to force that lookup.  If you don't then go back and take that dummy contact's account ID and manually do something with it, all you have done is created a temporary dummy contact, set it's account, and left it to be destroyed.
astroastro

Well that's not exactly true I am doing someting with my the ACCOUNT (i'm not referencing contacts in any way).  but it's null for some reason.

 

 

<apex:page standardController="Speaker_Bureau_gne__c" showheader="false" sidebar="false" extensions="em3_bureau_account_nomination_ctrlr"> <apex:pageBlock id="accounts" title="Account Selection" tabStyle="Account"> <apex:messages style="color: red; font-weight: bold; text-align: center;"/> <apex:sectionHeader title="Accounts" subtitle="Please Select an Account for Nomination"/> <apex:pageBlockButtons > <apex:form > <apex:actionFunction action="{!gpb}" name="setAcc"> <apex:param assignTo="{!abc}" name="value" value=""/> </apex:actionFunction> <apex:actionFunction action="{!Next}" name="next" reRender="next"/> <apex:commandButton id="next" value="Next" onclick="alert(document.getElementById('{!$Component.lkp}').value); setAcc(document.getElementById('{!$Component.j_id0:accounts:j_id9:lkp_lkid}').value); alert('complete');" action="{!Next}" onComplete="window.top.opener.location.reload(); window.top.close();" reRender="txt"/> <apex:commandButton value="Cancel" action="{!Cancel}"/> </apex:form> </apex:pageBlockButtons> <apex:form > <apex:InputField value="{!acc.parentID}" id="lkp"/> </apex:form> </apex:pageBlock> </apex:page>

 

 

public class em3_bureau_account_nomination_ctrlr { public boolean acc_check{set; get;} public Speaker_Bureau_gne__c the_bureau; public Account acc{set; get;} public String abc{set; get;} private List<Survey_gne__c> Surveys = new List<Survey_gne__c>(); public em3_bureau_account_nomination_ctrlr(ApexPages.StandardController controller){ this.the_bureau = [select ID, Name, Speaker_Category_gne__c from Speaker_Bureau_gne__c where ID = :ApexPages.currentPage().getParameters().get('id')]; this.acc_check = false; /*if(existing_survey()){ find_existing_memberships(); }else ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'A nomination survey has not been setup for that Bureau category. Press Cancel to return to the Account Detail')); */ } public void gpb(){} public boolean existing_survey(){ /*// Speaker Content this.surveys = [select Speaker_Bureau_Membership_gne__r.Speaker_Category_gne__c, Questionnaire_gne__r.Category_gne__c, Account__c from Survey_gne__c where Questionnaire_gne__r.Category_gne__c = 'Speaker Nomination Survey'AND Account__c = :ApexPages.currentPage().getParameters().get('id')]; if(!this.surveys.isEmpty()){ proceed = true; } return proceed; */ return null; } public PageReference next(){ if(this.abc != null){ this.acc = [select ID from Account where Name = :this.abc limit 1]; } if(this.acc == null){ ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Select an Account'+' Account:'+this.acc+' String:'+this.abc)); return null; }else{ boolean existing_survey = false; this.surveys = [select Speaker_Bureau_Membership_gne__r.Speaker_Category_gne__c, Questionnaire_gne__r.Category_gne__c, Account__c from Survey_gne__c where Questionnaire_gne__r.Category_gne__c = 'Speaker Nomination Survey'AND Account__c = :this.acc.id]; for(Survey_gne__c survey : this.surveys){ if(survey.Speaker_Bureau_Membership_gne__r.Speaker_Category_gne__c == this.the_Bureau.Speaker_Category_gne__c){ existing_survey = true; break; } } if(!existing_survey){ PageReference pRef = new PageReference('/apex/GNE_EM3_CreateSurvey_2?act_id='+this.acc.id+'&spb_id='+ApexPages.currentPage().getParameters().get('id')+'&retURL=/'+ApexPages.currentPage().getParameters().get('id')); return pRef.setRedirect(true); }else{ ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'The HCP does not need to take another Survey')); return null; } } } public PageReference cancel(){ PageReference pRef = new PageReference('/'+ApexPages.currentPage().getParameters().get('id')); return pRef.setRedirect(true); }}

 

no matter what I do even though I can use the inputField exactly how salesforce does by clicking on the mini hourglass lookup my account and retrieve it (the account name shows up in the inputField.  in the backend, it's always null.

 

This is driving me nuts.

 

If I use firebug and inspect the inputField element I can see the id being kept in this id value: "j_id0:accounts:j_id9:lkp_lkid" although  i'm sure this is somewhere in the backend somewhere as a hidden field?

 

this cannot be that hard, I'm probably overcomplicating it.  Just don't seem to grasp it.