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
ajay hada 9ajay hada 9 

want to save data in table

I create a table mytest__c and there is name field in table

i want to save input filed value in name field . so please help me . i create vf page and apex class for this

i create a vf page :

<apex:page controller="myClassController">
    <apex:form >
        <apex:inputField value="{! mytest1.Name}"/>
        <apex:commandButton value="Save" action="{! Save}"/>
    </apex:form>
</apex:page>

I create apex class is as fellow :

public class myClassController {
    public mytest__c mytest1{get;set;}
    public mytest__c mytest() {
        mytest1 = new mytest__c();
        return mytest1;
    }
    public PageReference Save(){
        //system.debug(mytest1);
        mytest1 = new mytest__c();
        insert mytest1;
        return null;
    }

}

 

So provide me solution

 

ajay hada 9ajay hada 9
This is not working
ManojjenaManojjena
Hi Ajay,

Please try below code it will solve your problem,but you need to add mandatory fields in page .
 
<apex:page controller="myClassController" StandradController="mytest__c">
    <apex:form >
        <apex:inputField value="{!mytest__c.Name}"/>
        <apex:commandButton value="Save" action="{!doSave}"/>
    </apex:form>
</apex:page>

public class MyClassController {
	public mytest__c mytest;
    public MyClassController (ApexPages.StandardController con){
      mytest= (mytest__c)con.getRecord();
    }
    public void doSave(){
		try{
			insert mytest;
		}catch(DMLexception de){
			System.debug('*******'+de);
		}
    }
}

If this helps you to solve your problem please mark it as best answer. It will help other to find best answer.
AnjaneyluAnjaneylu
Hi manoj,
What is the function of this line,
public myClassController(ApexPages.StandardController controller)
and what is happend while passing the parameter as ApexPages.StandardController controller
and in the save method why don't you mentioned pagereference in the place of void..
 
ManojjenaManojjena
Hi Anji,

public myClassController(ApexPages.StandardController controller)  line is a constructor of taking parameter as standradcontroller  instance to use the method in that class .

ApexPages.StandardController , it is like this because the class is in the namesapce ApexPages.

If you add any standradcontroller in your page system will automatically ask you to add one contructor like this .

Returntype PageRefrence you can add if you want, but you need to return som ething from class .
If you want to redirect to ant page you can use pageReference as well 

You can check below links for pagereference and Standradcontroller methods .
www.salesforce.com/docs/developer/pages/Content/apex_ApexPages_StandardController_methods.htm  


www.salesforce.com/us/developer/docs/apexcode/Content/apex_system_pagereference.htm

Please let meknow any issue .

 
ajay hada 9ajay hada 9

DEAR Manoj ,

                Your code is not working for me

ManojjenaManojjena
Hey Ajay ,

Apart from name is there any required field in your object . How you can check, just create a record from salesforce ui by clicking on the tab .
How may field value you need to enter from there those field you need to keep .

Else better  set a debug log and try to check in debug ,if insert fail it wil throw you an exception which you can catch in debug .

Please let me know still you have issue . 

 
ajay hada 9ajay hada 9
Any one please provide me right code to save form data in custom table
ManojjenaManojjena
Hi Ajay ,

Have you tested with salesforce UI .As the standrad validation rule will fire .So you need to add the standrad fields with Name .
is the name is the only one mandatory field in your custom object .Please confirm.
KapilCKapilC
Hello Ajay,

Give a try with the code below.
 
<apex:page controller="myClassController">
    <apex:form >
        <apex:inputField value="{! mytestRec.Name}"/>
        <apex:commandButton value="Save" action="{!saveData}"/>
    </apex:form>
</apex:page>



public class myClassController {
    public mytest__c mytestRec{get;set;}
  
  //Constructor Of The Class.
   public  myClassController () {
        mytestRec = new mytest__c();
    }
    public PageReference saveData(){
      
        insert mytestRec;
        // Send to detail record.
        return new PageReference('/'+mytestRec.id);
    }

}
Thanks,
Kapil