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
dev rana 10dev rana 10 

I m trying to insert value from vf page to sobject but value will not pass from text box.

Hi all i m new in salesforce i m trying to insert value in sobject but value of inputfiled is not passing in sobject.


this is my Controller

     public class reg {
             devrana__registration__c registration = new devrana__registration__c();        
                      public devrana__registration__c getregistration()
                      {
                                   return registration;
                        }                          
                          public PageReference save()
                            {   
                                   insert registration;
                                   return null;   
                            }
               }

this is my Vf page
       <apex:page controller="reg" >
    <apex:form >
       <p><b>registraion Page</b><br /></p>
           <apex:panelGrid columns="2" style="margin-top:1em;">
                  <p><b>UserName</b><br />
                     <!--apex:inputText required="true" id="username" /-->
                     <apex:inputField value="{!registration.name__c}"/>                   
                  </p>
                  <p><b>Password</b><br />
                     <apex:inputSecret id="password" />
                  </p>
                 <apex:commandButton action="{!save}" value="Register" id="register"  immediate="true"/>
           </apex:panelGrid>
    </apex:form>
</apex:page>

this is my record which is Auto number
User-added image
 
Best Answer chosen by dev rana 10
mritzimritzi
Apex:
public class reg {
    public devrana__registration__c registration{get;set;};        
    public reg(){
           registration = new devrana__registration__c();
     }    
    public PageReference save(){   
		if(registration.name!=null){
			insert registration;
			return new PageReference('/'+registration.id);
		}
		else
			return null;
    }
}

VF:
<apex:page controller="reg" >
    <apex:form >
       <p><b>registraion Page</b><br /></p>
           <apex:panelGrid columns="2" style="margin-top:1em;">
                 <p><b>UserName</b><br />
					<!-- assuming that the name field here is a custom field, Use api name as per your Org data -->
                     <apex:inputField value="{!registration.name__c}"/>                   
                  </p>
                  <p><b>Password</b><br />
					<!-- correct field name in value attribute as per your object field name -->
                    <apex:inputSecret id="password" value="{!registration.password__c}"/>
                  </p>
                 <apex:commandButton action="{!save}" value="Register" id="register" />
           </apex:panelGrid>
    </apex:form>
</apex:page>

This is a basic working example.
You can modify it to suit your requirement.

If this asnwer helps you out, mark it as BEST ANSWER.

All Answers

mritzimritzi
Apex:
public class reg {
    public devrana__registration__c registration{get;set;};        
    public reg(){
           registration = new devrana__registration__c();
     }    
    public PageReference save(){   
		if(registration.name!=null){
			insert registration;
			return new PageReference('/'+registration.id);
		}
		else
			return null;
    }
}

VF:
<apex:page controller="reg" >
    <apex:form >
       <p><b>registraion Page</b><br /></p>
           <apex:panelGrid columns="2" style="margin-top:1em;">
                 <p><b>UserName</b><br />
					<!-- assuming that the name field here is a custom field, Use api name as per your Org data -->
                     <apex:inputField value="{!registration.name__c}"/>                   
                  </p>
                  <p><b>Password</b><br />
					<!-- correct field name in value attribute as per your object field name -->
                    <apex:inputSecret id="password" value="{!registration.password__c}"/>
                  </p>
                 <apex:commandButton action="{!save}" value="Register" id="register" />
           </apex:panelGrid>
    </apex:form>
</apex:page>

This is a basic working example.
You can modify it to suit your requirement.

If this asnwer helps you out, mark it as BEST ANSWER.
This was selected as the best answer
dev rana 10dev rana 10
Hey thanks mritzi
     i change a code as per your help when i m trying to insert it gives me this error. 

Visualforce Error
    
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!save}' in component <apex:commandButton> in page devrana:reg: Class.devrana.re.save: line 4, column 1
Class.devrana.re.save: line 4, column 1
mritzimritzi
I have update the code, copy and paste it now. It will work.
dev rana 10dev rana 10
Hello mritzi
   now it not insert in sobject. still its not working.
dev rana 10dev rana 10
hey mritzi 
    its done. just change my fild name in vf page and its working  thanks u so much for your help.