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
Aditya Rayavarapu 5Aditya Rayavarapu 5 

Pass variable in Controller

Hello,
Please take a look at my controller (Public string acc4). In the else statement, I have hardcoded the value 'xxxx', but I want it to take the value of the string acc3 (which is an input field in the VF page). I've tried a few different ways for this, but can't seem to figure it out. Any suggestions?
Thanks


Part of VF Page:

 <apex:inputField value="{!c.Lastname}" />
 <apex:inputField value="{!c.Master__c}" />
  <apex:inputField value="{!cont.Lastname}" />

  <apex:commandButton action="{!save}" value="Save"/>


Part of CONTROLLER:

public Contact c {get; set;}
Public Contact cont{get;set;}
Public String accId{get;set;}
Public String acc2{get;set;}
Public String acc3{get;set;}
Public String acc4{get;set;}
public controller(){

        accId = ApexPages.currentPage().getParameters().get('LastName'); 
           if(accId != null)        
            {
                this.c= [   SELECT  LastName
                              FROM    Contact
                              WHERE   LastName = : accId ];  
            }
            
             else
            {this.c= new Contact  ();} 
            
            acc3 = ApexPages.currentPage().getParameters().get('Master__c'); 
           if(acc3 != null)        
            {
                this.c= [   SELECT  Master__c
                              FROM    Contact
                              WHERE   Master__c = : acc3 ];  
            }
            
             else
            {this.c= new Contact  ();} 
            
            acc2 = ApexPages.currentPage().getParameters().get('LastName'); 
           if(acc2 != null)        
            {
                this.cont= [   SELECT  LastName
                              FROM    Contact
                              WHERE   LastName = : acc2 ];  
            }
            
             else
            {this.cont=  new Contact(); } 
            
            acc4 = ApexPages.currentPage().getParameters().get('Master__c'); 
           if(acc4 != null)        
            {
                this.cont= [   SELECT  Master__c
                              FROM    Contact
                              WHERE   Master__c = : acc3 ];  
            }
            
             else
            {this.cont=  new Contact(ReportsTo=new Contact(Master__c='xxxx'); } 
            
            }

 public PageReference save() {
insert c;
insert cont;
ManjunathManjunath
Hi,

See the difference between the "Inputfield" and "inputtext". I you want to bind the varibale to the controller use that in the vf page.

to bind it to a controller variable use inputtext.

Regards,
MCS

 
ManjunathManjunath
in your Vf page a node like this needs to be added
  <apex:inputtext value="{!acc3}" />
Aditya Rayavarapu 5Aditya Rayavarapu 5
I am inserting two contacts at once. The second contact has a lookup to the first one via the ReportsTo field. The Master__c field is an external id, and therefore I need to set ReportsTo field to the Master__c value from the first contact. In the controller I have to specify this.cont=new contact (reportsto= new contact (master__c='xxxx')). Instead of the xxxx, it has to be something similar to master__c=acc3 but I can't figure out the syntax.