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
Tyler HarrisTyler Harris 

Create Lookup field to Contact on Custom Controller

How can I display a look up field to add a contact to a variable in Apex.

Visualforce
<apex:form >
      <apex:dataTable id="contacts" value="{!ct}" var="conz">
          <apex:inputField value="{!conz.id}" />
      
      </apex:dataTable>

Apex
public virtual class StoreFront2 {    

    public Contact ct{get;set;}   
    
    public void handleContact(){
    if(ct == null){
    ct = new Contact();
        ct.id = null;}
}
}

Thanks in advance.
Ajay K DubediAjay K Dubedi
If you Want to create a custom lookup field for contact Thab you hae to create a custom field On Contact Object with lookup to the Contact object than you can use that custom fiel in your controller class and Visualforce Page.  

Eg: 
Visual Force Page:
 
<apex:page Controller="CustomLookup">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:inputField value="{!ct.Contact1__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Contrller Class:
public class CustomLookup {
public Contact ct{get;set;}
public CustomLookup(){
 ct=[select Contact1__c from Contact limit 1];
}
}

Do let me know if this Help!
Tyler HarrisTyler Harris
Sorry this isn't working for me.

No such column 'Contact1__c' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. Line 13

I just want to be able for a user to select a Contact and pass it to a variable or wrapper class.

Apex
 
public virtual class StoreFront2 {    
    public String message { get; set; }
    List<DisplayMerchandise> products;
    Map<Id, DisplayMerchandise> cart;
    public Boolean incart{get;set;}
    public Id rowz;
    public id rowDel{get;set;}
  
    public class CustomLookup{
    public Contact ct{get;set;}   
    
        public CustomLookup(){
            ct=[select Contact1__c from Contact limit 1];
        }
    }
    
    public PageReference shop(){
   
        handleTheBasket();
        message = 'You bought: ';
        for (DisplayMerchandise p:products){
            if(p.tempCount > 0){
               message += p.merchandise.name + ' (' + p.tempCount + ') ' ;
               }
            }
        return null;
    }
    
    public void remove(){
     rowz = (Id) ApexPages.currentPage().getParameters().get('rowDel');
        system.debug(rowz);
        if(cart.containsKey(rowz)){
            cart.remove(rowz);
            if(cart.isEmpty()){
                incart = false;
                system.debug(incart);
            }
    
            
        }  
         
    }

        public pageReference back(){
        PageReference doit = new PageReference('/apex/StoreCart');
        doit.setRedirect(false);
            return doit;
        }
    
    	public void confirm(){
            
        List<Purchases__c> pur = new List<Purchases__c>();
            
        
    	}
    
    public Pagereference checkout(){
        if(cart.isEmpty()){
            ApexPages.Message myError = new ApexPages.Message(ApexPages.Severity.ERROR, 'Shopping Cart is Empty');
            ApexPages.addMessage(myError);
            System.debug('on');
            return null;
            
        }
        else{
        PageReference send = new PageReference('/apex/ConfirmBuy');
        return send;
        }
    } 
    
    
    public void handleTheBasket(){
        for(DisplayMerchandise c : products){
            if(c.tempCount > 0){
            if(cart.containsKey(c.merchandise.Id)){
                
                cart.get(c.merchandise.Id).count += c.tempCount;
                
            }
            else{
                cart.put(c.merchandise.Id, c);
                cart.get(c.merchandise.Id).count = c.tempCount;
                incart = true;
                System.debug(incart);
            
            } 
        
        }
        }
        
    }
    
    public Map<Id, DisplayMerchandise> getCart() {
        if(cart == null){
            cart = new Map<Id, DisplayMerchandise>();
            incart = false;
                }
      
        return cart;
    }

        
       
    
    public class DisplayMerchandise {
        public Merchandise__c merchandise{get; set;}
        public Decimal count{get; set;}
        public Decimal tempCount{get;set;}
        public DisplayMerchandise(Merchandise__c item){
            this.merchandise = item;
            
        }
    }

    public List<DisplayMerchandise> getProducts() {
        if (products == null){
            products = new List<DisplayMerchandise>();
    
            for (Merchandise__c item :
            [SELECT id, name, description__c, price__c
            FROM Merchandise__c
            WHERE Total_Inventory__c > 0]) {
           
            products.add(new DisplayMerchandise(item));
            }
        }
        return products;
    }

}

Visualforce
<apex:page controller="StoreFront2" showHeader="false" sidebar="false" >
    <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>
    <h1>Confirm Page</h1>
  <apex:form >
      <apex:dataTable id="contacts" value="{!ct}" var="conz">
          <apex:inputField value="{!ct.Contact1__c}" />
      
      </apex:dataTable>
     <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">
			
   <apex:column headerValue="ID" rendered="false">
       <apex:outputText value="{!cart[carti].merchandise.Id}" >
       </apex:outputText>
          </apex:column>
          <apex:column headerValue="Product">
              <apex:outputText value="{!cart[carti].merchandise.name}">
          	
         </apex:outputText>
          </apex:column>
          <apex:column headervalue="Price">
              <apex:outputText value="{!cart[carti].merchandise.price__c}" />
          </apex:column>
          <apex:column headerValue="#Items">
              <apex:outputText value="{!cart[carti].count}"/>
          </apex:column>
      </apex:dataTable>
      <apex:commandButton action="{!back}" value="Back"/>
    </apex:form>
  
</apex:page>

 
Ajit Kumar 19Ajit Kumar 19
Use that code in your constructor like
public Contact ct{get;set;}
Public StoreFront2(){
 ct=[select Contact1__c from Contact limit 1];
}

if this also not working than use only that part of code which is give above because that code is workin for me
Tyler HarrisTyler Harris
Well I want to be able for the user to choose the Contact record from the database and I'll write some logic to bind that Contact to the Purchase. However, I can't seem to add a lookup field to find the Contact in the system.

Errors
StoreFront2 Line 12 No such column 'Contact1__c' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

ConfirmBuy Line 1 Unknown property 'StoreFront2.ct'

Apex:
public virtual class StoreFront2 {    
    public String message { get; set; }
    List<DisplayMerchandise> products;
    Map<Id, DisplayMerchandise> cart;
    public Boolean incart{get;set;}
    public Id rowz;
    public id rowDel{get;set;}

    public Contact ct{get;set;}
    
    public StoreFront2(){
        ct=[select Contact1__c from Contact Limit 1];
        
    }
    
    public PageReference shop(){
   
        handleTheBasket();
        message = 'You bought: ';
        for (DisplayMerchandise p:products){
            if(p.tempCount > 0){
               message += p.merchandise.name + ' (' + p.tempCount + ') ' ;
               }
            }
        return null;
    }
    
    public void remove(){
     rowz = (Id) ApexPages.currentPage().getParameters().get('rowDel');
        system.debug(rowz);
        if(cart.containsKey(rowz)){
            cart.remove(rowz);
            if(cart.isEmpty()){
                incart = false;
                system.debug(incart);
            }
    
            
        }  
         
    }

        public pageReference back(){
        PageReference doit = new PageReference('/apex/StoreCart');
        doit.setRedirect(false);
            return doit;
        }
    
    	public void confirm(){
            
        List<Purchases__c> pur = new List<Purchases__c>();
            
        
    	}
    
    public Pagereference checkout(){
        if(cart.isEmpty()){
            ApexPages.Message myError = new ApexPages.Message(ApexPages.Severity.ERROR, 'Shopping Cart is Empty');
            ApexPages.addMessage(myError);
            System.debug('on');
            return null;
            
        }
        else{
        PageReference send = new PageReference('/apex/ConfirmBuy');
        return send;
        }
    } 
    
    
    public void handleTheBasket(){
        for(DisplayMerchandise c : products){
            if(c.tempCount > 0){
            if(cart.containsKey(c.merchandise.Id)){
                
                cart.get(c.merchandise.Id).count += c.tempCount;
                
            }
            else{
                cart.put(c.merchandise.Id, c);
                cart.get(c.merchandise.Id).count = c.tempCount;
                incart = true;
                System.debug(incart);
            
            } 
        
        }
        }
        
    }
    
    public Map<Id, DisplayMerchandise> getCart() {
        if(cart == null){
            cart = new Map<Id, DisplayMerchandise>();
            incart = false;
                }
      
        return cart;
    }

        
       
    
    public class DisplayMerchandise {
        public Merchandise__c merchandise{get; set;}
        public Decimal count{get; set;}
        public Decimal tempCount{get;set;}
        public DisplayMerchandise(Merchandise__c item){
            this.merchandise = item;
            
        }
    }

    public List<DisplayMerchandise> getProducts() {
        if (products == null){
            products = new List<DisplayMerchandise>();
    
            for (Merchandise__c item :
            [SELECT id, name, description__c, price__c
            FROM Merchandise__c
            WHERE Total_Inventory__c > 0]) {
           
            products.add(new DisplayMerchandise(item));
            }
        }
        return products;
    }

}

Visualforce
<apex:page controller="StoreFront2" showHeader="false" sidebar="false" >
    <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>
    <h1>Confirm Page</h1>
  <apex:form >
      <apex:pageBlock>
     		<apex:pageBlockSection>
          		<apex:inputField value="{!ct.Contact1__c}"/>
          		<apex:inputText title="First Name" />
          		<apex:inputText title="Last Name" />
          		<apex:inputText title="Email Address"/>
          	</apex:pageBlockSection>
      </apex:pageBlock>
      
     <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">
			
   <apex:column headerValue="ID" rendered="false">
       <apex:outputText value="{!cart[carti].merchandise.Id}" >
       </apex:outputText>
          </apex:column>
          <apex:column headerValue="Product">
              <apex:outputText value="{!cart[carti].merchandise.name}">
          	
         </apex:outputText>
          </apex:column>
          <apex:column headervalue="Price">
              <apex:outputText value="{!cart[carti].merchandise.price__c}" />
          </apex:column>
          <apex:column headerValue="#Items">
              <apex:outputText value="{!cart[carti].count}"/>
          </apex:column>
      </apex:dataTable>
      <apex:commandButton action="{!back}" value="Back"/>
    </apex:form>
  
</apex:page>

 
Ajit Kumar 19Ajit Kumar 19
Hey Do you have created a custom field name Contact1__c havint lookup to the Contact object
Tyler HarrisTyler Harris
No, I just want to be able to create a lookup field that has access to Contacts in the system. I want to be able to pass that Contact ID to a variable in my Apex code.