How to check for existing related record using Sites Page with user registration

Hello - I am trying to figure out a way to check if a custom object record already exists or not when a Sites user either registers or logs into the Sites Page and attempts to create a new record (in this case Job Application). The Custom Object is Candidate, and I want to accompish two things:

1) Check for an existing Candidate record in force.com based on the email address prompted for in the Job Application form (or perhaps the Username in the registration form); or

2) Allow a registered user to login and create a new Job Application record in force.com using their existing Candidate record (if they already applied for another position).

Currently a registered user can apply for a job position. However, if the same user comes back to the site, logs in and applies for another position then a new Candidate record is created. I would like to check if a Candidate record already exists and return all of the existing Candidate fields so as to avoid duplicate records and unnecessary data input.

Thanks for the response, John. That is an interesting capability, but I don't think it will help me get to the end result I'm trying to achieve. I probably didn't do a very good job of explaining what I'm trying to do. Let me give it another shot.The goal is to allow a registered Job Candidate User who has already applied for a job previously to apply for another job without having to reenter their candidate information (and thus avoid creating a duplicate candidate record in the Candidate__c object). Records are created in the User, Account and Contacts objects as part of the Sites User Registration process when the Job Candidate registers to the Site for the first time. Once registered, the Job Candidate can apply for a Job Position that they select from a list of Job Search results by clicking on the Position ID for a record in the list which takes them to the Job Detail (VF Page: PublicJobDetails) which has an "Apply" button which takes them to the Job Application form (VF Page: PublicJobApplication). The Job Candidate User fills out the form and clicks on the "Save" button to submit the job application. This action creates both a new Job Application record (in the Job_Application__c object) and a candidate record (in the Candidate__c object). This works fine for a Job Candidate User who registers and only submits a job application for 1 job. However, if the Job Candidate User returns to the site or elects to submit a job application for another job, then a new candidate record is created. I'm trying to understand how to avoid the creation of a new candidate record (which is in effect a duplicate).My thinking is that the email for the candidate can be used to check for an existing candidate record (or Contact record... not sure which makes the most sense) and then return the existing candidate information wherein the Job Candidate User could update the information, but not have to completely re-enter it. Sorry for the long winded explanation. I hope this more clearly explains my issue.Thanks,

I'm still a bit fuzzy how the flow works, but it sounds like you know the email address but not the applicant ID when the user logs in?

If that's accurate, you should be able to query by email address (assuming you enforce uniqueness on email), return the ID, put the ID in the URL, and have conditional code such as my encryption example to redirect the page if there's an ID in the URL.

New person:

Deafult page = new edit page & page does not reload

Existing person:

Page redirects upon load & they see detail page with their information

Hello - I tried to follow your code example. I'm getting stuck on the PageReference. I'm just not sure how to set this up so that it will do what I want it to do. I have scoured the dev force site and found nothing that seems to help. My query seems to work correctly, but I think that I am setting up the PageReference incorrectly.

The jobid "01p80000000BwAt" is the correct positionId from the PublicJobDetails Sites Page.

This renders the following error on the PublicJobApplication Sites Page:

Error: Error occurred while loading a Visualforce page.

I'm sure that I have done this entirely wrong (but I seem to be getting closer). Any help would be greatly appreciated. Learning this stuff is painful without a programming background (but Apex, VF and Sites are awesome DaaS technology and it is all starting to make sense).

John - I really apprecaite all of your suiggestions. I'm still unable to get this to work. I made the changes to the URL string variable per your reply and I used the syntax you suggested to test the controller, and got the following message using the Execute Apex function in the System Log window:

The VF Page redirects to an error message Page (noted in previous message). The only clue to this problem that I can see is that the URL that you are redirected to uses "jobid?" to reference the positionId rather than "Id?" which tells me that the method is not being invoked properly (or at all), otherwise the URL would contain "Id?" in it instead. I would like to think that I am close to solving this, but I am stuck. Any help is really appreciated.

You are an extremely patient person (approaching Saint status). I really do appreciate all of your assistance.

The quick answer to your 1st question is that I see the failure when I add the action attribute on the PublicJobApplication VF Page to the Apex Page Tag that points to the checkExistingCandidate method in the PublicJobApplicationExtension Apex class. I will do my best to answer your questions in a more detailed manner.

I have tested the actual VF Sites Page a couple of ways:

1) Insert the Candidate. Remove the action="{!checkExistingCandidate}" attirbute on the Page Tag and save a Job Application record (which also creates a Candidate record). Once removed (and the PublicJobApplication VF Page is saved) I am able to access the PublicJobApplication Page by clicking on the "Apply" button in the PublicJobDetails Page, which uses the OnClick attribute to redirect to browser to the PublicJobApplication Site Page which references the

{!$Site.Prefix}/PublicJobApplication?jobid={!Position__c.Id}.

Then I'm able to fill in the information and save the record by calling the saveApplication method by clicking on the "Save" button (commandButton tag in VF markup) in the PublicJobApplication Page.

2) Try to call the existing Candidate. Reinsert the action="{!checkExistingCandidate}" attirbute on the Page Tag in the PublicJobApplication Page. Login as the user (who is now also has a Candidate record), select a job poisiton to apply for and click on the apply button... I get the error.

The App flows I'm trying to achieve:

A) Registered User who already has a Candidate record in force.com and searchs jobs first

Looks like you're reusing the string CandidateID trying to again cast it as an ID 4 lines down. Make then 2 different variables and you should be ok. Also, I'm guessing you only wnat to use 1 of ID or the email address to match, so I put in the check if candidateID is null before querying for candidates

I unable to get the code for the checkExistingCandidate method (as you've written it below) to work. I running into two problems, and I am uncertain of the logical flow of the method as you've coded it.

Problem 1: The candidateIdFrom EmailAddress variable fails to compile. I get the error "Compile Error: Type cannot be constructed: ID at line 27 column 39". My understanding is that this is coded as a variable that I'm declaring of type ID, not a method. I can fix this by simply declaring the variable and not setting its value, but I think that undermines the logic of what you've suggested.

Problem 2: In the test "if (candidates.size()!=0 && CandidateId!=null)" I get an error "Compile Error: Method does not exist or incorrect signature: candidates.size() at line 32 column 11". This makes sense to me because there is no size parameter to reference. I believe that records need to be queried for a variable that is of type Array before you can reference the size() parameter for what is returned. I think I understand the intention of what you've suggested, but I don't understand how to get there (another SELECT statement for a candidates variable of type Array to return records before referencing the size() parameter?).

Uncertainty Explained: I do not understand why the if tests are testing for candidateId != null in both cases. It seems to drive the result to the same outcome. Is this intentional? What am I missing?

In an attempt to understand the logic of what your code example seems to suggest (to me), I rewrote the method in the following manner (differences highlighted in Red Bold):

To test this I logged in as an existing User with and existing Candidate record. The functionality still behaves as if you are entering a new Candidate into the Job Application form. In otherwords, I am still unable to get the existing Candidate record to be returned into the form for a Candiate that already exists who is applying for another job position. Its as if the action attribute is ignored (though I believe it is working per the logic).

[Note: The URL for the Job Application form (PublicJobApplication Page) has the "?jobid=" in it rather than the "?Id=" as specified by calling the method.