The heart of the public recruiting application is the job postings page. Here, Universal Containers can list all of their available positions, and visitors can search for positions. Later, you will add a job detail page where users can view more information, and a job application page, where users can submit applications—but first, create the job postings page.
This topic covers the following steps:
Note
You learned about Apex and Visualforce in earlier chapters; these examples will build on that knowledge. |
Before building the Visualforce page, create the custom Apex controller. This controller, named PublicJobSearchController, extends the standard Visualforce functionality by adding custom search capabilities on the Position custom object, allowing visitors to your Force Platform site to search by department, geographic location, and keyword. The controller also determines the content of the query results.
Note
You can find the completed controllers and pages in the Recruiting Application by navigating to Setup Develop Pages. |
Define the Department and Location fields as lists, and the Keyword field as a string. Display the ID, Name, Department, City, State, Country, Job Description, Position Open Date, and Status fields to the user.
Your Apex controller should look like this:
public class PublicJobSearchController { Position__c[] ccts; String searchPosition; String positionid; String DepartmentName = '%'; String LocationName = '%'; String likeStr = '%'; private String viewDepartmentName = 'Department'; private String viewLocationName = 'Locations'; // Department picklist for the search page private static final List<SelectOption> VIEW_DEPARTMENT = new SelectOption[] { new SelectOption('0','All Departments'), new SelectOption('Engineering','Engineering'), new SelectOption('Finance','Finance'), new SelectOption('IT','IT'), new SelectOption('Sales','Sales'), new SelectOption('Support','Support') }; // Location picklist for search page private static final List<SelectOption> VIEW_LOCATION = new SelectOption[] { new SelectOption('0','All Locations'), new SelectOption('San Francisco','San Francisco'), new SelectOption('New York','New York'), new SelectOption('Reston','Reston'), new SelectOption('Toronto','Toronto') }; public List<SelectOption> getDepartmentViewNames() { return VIEW_DEPARTMENT; } public List<SelectOption> getLocationViewNames() { return VIEW_LOCATION; } public void setViewDepartmentName(String viewDepartmentName) { this.viewDepartmentName = viewDepartmentName; } public String getViewDepartmentName() { return viewDepartmentName; } public void setViewLocationName(String viewLocationName) { this.viewLocationName = viewLocationName; } public String getViewLocationName() { return viewLocationName; } // Page onLoad action to auto-run the job postings query public void initList() { query(); } public String getSearchPosition() { return this.searchPosition; } public void setSearchPosition(String search) { this.searchPosition = search; } public void populateQuery() { likeStr = '%' + searchPosition + '%'; if (viewDepartmentName != '0') DepartmentName = viewDepartmentName; if (viewLocationName != '0') LocationName = viewLocationName; query(); } // Query to return the first 100 matching positions public PageReference query() { ccts = [SELECT id, name, Department__c, Position_Type__r.Name, Location__r.City__c, Location_City_State_Country__c, Job_Description__c, Open_Position_Date__c, Status__c FROM Position__c WHERE Department__c like :DepartmentName AND Location__r.City__c like :LocationName AND name like :likeStr ORDER BY Name ASC LIMIT 100]; return null; } public Position__c[] getPosition() { return ccts; } }
Now create a new Visualforce page, named PublicJobs, that uses the PublicJobSearchController custom controller that you just created. This page displays all available positions and allows users to search for available jobs based on department, location, and keyword. Your Visualforce page should look like this:
<apex:page showheader="false" action="{!initList}" controller="PublicJobSearchController" standardStylesheets="true"> <apex:form> <!--Search by Department, Location, or Keyword--> <apex:pageBlock title="Search Job Postings"> <apex:pageBlockSection columns="1"> <apex:pageBlockSectionItem> <apex:outputText value="Department"/> <apex:selectList value="{!viewDepartmentName}" id="departmentViews" size="1" required="true"> <apex:selectOptions value="{!DepartmentViewNames}"/> </apex:selectList> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem> <apex:outputText value="Location"/> <apex:selectList value="{!viewLocationName}" id="locationViews" size="1" required="true"> <apex:selectOptions value="{!LocationViewNames}"/> </apex:selectList> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputText value="Keyword"/> <apex:inputText value="{!searchPosition}"/> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputText value=""/> <apex:commandButton value="Search" action="{!PopulateQuery}" reRender="JobList"/> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:form> <!-- Search results--> <apex:pageBlock title="Search Results"> <apex:pageBlockTable id="JobList" value="{!Position}" var="Position__c" rendered="{!NOT(ISNULL(Position))}"> <apex:column> <apex:facet name="header"> <apex:outputText value="Position ID"/> </apex:facet> <!-- Position name field linked to job details page --> <apex:outputText value="{!Position__c.name}"/> </apex:column> <apex:column> <apex:facet name="header"> <apex:outputText value="Title"/> </apex:facet> <apex:outputText value= "{! Position__c.Position_Type__r.Name}"/> </apex:column> <apex:column> <apex:facet name="header"> <apex:outputText value="Department"/> </apex:facet> <apex:outputText value="{!Position__c.Department__c}"/> </apex:column> <apex:column> <apex:facet name="header"> <apex:outputText value="Location"/> </apex:facet> <apex:outputText value= "{!Position__c.Location_City_State_Country_c}"/> </apex:column> <apex:column> <apex:facet name="header"> <apex:outputText value="Job Description"/> </apex:facet> <apex:outputText value="{!Position__c.Job_Description__c}"/> </apex:column> <apex:column> <apex:facet name="header"> <apex:outputText value="Position Open Date"/> </apex:facet> <apex:outputText value= "{!Position__c.Open_Position_Date__c}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlock> </apex:page>
Now that your Visualforce page is ready, go back to the application and enable the page for your site by doing the following:
1. | Click Setup
Develop
Sites. |
2. | Click the site label link for your site. This takes you to the Site Details page. |
3. | Click Edit on the Site Pages related list. |
4. | Add the PublicJobs page to the Enabled Visualforce Pages list. |
5. | Click Save. |
The job postings that website users see are object records, so you have to give your site’s users permission to view the Position object by doing the following:
1. | From the Site Details page, click Public Access Settings. |
2. | Click Edit. |
3. | Under Custom Object Permissions, enable “Read” permission on Positions. The custom object permissions for this example should match the following figure. |
4. | Click Save. |
You have created the Apex controller and Visualforce pages, enabled the PublicJobs page, and granted public users permission to view Positions. Now all that’s left to do is to make the PublicJobs page the home page for your active site:
1. | |
2. | Click Edit. |
3. | Use the lookup field to find and select PublicJobs for the Active Site Home Page field. |
4. | Click Save. |
Browse to your site’s URL to see your new Visualforce page in action.
3.145.97.170