function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bovisbovis 

Creating custom object and populating lookup field in one go

Hi,

 

How do you create 2 custom objects using Visualforce and then associate one as a lookup field in the other? E.g. Lets use the recruiter application. I want someone to create themselves as a candidate and then apply for a job at the same time.

 

Here's the apex

<apex:page controller="ApplicationCreateController">
  <apex:sectionHeader title="Visualforce Example" subtitle="Create a Job Application"/>
  <apex:form >
    <apex:pageMessages /> <!-- this is where the error messages will appear -->
    <apex:pageBlock title="Candidate Info">
      <apex:pageBlockButtons >
        <apex:commandButton action="{!save}" value="Save"/>
      </apex:pageBlockButtons>
      <apex:pageBlockSection showHeader="false" columns="1">
        <apex:inputField value="{!candidate.First_Name__c}"  label="First Name"  />
        <apex:inputField value="{!candidate.Last_Name__c}"  label="Last Name" />
        <apex:inputField value="{!candidate.Email__c}"  label="Email"  />
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>

 

public with sharing class ApplicationCreateController {
 
  // the candidate record you are adding values to
  public Candidate__c candidate {
    get {
      if (candidate == null) candidate = new Candidate__c();
      return candidate;
    }
    set;
  }

  // the job application record you are adding values to
  public Job_Application__c jobapplication{
    get {
      if (jobapplication == null) jobapplication = new Job_Application__c();
      return jobapplication;
    }
    set;
  }
 
  public ApplicationCreateController() {
    // blank constructor
  }
 
  // save button is clicked
  public PageReference save() {
    try {
            insert candidate; // inserts the new record into the database
            insert jobapplication; // inserts the new record into the database            
            jobapplication.Candidate__c = candidate.Name; // THIS DOESNT SEEM TO SET THE CANDIDATE LOOKUP FIELD IN THE JOB APPLICATION   
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new candidate.'));
      return null;
    }
    // if successfully inserted new candidate, then displays the thank you page.
    return Page.Application_Create_Thankyou;
  }
}

 

The visualforce runs without error and I get a new candidate and job application object but the lookup reference between them is not set.

 

Any help would be very much appreciated?

 

Thanks

Bovis

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The lookup takes an ID, not a name, plus you haven't actually saved the record after making the change.  I've modified the save below to set the candidate id into the jobapplication lookup field before it is inserted - try that:

 

  // save button is clicked
  public PageReference save() {
    try {
            insert candidate; // inserts the new record into the database
            jobapplication.Candidate__c = candidate.id; 
            insert jobapplication; // inserts the new record into the database            
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new candidate.'));
      return null;
    }
    // if successfully inserted new candidate, then displays the thank you page.
    return Page.Application_Create_Thankyou;
  }
}

 

All Answers

bob_buzzardbob_buzzard

The lookup takes an ID, not a name, plus you haven't actually saved the record after making the change.  I've modified the save below to set the candidate id into the jobapplication lookup field before it is inserted - try that:

 

  // save button is clicked
  public PageReference save() {
    try {
            insert candidate; // inserts the new record into the database
            jobapplication.Candidate__c = candidate.id; 
            insert jobapplication; // inserts the new record into the database            
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new candidate.'));
      return null;
    }
    // if successfully inserted new candidate, then displays the thank you page.
    return Page.Application_Create_Thankyou;
  }
}

 

This was selected as the best answer
bovisbovis

Thank you. Very much appreciated