2

I am looking to use an Apex Class in a Visualforce page that populates a field based on the result of a SoQL query.

The idea is that I am making a custom Save And Do X button which must be available before a record is saved, hence needing a VFpage.

I have the VFpage set up and am gradually picking away at the functionality step by step. The first thing to do is find and retrieve a Lookup record and put that onto the relevant field on my VFpage.

Essentially the SoQL isn't complex as I am just testing it (hence the hard coded date):

Select Name From Timesheet__c WHERE Week_Beginning__c = 2013-03-25

Layout of my VFpage for reference:

<apex:page standardController="Call__c"> 
<apex:sectionHeader title="Call__c Edit" subtitle="{!Call__c.name}"/> 
<apex:form > 
<apex:pageBlock title="Call__c Edit" mode="edit">

<!--buttons for the VFpage at the top-->

I will want to put "Add Timesheet And Save Buttons in here"

<apex:pageBlockButtons location="top">
<apex:commandButton value="Save" action="{!save}"/> 
<apex:commandButton value="Save & New" action="{!save}" /> 
<apex:commandButton value="Cancel" action="{!cancel}"/> 
</apex:pageBlockButtons>

<!--buttons for the VFpage at the bottom-->

<apex:pageBlockButtons location="bottom"> 
<apex:commandButton value="Save" action="{!save}"/> 
<apex:commandButton value="Save & New" action="{!save}" /> 
<apex:commandButton value="Cancel" action="{!cancel}"/> 
</apex:pageBlockButtons>



<apex:pageBlockSection title="Information" columns="2"> 
<apex:inputField value="{!Call__c.Account__c}" required="true"/> 

This is where the Timesheet Lookup result is to be returned to

<apex:inputField value="{!Call__c.Timesheet__c}" required="true"/>

<apex:inputField value="{!Call__c.Contact__c}" required="false"/> 
<apex:inputField value="{!Call__c.RecordTypeId}" required="false"/> 
<apex:inputField value="{!Call__c.Call_Date__c}" required="false"/> 
<apex:inputField value="{!Call__c.Call_Category__c}" required="false"/> 
<apex:inputField value="{!Call__c.Call_Notes__c}" required="false"/> 
</apex:pageBlockSection>

</apex:pageBlock>
</apex:form>
</apex:page>

Learning to walk a bit first. So focussing on just getting the result first before attempting to do everything in one fell swoop. First step: get that SoQL linking the record to Timesheet (& remove hardcoded dates), Second: popping a create Timesheet window if there is no result with the SoQL, Third: saving the VFpage to the Call object. And then tidy up with some validation error handling.

EDIT:

I have put this controller in here:

public with sharing class TimesheetFindSOQL {

    public TimesheetFindSOQL(ApexPages.StandardController controller) {

    }

//instance variable
public Timesheet__c record {get; set;}

//class constructor
public TimesheetFindSOQL() {
  List<Timesheet__c> records = [Select Id, Name From Timesheet__c WHERE Week_Beginning__c = 2013-03-25];
  if(!records.isEmpty()) {
    record = records.get(0);
        }
    record = records.get(1);

    }
}

I am expecting to put something a bit like:

<apex:commandButton value="Add Timesheet" action="{!NO_IDEA_WHAT_GOES_HERE" /> 

in here now. But I am clueless about the proper way to do it. Fumbling around has got me nowhere so far.

1 Answer 1

1

There is no lookup field selected in your SOQL query, only a text field, Name. However, assuming you have queried for a lookup field, you reference it the same way you reference any field in visualforce.

If I had some apex:

//instance variable
public Timesheet__c record {get; set;}

//class constructor
public MyClass() {
  List<Timesheet__c> records = [Select Id, Name, My_Lookup__c From Timesheet__c WHERE Week_Beginning__c = 2013-03-25];
  if(!records.isEmpty()) {
    record = records.get(0);
  }
}

Then the visualforce to display the lookup field is just:

<apex:inputField value="{!record.My_Lookup__c}" />

See

http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_inputField.htm

Edit

I'm reading this again, and it seems perhaps you want a lookup field to the record you queried for, as opposed to displaying a lookup field which exists on the record you queried for. If this is the case, you need to have a field which is of type Lookup(Timesheet) and bind to that field in your visualforce.

To populate that field with the result of your query.

//sobjectInstance is an sobject with a lookup to the timesheet sobject
//record is the results of the soql above.
sobjectInstance.Lookup_Timesheet__c = record.Id;

More Edits

Editing your code so it does what you want.

APEX:

public with sharing class TimesheetFindSOQL {

    //instance variable
    public Timesheet__c record {get; set;}

    public TimesheetFindSOQL(ApexPages.StandardController controller) {
        record = (Timesheet__c) controller.getRecord();

        List<Timesheet__c> records = [Select Id, Name From Timesheet__c WHERE Week_Beginning__c = 2013-03-25];

        if(!records.isEmpty()) {
            record.MyTimesheetLookup__c = records.get(0).MyTimesheetLookup__c;
        }
    }

    /*This constructor is not getting called, its using the constructor above instead.
    public TimesheetFindSOQL() {}*/
}

VISUALFORCE:

<apex:page standardController="Timesheet__c" extensions="TimesheetFindSOQL">
...
<apex:inputField value="{!record.MyTimesheetLookup__c}" />

If you can't get it working at this point, I would suggest taking a look at: http://wiki.developerforce.com/page/An_Introduction_to_Visualforce

8
  • I'm just about to disappear for a couple of days but I'll jump back in next week to look at what you have said. You are correct, I am essentially trying to populate a lookup via the result of the SoQL. Essentially the scenario is this: I have a "call recording" VF page with fields on (mimicking a Call object), one is a lookup to a timesheet object which the Call is to attach to. I want to use an "Attach Timesheet" button to populate the lookup based on the date entered (and then my next step will be to pop a create Timesheet screen if it isn't there) and then save back to the Call object.
    – 44f
    Apr 5, 2013 at 14:48
  • Ok. I have the initial code in my Class now, and that looks fine, no errors.
    – 44f
    Apr 10, 2013 at 12:42
  • I'm trying to figure out what to do with: //sobjectInstance is an sobject with a lookup to the timesheet sobject //record is the results of the soql above. sobjectInstance.Lookup_Timesheet__c = record.Id; what I am trying to do for the time being is return the SoQL result back to my VFpage. So the lookup there which is auto-populated with the result, ready for the user to save.
    – 44f
    Apr 10, 2013 at 13:28
  • Right, but you cant bind a field in visualforce to a query result, you have to bind it to a field which is a lookup to the timesheet table.
    – Phil Rymek
    Apr 10, 2013 at 18:58
  • Ok, the field I want it to go to is a lookup to the timesheet table. (i.e. field "lookup" type that points at the Timesheet object). I have stuck an edit in to show my latest posistion. (thanks for the help so far!)
    – 44f
    Apr 15, 2013 at 13:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .