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.