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
Rachid El KaddouriRachid El Kaddouri 

How to prepopulate a Lookup field in VisualForce page?

Hi everyone,

I'm a little bit stuck at a point to prepopulate a Name from the Contact Object into a Custom Object. Situation: I have a VF page that I use to create new records, I replaced the SF standard NEW button with a custom VF page. In this VF Page there is a LookUp to the Contacts Object but I don't want Users to select the contact from the LookUp. Instead I want to auto-populate the User Name in the Contact field.
I searched for answers but I couldn't find anything. Hope somebody can help me! Below you can find the code snippet of the controller:
public class CreateMultipleVGAvailability{
    public String VGAvailabilityLink{
      get{
             String VGAvailabilityLink = URL.getSalesforceBaseUrl().toExternalForm() + '/a3R/o' ;
             return VGAvailabilityLink;
 }
 }
public VG_Availability__c MasterAvailability;
public CreateMultipleVGAvailability(ApexPages.StandardController stdController) {
this.MasterAvailability = (VG_Availability__c)stdController.getRecord();       
 }
public void CreateVGAvailabilities(){

        List<VG_Availability__c> NewAvailabilities = new list<VG_Availability__c>();
        String DifferentDates = Apexpages.currentPage().getParameters().get('dates');
        String Message;
        String ContactDateCombination;
         
        List<String> Dates = DifferentDates.split(', ');
                 for(String D: Dates){
            
            Date myDate = date.parse(D);
            
        VG_Availability__c NewRecord = new VG_Availability__c();
        NewRecord.contact__c = MasterAvailability.contact__c;
        NewRecord.Is_Available__c = MasterAvailability.Is_Available__c;
        NewRecord.Comments__c = MasterAvailability.Comments__c;        
        NewRecord.Date__c = myDate;
        ContactDateCombination = string.valueof(NewRecord.contact__c) + string.valueof(myDate);
            
AggregateResult[] ContactDateCombinationAmount = [SELECT count(Id) Amount FROM VG_Availability__c WHERE ContactDateCombination__c =: ContactDateCombination];
            
            if( ContactDateCombinationAmount[0].get('Amount') != 1){
                NewAvailabilities.add(Newrecord);
            }else{
                if(Message == Null){
                    Message = myDate + ', ';               
                }else{
                    Message = Message + myDate + ', '; 
                }
            
            }  
    }
    if(Message != Null){
 
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error Message.');
        ApexPages.addMessage(myMsg); 
    }
 
    Insert(NewAvailabilities);
    }

}


KevinPKevinP
Checkout the controversial topic of URL hacking! Links left to google.
Rachid El KaddouriRachid El Kaddouri
URL hacking for a custom VF page isn't possible... I tried URL Hacking before with Standard SF buttons but not with Buttons that link to a custom visualforce page with extension controller..
KevinPKevinP
sure you can url hack a custom vf page.  also look at using a publisher action instead. --  Kevin Poorman Sent with Airmail
Rachid El KaddouriRachid El Kaddouri
I'm overriding the standard new button with a custom vf page button.. I can't get it to work with the URL hacking.. I'm trying to find a solution within the controller.. Any ideas someone else??
Rachid El KaddouriRachid El Kaddouri
Never mind, I allready solved the problem within the controller..