+ Start a Discussion
Samuel RobertSamuel Robert 

unable to save a record in Custom object using Visual Force page

/-- Below is my Visual force page coding.

<apex:page Controller="CreateAccount" >

<head>
  <meta charset="utf-8"/>
  <title>jQuery UI Datepicker - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"/>
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css"/>

  <script>
  $(function() {
    $( "#datepicker" ).datepicker();
  });
  </script>
</head>
<body>
 

 
</body>

<apex:stylesheet value="http://www.pageresource.com/wallpapers/wallpaper/latest-google-hd.jpg"/>
 <apex:form id="form">
 <div align="center" draggable="false" >
 <apex:image url="http://googlezone.ru/wp-content/uploads/2014/11/101020141429292562322.jpg" width="120" height="50"/>
 </div>
 <apex:pageBlock title="Create your Google Account" mode="edit"  id="pgblock"> 
 <apex:pageBlockSection >
 
 Firstname <apex:inputtext value="{!Firstname}"/>
 Lastname <apex:inputtext value="{!Lastname}"/>
 Username <apex:inputtext value="{!Username}"/>
 Password <apex:inputtext value="{!Password}"/>
 Retypepassword <apex:inputtext value="{!Retypepassword}"/>
 MobilePhone <apex:inputtext value="{!MobilePhone}"/>
 Email <apex:inputtext value="{!Email}"/>
 DateOfBirth <input type="text"  id="datepicker"/>
 </apex:pageBlockSection>
 <apex:commandButton action="{!saveRecord}" style="float:centre" styleClass="button" value="Save" id="recordID"  />
 </apex:pageBlock>
 </apex:form>
</apex:page>


Apex Class

public with sharing class CreateAccount {

    public PageReference saveRecord() {
       
 GoogleBlog__c blog = new GoogleBlog__c();
    blog.FirstName__c= Firstname;
    blog.Lastname__c= Lastname;
    blog.Password__c= Password;
    blog.RetypePassword__c= Retypepassword;
    blog.Username__c= Username;
    blog.MobilePhone__c= MobilePhone;
    blog.Email__c= Email;
    Date.valueOf(DateOfBirth);
    insert blog;
    system.debug('!!!!!!!!!!' + blog);
  return null;
  
    }

    public String getContact() {
        return null;
    }


Public string Firstname {get;set;}
Public string Lastname {get;set;}
Public string Username {get;set;}
Public string Password {get;set;}
Public string Retypepassword {get;set;}
Public string MobilePhone {get;set;}
Public string Email {get;set;}
Public string DateOfBirth {get;set;}
}

When i try to save a Recod it throws me error like this, Can anyone suggest me what is the mistake in thisERROR
Best Answer chosen by Samuel Robert
SRKSRK
there are two ways, one id you can create a "public date DateVaribale{get;set;}
and <apex:inputtext value="{!DateVaribale}/>

but in this case you will have two issues, you will not get pop up calender
and there is no vaidation on date formate, so you have to write javascript for the same


Or you can do this

public <Opportunity> TempOppbjFordate{get;set;}

in contructor inilazie wiht null
public CreateAccount()
{
TempOppbjFordate = new Opportunity();
}

then in vf page 

<apex:inputfield value="{!TempOppbjFordate.Closedate}"/>

and then in the end in saveRecord()  method , move the value from Closedate to DateOfBirth 

 GoogleBlog__c blog = new GoogleBlog__c();
...
..
..
if(TempOppbjFordate.Closedate != null)
{
blog.DateOfBirth = TempOppbjFordate.Closedate;
}



in  this case, you will get pop up calender
and there will be automatice vaidation from salesforce on date formate,

and in plase of opportunity you can use and object and in plase of close date you can use any date filed from the object

 

All Answers

SRKSRK
It because of you are try to convert a null into date, by using Date.valueOf(DateOfBirth);

put a null check before using Date.valueOf, as ,mentioned below

if(DateOfBirth != null && DateOfBirth != '')
{
Date.valueOf(DateOfBirth);
}

Also i dont see you bind DateOfBirth with any of input field so it will not get populated please also take case of that if you want to populate date of birth
Samuel RobertSamuel Robert
Hi SRK, Can you please tell me how do i bind DOB to input field to make it populated
SRKSRK
there are two ways, one id you can create a "public date DateVaribale{get;set;}
and <apex:inputtext value="{!DateVaribale}/>

but in this case you will have two issues, you will not get pop up calender
and there is no vaidation on date formate, so you have to write javascript for the same


Or you can do this

public <Opportunity> TempOppbjFordate{get;set;}

in contructor inilazie wiht null
public CreateAccount()
{
TempOppbjFordate = new Opportunity();
}

then in vf page 

<apex:inputfield value="{!TempOppbjFordate.Closedate}"/>

and then in the end in saveRecord()  method , move the value from Closedate to DateOfBirth 

 GoogleBlog__c blog = new GoogleBlog__c();
...
..
..
if(TempOppbjFordate.Closedate != null)
{
blog.DateOfBirth = TempOppbjFordate.Closedate;
}



in  this case, you will get pop up calender
and there will be automatice vaidation from salesforce on date formate,

and in plase of opportunity you can use and object and in plase of close date you can use any date filed from the object

 
This was selected as the best answer
Samuel RobertSamuel Robert
Thanks man
SRKSRK

Happy days ;)
Please mark it as solved if it solve your issues 

 

thanks ::)

Samuel RobertSamuel Robert
How to mark it solved ?
SRKSRK
Just go to your question and mark it as bast answer :)

thanks