This example shows how to create a job application form for your sites that search dynamically from your salesforce and apply for specific position.
global class jobApplicationExCtrl {
private final Candidate__c cand;
public string resumeText {Get; Set;}
public String myPosition_Id {Get; Set;}
public jobApplicationExCtrl(ApexPages.StandardController stdController) {
this.cand = (Candidate__c)stdController.getRecord();
}
public PageReference Save()
{
System.debug('--------------------- jobApplicationExCtrl Save BEGIN');
PageReference ret;
try{
insert cand;
System.debug('--------------------- jobApplicationExCtrl cand Id = ' + cand.Id);
} catch (System.DmlException e) {
System.debug('--------------------- jobApplicationExCtrl save Dml Exception = ' + e.getMessage());
}
if (resumeText != null && resumeText != '')
{
System.debug('--------------------- jobApplicationExCtrl resumeText = ' + resumeText);
Blob resumeBlob = Blob.valueOf(resumeText);
Attachment att = new Attachment(parentId = cand.Id, name='Resume.txt', body = resumeBlob);
try{
insert att;
System.debug('--------------------- jobApplicationExCtrl att Id = ' + att.Id);
} catch (System.DmlException e) {
System.debug('--------------------- jobApplicationExCtrl save Dml Exception = ' + e.getMessage());
}
}
PageReference candPage = new ApexPages.StandardController(cand).view();
candPage.setRedirect(true);
ret = candPage;
System.debug('--------------------- jobApplicationExCtrl Save END');
return ret;
}
public PageReference Apply()
{
System.debug('--------------------- jobApplicationExCtrl Apply BEGIN');
PageReference ret;
if (myPosition_Id != null && myPosition_Id != '')
{
System.debug('--------------------- jobApplicationExCtrl myPosition_Id = ' + myPosition_Id);
try{
insert cand;
System.debug('--------------------- jobApplicationExCtrl cand Id = ' + cand.Id);
} catch (System.DmlException e) {
System.debug('--------------------- jobApplicationExCtrl Apply Dml Exception = ' + e.getMessage());
}
if (resumeText != null && resumeText != '')
{
System.debug('--------------------- jobApplicationExCtrl resumeText = ' + resumeText);
Blob resumeBlob = Blob.valueOf(resumeText);
Attachment att = new Attachment(parentId = cand.Id, name='Resume.txt', body = resumeBlob);
try{
insert att;
System.debug('--------------------- jobApplicationExCtrl att Id = ' + att.Id);
} catch (System.DmlException e) {
System.debug('--------------------- jobApplicationExCtrl Apply Dml Exception = ' + e.getMessage());
}
}
Job_Application__c app = new Job_Application__c();
app.Position__c = myPosition_Id;
app.Candidate__c = cand.Id;
try{
insert app;
} catch (System.DmlException e) {
System.debug('--------------------- jobApplicationExCtrl Apply Dml Exception = ' + e.getMessage());
}
System.debug('--------------------- jobApplicationExCtrl Apply app.Id = ' + app.Id);
PageReference candPage = new ApexPages.StandardController(cand).view();
candPage.setRedirect(true);
ret = candPage;
}
else
{
System.debug('--------------------- jobApplicationExCtrl No Position Selected');
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'Please search for a position.');
ApexPages.addMessage(myMsg);
ret = null;
}
System.debug('--------------------- jobApplicationExCtrl Apply END');
return ret;
}
@RemoteAction global static Position__c[] findPositions(String Name)
{
System.debug('--------------------- jobApplicationExCtrl findPositions BEGIN');
String queryName = '%'+ String.escapeSingleQuotes(Name)+'%';
Position__c[] p = [Select p.Type__c, p.Name, p.Id, p.Department__c From Position__c p where p.Name LIKE :queryName Limit 10];
System.debug('--------------------- jobApplicationExCtrl findPositions END');
return p;
}
}
and the page looks like this <apex:page standardController="Candidate__c" extensions="jobApplicationExCtrl" sidebar="false" showHeader="false" standardStylesheets="false"> <apex:stylesheet value="{!URLFOR($Resource.UC_SiteBranding, 'style.css')}"/> <script type="text/javascript"> var positions; function positionsSearch(name) { jobApplicationExCtrl.findPositions(name,handlePositions); return false; } function handlePositions(result, event) { if(event.type == 'exception') { alert(event.message); } else { positions = result; showPositions(); } } function showPositions() { var newList = ""; for(var i = 0; i < positions.length; i++) { newList += "<input type=\"radio\" onclick=\"javascript: setPositionId('" +positions[i].Id+"');\" id=\"position\" name=\"position\" value=\"" +positions[i].Id+"\" /> "+positions[i].Name+ " <br />" } document.getElementById('positionsList').innerHTML = newList; } function setPositionId (posStr) { var posText = getApexElementByID("input","hiddenPositionId"); posText.value = posStr; } function getApexElementByID(tagname, eid) { var tags = document.getElementsByTagName(tagname); var regex = new RegExp(":" + eid + "$"); for (var i=0; i < tags.length; i++) { if (tags[i].getAttribute("id") != null) { var pos = tags[i].getAttribute("id").search(regex); if (pos != -1) return tags[i]; } } return null; } </script> <apex:outputPanel id="header" layout="block"> <apex:outputPanel id="logo" layout="block"> <h1><apex:outputLink value="#">Universal Containers</apex:outputLink></h1> <h2><apex:outputLink value="http://www.salesforce.com/">A Great Place to work</apex:outputLink></h2> </apex:outputPanel> <apex:outputPanel id="menu" layout="block"> <ul> <li><apex:outputLink value="#">Home</apex:outputLink></li> <li><apex:outputLink value="#">About Us</apex:outputLink></li> <li><apex:outputLink value="#">Benefits</apex:outputLink></li> <li><apex:outputLink value="#">Positions</apex:outputLink></li> <li><apex:outputLink value="#">Apply</apex:outputLink></li> </ul> </apex:outputPanel> </apex:outputPanel> <apex:outputPanel id="splash" layout="block"></apex:outputPanel> <apex:form id="jobapp"> <apex:outputPanel id="page" layout="block"> <apex:outputPanel id="content"> <h2>Apply</h2> <apex:pageMessages /> <fieldset> <legend>1) Search for a Position</legend> <apex:outputPanel > <!-- TODO (EXERCISE 3-1) Add Text and Button input HTML tags. --> <!-- The Text input should have an id of searchTerm --> <!-- The Button input should have a value of search --> <!-- onclick call the positionsSearch javaScript function with the value of searchTerm --> <input type="text" id="searchTerm" /> <input type="button" onclick="return positionsSearch(document.getElementById('searchTerm').value);" value="Search" /> <div id="positionsList"></div> <div style="float: right;"> </div> </apex:outputPanel> <apex:inputHidden value="{!myPosition_Id}" id="hiddenPositionId"/> </fieldset> <fieldset> <legend>Contact Information</legend> <apex:outputPanel layout="block"> <apex:outputLabel value="First Name:" for="fName"/> <apex:inputText id="fName" value="{!Candidate__c.First_Name__c}" /> </apex:outputPanel> <apex:outputPanel layout="block"> <apex:outputLabel value="Last Name:" for="lName"/> <apex:inputText id="lName" value="{!Candidate__c.Last_Name__c}" /> </apex:outputPanel> <apex:outputPanel layout="block"> <apex:outputLabel value="Email:" for="email"/> <apex:inputText id="email" value="{!Candidate__c.Email__c}" /> </apex:outputPanel> <apex:outputPanel layout="block"> <apex:outputLabel value="Phone:" for="phone"/> <apex:inputText id="phone" value="{!Candidate__c.Phone__c}" /> </apex:outputPanel> </fieldset> <fieldset> <legend>3) Cut & Paste Resume</legend> <!-- TODO (EXERCISE 3-1) add an Apex Input Text Area tag that is tied to the action method--> <!-- Override the style to set the width to 473px and height to 250px--> <apex:inputTextarea id="resume" value="{!resumeText}" style="width: 473px; height: 250px;"/> </fieldset> <!-- TODO (EXERCISE 3-1) change the commandButton to call the Apply method in the extension controller--> <apex:commandButton value="Apply" action="{!Apply}"/> </apex:outputPanel> <apex:outputPanel id="sidebar"> <h2>Universal Containers is Hiring!</h2> <p>Universal Containers (UC) is is a global company with more than 4,750 employees in offices around the world.</p> <p>We are an Equal Employment Opportunity and Affirmative Action Employer.</p> <p><strong>Accessibility</strong> – If you require accessibility assistance applying for open positions please contact the <a href="mailto:applications@universalcontainers.com">Recruiting Department</a>.</p> </apex:outputPanel> </apex:outputPanel> <apex:outputPanel id="footer" layout="block"> <p id="legal">Copyright © 2007 BreakFast. All Rights Reserved. Designed by <apex:outputLink value="http://www.freecsstemplates.org/">Free CSS Templates</apex:outputLink>.</p> <p id="links"><apex:outputLink value="#">Privacy Policy</apex:outputLink> | <apex:outputLink value="#">Terms of Use</apex:outputLink></p> </apex:outputPanel> </apex:form> </apex:page>
No comments:
Post a Comment