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
mxalix258mxalix258 

Very simple visualforce form and custom controller not working

I'm trying to create a very simple visualforce form, that just doesn't seem to be working and I don't know why

 

VF:

<apex:page controller="SubmitReferralController" standardStylesheets="false" showHeader="false" sidebar="false">
    <apex:stylesheet value="{!$Resource.semantic}"/>
<html lang="en">
<head>
</head>
<body>
<div style="margin-left:200px">    
<h1>Referrals Fee Program</h1>
<p>
  Any questions? Contact the <a href="semantic-form.css">Referral team</a>.
</p>
</div>
<apex:form styleClass="semantic"> 
<apex:pageBlock >
<div style="margin-left:200px">
<apex:outputPanel >
    <div>
      <apex:outputlabel Value="First Name"></apex:outputlabel>
      <apex:inputText value="{!c.FirstName}" size="30" id="referrer_first_name"/>
      <apex:outputlabel Value="Last Name"></apex:outputlabel>
      <apex:inputText value="{!c.LastName}" size="30" id="referrer_last_name"/>
     </div>
<div style="margin-left:200px">
<apex:commandButton value="Submit Referral" action="{!submitReferral}"/>
</div>
</apex:outputPanel>
</div>
</apex:pageBlock>
</apex:form>         
</body>
</html>
</apex:page>

 Controller:

public class SubmitReferralController {
    
    public Contact c { get; set; }
    
    public PageReference submitReferral(){
        try {
        insert c;
       }
       catch(System.DMLException e) {
           ApexPages.addMessages(e);
       return null;
       }
       return null;
     }
}

 

I'm sure it's something really stupid, but I just can't sort it out.

 

Any help would be greatly appreciated!

Rahul_sgRahul_sg
you are missing a constructor...contact C is never initialized ..put some debug statement you will see the error.
mxalix258mxalix258

What would that look like? Sorry, I'm still learning!

b.ajitkumarb.ajitkumar

I would make changes to the Custom controller as shown below:

 

public class SubmitReferralController {
    public Contact c { get; set; }
    
    public SubmitReferralController () {
        
        Id id = ApexPages.currentPage().getParameters().get('id');
        c = (id == null) ? new Contact() : 
            [SELECT FirstName, LastName FROM Contact WHERE Id = :id];
    }
    
    public PageReference submitReferral(){
        try {
        upsert c;
       }
       catch(System.DMLException e) {
           ApexPages.addMessages(e);
       return null;
       }
       return null;
     }
}

 I have added the constructor and modified the insert to upsert (both underlined above)

 

You can refer the following links for more information:

http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_custom.htm

Developers Guide:

http://www.salesforce.com/us/developer/docs/pages/salesforce_pages_developers_guide.pdf

VisualForce Workbook:

http://www.salesforce.com/us/developer/docs/workbook_vf/workbook_vf.pdf

 

Aviator517Aviator517
I intend for this to put an external form, that isn't inside our Salesforce. As such, I'll want to find the contact based on the inputted fields, and if no match is found to create a new contact. Could I do this by removing get the ID, and modify the SOQL query to query based on values inputted in the form?