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
affableaffable 

how to insert records to customtable using purly customController

hi mates  please help in below Scenario;

Scenario: i have visual page consists of three feilds name,price,address and save button. when i click on save button these fileds information must be stored  as record in custom tableor object Book1__c .

please provide code for this Scenario.

 

 

I have tried with some smple code but its not working am new to apex Language;

 

public class Book3
{
string name=null;
Double Bookprice=null;
Integer Bookid=null;
public Book1__c newbook;
public Book3()
{
 ID id=ApexPages.CurrentPage().getParameters().get('id');
 newbook = (id == null)?new Book1__c():
 [select Bookid__c,name,Bookprice__c from Book1__c where id=:id];
}

public String getName()
 {
 return name;
 }
 public void setName(String name)
 {
  this.name=name;
 }
public Integer getNumber()
 {
 return Bookid;
 }
public void setNumber(Integer Bookid)
{
 this.Bookid=Bookid;
}  
  public Double getprice()
 {
 return Bookprice;
 }
public void setprice(Double  Bookprice)
{
 this.Bookprice=Bookprice;


public Book1__c getbook()
{
 return newbook;
 }
public pageReference save()
{
 try{
  upsert newbook;
  }
  catch(System.DmlException e)
  {
  ApexPages.addMessages(e);
  return null;
  }
  return null;
  }
  }

 

 


visual page
-----------
<apex:page controller="Book3">
<apex:form >
<apex:pageBlock title="This is sample" mode="edit">
<apex:pageMessages />
NAME: &nbsp;&nbsp;&nbsp;&nbsp;
<apex:inputText value="{!Name}"/>
<br/>
BOOKID:&nbsp;&nbsp;&nbsp;&nbsp;
<apex:inputText value="{!Number}"/>
<apex:commandButton value="save" action="{!save}"/>
</apex:pageBlock>
</apex:form>
</apex:page>

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

That usually means there's a character or two before the <apex:page> tag.  

All Answers

bob_buzzardbob_buzzard

Rather than maintaining the values as attributes of the controller, you should be able to simply use the newbook instance for most of this:

 

public class Book3
{
public Book1__c newbook;
public Book3()
{
 ID id=ApexPages.CurrentPage().getParameters().get('id');
 newbook = (id == null)?new Book1__c():
 [select Bookid__c,name,Bookprice__c from Book1__c where id=:id];
}
public Book1__c getbook()
{
 return newbook;
 }
public pageReference save()
{
 try{
  upsert newbook;
  }
  catch(System.DmlException e)
  {
  ApexPages.addMessages(e);
  return null;
  }
  return null;
  }
  }
 

and on the page:

 

 

<apex:page controller="Book3">
<apex:form >
<apex:pageBlock title="This is sample" mode="edit">
<apex:pageMessages />
NAME: &nbsp;&nbsp;&nbsp;&nbsp;
<apex:inputField value="{!book.Name}"/>
<br/>
BOOKID:&nbsp;&nbsp;&nbsp;&nbsp;
<apex:inputField value="{!book.Bookid__c}"/>
<apex:commandButton value="save" action="{!save}"/>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 

 

affableaffable

 it displaying following error.

Error Error: Author4 line 1, column 1: Content is not allowed in prolog Error Error: Content is not allowed in prolog.

 

1 <apex:page controller="Book3">
2 <apex:form >
3 <apex:pageBlock title="This is sample" mode="edit">
4 <apex:pageMessages />
5 NAME: &nbsp;&nbsp;&nbsp;&nbsp;
6 <apex:inputField value="{!book.Name}"/>
7 <br/>
8 BOOKID:&nbsp;&nbsp;&nbsp;&nbsp;
9 <apex:inputField value="{!book.Bookid__c}"/>
10 <apex:commandButton value="save" action="{!save}"/>
11 </apex:pageBlock>
12 </apex:form>
13 </apex:page>

bob_buzzardbob_buzzard

That usually means there's a character or two before the <apex:page> tag.  

This was selected as the best answer