+ Start a Discussion
joestuartjoestuart 

Update custom object field with Visualforce page form

Hi,
I have stripped my code back to the bare minumum as I am having lots of trouble trying to make a form with one field.  The field needs to submit the string "submitted" when the user hits the submit button.

My Error in the VFP
<blockquote>Unknown property 'leadershipReadyCon.Leadership_Ready__c'</blockquote>

My APEX Class code
public with sharing class leadershipReadyCon {

	 public final Leadership_Ready__c myLeadershipReady;
    
	//Constructor function
    public leadershipReadyCon() {
        myLeadershipReady = [SELECT Id, Automated_TP_Status__c FROM Leadership_Ready__c 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('prodId')];
    }
    
    Leadership_Ready__c lr = new Leadership_Ready__c();
    
    

    public Leadership_Ready__c getLeadershipReady() {
        return myLeadershipReady;
    }

    public PageReference save() {
        //Add your custom logic to update specific fields here 
        //e.g. myLeadershipReady.email = 'xx@xx.com'; 
        update myLeadershipReady;
        return null;
    }        
}

Here is my Visual Force Page code 
<apex:page controller="leadershipReadyCon" tabStyle="Leadership_Ready__c">
    
    <apex:form >
        
        <apex:pageBlock title="Automated Training Plan Status">
            You belong to Account Name: <apex:inputField value="{!Leadership_Ready__c.Automated_TP_Status__c}"/>
            <apex:commandButton action="{!save}" value="save"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Any help would be great, I don't know if this is the best way to submit data to a field in SFDC with APEX so if I am totaly on the wrong track, please let me know.

Thanks.
KapilCKapilC
Hi Joe,

There are some modifications needed in your code. Please find the code below.
 
public with sharing class leadershipReadyCon {

  // After declaring final you can not modify any record.	 
  //public final Leadership_Ready__c myLeadershipReady;
    
// getter and setter required to access and update record field from page.
    public Leadership_Ready__c myLeadershipReady {get;set;}
   
    
	//Constructor function
    public leadershipReadyCon() {
       //Initialize  first to avoid null error.
        Leadership_Ready__c myLeadershipReady  = new Leadership_Ready__c();
        
        // if there is any record found then it will assign to myLeadershipReady otherwise we have already Initialize it.
         for(Leadership_Ready__c  lr : [SELECT Id, Automated_TP_Status__c FROM Leadership_Ready__c  WHERE Id = :ApexPages.currentPage().getParameters().get('prodId')]){
             myLeadershipReady =  lr;
    }
    

    public PageReference save() {
        //Add your custom logic to update specific fields here 
        //e.g. myLeadershipReady.email = 'xx@xx.com'; 
        update myLeadershipReady;
        return null;
    }        
}

<apex:page controller="leadershipReadyCon" tabStyle="Leadership_Ready__c">
    
    <apex:form >
        
        <apex:pageBlock title="Automated Training Plan Status">
           <!-- You belong to Account Name: <apex:inputField value="{!Leadership_Ready__c.Automated_TP_Status__c}"/>
                 Always you your variable not the object as you can see in code below.
               -->
             You belong to Account Name: <apex:inputField value="{!myLeadershipReady.Automated_TP_Status__c}"
             
            <apex:commandButton action="{!save}" value="save"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>
[If you got answer from my post please mark it as solution.]


Thanks,
Kapil
Email (mailto:forcecube@gmail.com)


 
joestuartjoestuart
Thanks for your help Kapil, I appricate the comments in the code.  Very helpful.

I am getting 4 different errors with the new code.  I have added them in the code as comments starting with //error at the related line.
 
public with sharing class leadershipReadyCon {

  // After declaring final you can not modify any record.	 
  //public final Leadership_Ready__c myLeadershipReady;
    
// getter and setter required to access and update record field from page.
    public Leadership_Ready__c myLeadershipReady {get;set;}
   
    
	//Constructor function
    public leadershipReadyCon() {
       //Initialize  first to avoid null error.
        Leadership_Ready__c myLeadershipReady  = new Leadership_Ready__c();
        
        // if there is any record found then it will assign to myLeadershipReady otherwise we have already Initialize it.
         for(Leadership_Ready__c  lr : [SELECT Id, Automated_TP_Status__c FROM Leadership_Ready__c  WHERE Id = :ApexPages.currentPage().getParameters().get('prodId')]){
             myLeadershipReady =  lr;
    }
    
	//error unexpected token: 'public'
	//error unexpected syntax: 'missing SEMICOLON at 'save''
	//error unexpected syntax: 'missing SEMICOLON at '{''
        public PageReference save(){
        //Add your custom logic to update specific fields here 
        //e.g. myLeadershipReady.email = 'xx@xx.com'; 
        update myLeadershipReady;
        return null;
    }        
}
//Error unexpected syntax: 'mismatched input '<EOF>' expecting RCURLY'

Thanks Kapil
joestuartjoestuart
Ok so the issue was it was missing a curly braket down the bottom.  I fixed that so the code compiled and now when I hit the save button I get this error:

System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!save}' in component <apex:commandButton> in page inputfieldhelloworld: Class.leadershipReadyCon.save: line 28, column 1
Class.leadershipReadyCon.save: line 28, column 1


Do I need to return something? 
KapilCKapilC
HI Joe,

Sorry for the late reply. I think you are not sending recordid while opening your page. Make sure your page url look like "https://c.ap4.visual.force.com/apex/yourpageName?id=0036F00002D4xt8"

https://c.ap4.visual.force.com ==> your org baseurl
id= YourRecordId

Regards,
Kapil
KapilCKapilC
public PageReference save(){
        update myLeadershipReady;
        return new PageReference ('/'+myLeadershipReady.id);
    }

If you want to redirect on record's detail view.