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
Jonathan Osgood 3Jonathan Osgood 3 

Having trouble relating records in my custom Controller

Hi All,

I have a custom form_c object which creates a new contact from the form_c.Name fields. Thats working fine, but I can't seem to link the two records via lookup. I'm trying to populate a lookup field to contact on my form object. form.Student__c;

Here is my controller so far:
public class MeritusSignupController {


 public Form__c form {get;set;}
 public String formId {get;set;}
 public Account a {get;set;}
 public Contact c {get;set;}
 public String is_new {get;set;}


 public MeritusSignupController()
    {
    
     form= new Form__c();
     form.Applicant_First_Name__c = UserInfo.getFirstName();
     form.Applicant_Last_Name__c = UserInfo.getLastName();
     form.Email__c = UserInfo.getUserEmail();
     a= new account();
     is_new = ApexPages.CurrentPage().getParameters().get('newform');
     formId = ApexPages.CurrentPage().getParameters().get('formid');
     String userId= UserInfo.getUserId();
     c= new contact();
      
     List<Form__c> formList = new List<form__c>();
        if (is_new == 'true')  
        {
            form = new form__c();
            form.Applicant_First_Name__c = UserInfo.getFirstName();
            form.Applicant_Last_Name__c = UserInfo.getLastName();
            form.Email__c = UserInfo.getUserEmail();
        }
    
        else if (formId != null)
        {
            formList = [Select id, Student__r.Name, Student__c, Read_understand_requirements__c, Applicant_First_Name__c,Applicant_Last_Name__c, 
            Address__c, Applicant_City__c,Applicant_State__c,Applicant_Zip_Code__c, Primary_Phone__c, Secondary_Phone__c, Secondary_Contact_First_Name__c, 
            Secondary_Contact_Last_Name__c,Relationship_to_Student__c,Secondary_Contact_Phone_Number__c,Gender__c,Country_of_Origin__c,Race_Ethnicity__c,Birthdate__c,
            Languages_Spoken__c,High_School_Attended__c,Planning_on_Completing_FAFSA_or_DreamAct__c,Did_either_or_both_of_your_parents_gradu__c, 
            Email__c from form__c where id =: formid];
        }
        
        else 
        {
            formList = [Select id, Student__r.Name, Student__c, Read_understand_requirements__c, Applicant_First_Name__c,Applicant_Last_Name__c, 
            Address__c, Applicant_City__c,Applicant_State__c,Applicant_Zip_Code__c, Primary_Phone__c, Secondary_Phone__c, Secondary_Contact_First_Name__c, 
            Secondary_Contact_Last_Name__c,Relationship_to_Student__c,Secondary_Contact_Phone_Number__c,Gender__c,Country_of_Origin__c,Race_Ethnicity__c,Birthdate__c,
            Languages_Spoken__c,High_School_Attended__c,Planning_on_Completing_FAFSA_or_DreamAct__c,Did_either_or_both_of_your_parents_gradu__c, 
            Email__c from form__c where CreatedById =: userId order by lastmodifieddate desc limit 1];
        
        }
        
         if (formlist.size() >0)
        {                
                form = formlist[0];
                if (form.applicant_first_name__c == null || form.applicant_first_name__c == '')
                    form.applicant_first_name__c = UserInfo.getFirstName();
                if (form.applicant_last_name__c == null || form.applicant_last_name__c == '')
                    form.applicant_last_name__c = UserInfo.getLastName();
                if (form.email__c == null || form.email__c == '')
                    form.email__c = UserInfo.getUserEmail();
                if (form.Student__c != null)
                {
                    String conId = form.Student__c;
                    List<Contact> conList = [select id from contact where id =: conId limit 1];   
                    if (conList.size() >0)
                        c= conList[0]; 
                 }
         }                  
             
        
    }
    
 void savefunc()
     {
     Boolean flag = false;
      if (form.Applicant_First_Name__c == null)  {
           form.Applicant_First_Name__c.addError('Required field.');
           flag = true;
          } 
        
                             
                                        
     if (flag == false)     
      {            
    
        c.FirstName = form.Applicant_First_Name__c;
        c.LastName= form.Applicant_Last_Name__c;
        c.id = form.Student__c;
        
        
        upsert form;        
        upsert c;
       } 
       
     } 
     public PageReference saveForm() {
        
        try
        {
        savefunc();
        }
        catch (Exception e)
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, e.getmessage()));
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, String.valueOf(e.getLineNumber())));
            
        }
        return null;
}

     
}

 
Best Answer chosen by Jonathan Osgood 3
Jonathan Osgood 3Jonathan Osgood 3
ok, I figured it out:

Added this line:
if (form.student__c  == null)
        {
            form.student__c = c.id;
            update form;
       }