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
JvalleeJvallee 

Access Page Component w/ Apex

I'm still new to this, so hopefully I am using the right terms:
 
I have created a Controller Extention as an Apex class that will in effect override the !save of the standard controller.  What I was hoping to do is save a field to a related object in this method, which I have been able to do, but I need to get the value that I will write from an <apex:inputField> page component.  Is this possible? I can't seem to find a way to reference these components in Apex.
Code:
public class LeadTouchExtension{

   public final Lead_Touch__c leadTouch;
                
   //Create a new save action
   ApexPages.Action saveAction = new ApexPages.Action('{!LeadTouchSave( string )}');
   
   public LeadTouchExtension ( ApexPages.StandardController stdController )
   {
      this.leadTouch = (Lead_Touch__c)stdController.getRecord();
   }
     
   public void LeadTouchSave()
   {
      try{
         
         for( Lead relatedLead : [Select ID, Salutation, FirstName, LastName FROM lead WHERE Lead.ID = :leadTouch.Lead__c LIMIT 1 ] )
         {
            //this needs to reference the Form and any pageBlock (maybe page block section) by name.                
            relatedLead.FirstName = ***Need to reference page component here!***  
         
            update relatedLead;
        
         }
      
      }
      catch( DmlException ex )
      {
         ApexPages.addMessages(ex);
      }

      
   }

}

 
jeremy_rossjeremy_ross
Are you familiar with binding component values to controller properties?  If not, take a look at http://www.salesforce.com/us/developer/docs/pages/index_CSH.htm#pages_quick_start_input_components.htm

Jeremy
JvalleeJvallee

Jeremy, thanks so much for the reply.  I checked out the link that you posted, I've read it a few times and I'm sorry to say that I don't see the solution, but maybe I'm still too new at this. 

The example you link has this page definition

Code:
<apex:page standardController="Account">
    <apex:form> 
        <apex:pageBlock title="Hello {!$User.FirstName}!">
            You are viewing the {!account.name} account. <p/>
            Change Account Name: <p/> 
            <apex:inputField value="{!account.name}"/> <p/>
            <apex:commandButton action="{!save}" value="Save New Account Name"/> 
        </apex:pageBlock>
    </apex:form> 
</apex:page>

In this example, a standard controller for an Account object is used for the page.  So the inputField is set to {!account.name}

In my example, the standard controller is set to a custom object LeadTouch, which has a related Lead object.  I fill my inputField with  {! Lead_Touch__c.Lead__r.FirstName}  and all is good for the load.  The trouble is, when I call {!save}, that will not save to the related Lead and usually causes an error.  So I have created an Extension to the Standard Contoller (see my first post.)  In the extension I define LeadTouchSave()  I have been able to to update the Lead record, but I don't know how to get the value of the inputField for the update.  I need to get the users input for this to work.  And from what I understand about the example you referenced is that it works with the record associated with the standard controller.

Any suggestions?

Thanks Again,

 

Jason


 

 

jeremy_rossjeremy_ross
In your controller, expose a property called lead:

Code:
public Lead lead {
  get;
  set;
}

 
In the controller's constructor, set this lead property to a valid instance of a Lead.

Then in your vf page, you can do something like this:

Code:
<apex:inputField value="{!lead.FirstName}"/>

In your save method you have to manually update the lead by doing this:

Code:
update lead;

 I'm not sure if your use case is create or update.  If create, change the above to the create call instead of update.


 Jeremy



Message Edited by jeremy_ross on 10-07-2008 02:21 PM
JvalleeJvallee
Hi Jeremy,
 
I guess that I was think of things a little differently.  This does now makes sense and I was able to make the changes successfully.
 
Thanks,
 
Jason