+ Start a Discussion
Rahul Singh 1384Rahul Singh 1384 

Visualforce Page like a form to save data

Hello Friends,
I am new to visualforce development,
I want to create a visualforce form in which i want a picklist field and values for picklist will be : DOC, RAR, ZIP
and a TextArea Field in which we can give comment or description
and a Save Button which will save all data of the form to the Activity Object Inside Salesforce
I have picklist field 'Filetype' with values DOC, RAR, ZIP and a TextArea Field 'Description' on Activity Object

and please do not provide any link or reference, if possible, then help me with an example,

Any help would be greatly appreciated
Thanks in Advance
Best Answer chosen by Rahul Singh 1384
mukesh guptamukesh gupta
Hi Rahul,

Please use this code:-

Visualforce page:-
<apex:page standardController="Task" extensions="TaskExtension">

<apex:form >
    <apex:pageBlock >
<p> FIleType
           <apex:selectList size="1" value="{!selectedFileType}">
             <apex:selectOption itemLabel="--None--" itemvalue=""></apex:selectOption>
             <apex:selectOption itemLabel="DOC" itemvalue="DOC"></apex:selectOption>
             <apex:selectOption itemLabel="RAR" itemvalue="RAR"></apex:selectOption>
             <apex:selectOption itemLabel="ZIP" itemvalue="ZIP"></apex:selectOption>
           </apex:selectList> 
</p>            
    <p>Task Comment    </p>       
                               <apex:inputTextarea value="{!comment}" label="Task Comment"/>   

 <p></p>

            
                <apex:commandButton action="{!taskInsert}" Value="Add Task"/>
 

       
     
    
    
      <apex:pageBlockTable value="{!acc}" var="a">
     
             <apex:column > 
                <apex:facet name="header">Account Name</apex:facet> 
                <apex:outputText value="{!a.Name}"/> 
            </apex:column> 

           <apex:column > 
                <apex:facet name="header">Account Number</apex:facet> 
                <apex:outputText value="{!a.AccountNumber}"/> 
            </apex:column> 

    
    
    </apex:pageBlockTable>
    

    </apex:pageBlock>

</apex:form>
</apex:page>

APex controller:-
 
public class TaskExtension {

 
public string comment{get;set;}
public string selectedFileType{get;set;}
public List<Account>acc {get;set;}

    public TaskExtension(ApexPages.StandardController controller) {
    
    
    
    

    }
    
    public PageReference taskInsert() {
            
    String userId = UserInfo.getUserId();
    
    Task t = new Task();
    t.OwnerId = userId;
    t.Subject = 'Hello World';
    t.Status = 'Open';
    t.Priority = 'Normal';
    t.WhatId = '0012800001a4VGL';//o.Id;
    t.FileType__c = selectedFileType;
    t.Task_Comment__c = comment;
    
    insert t;
    
    
      
    acc = [select name, AccountNumber from Account where id='0012800001a4VGL'] ;
     // pagereference ref = new pagereference('/apex/messsagePage');
       // ref.setredirect(true);
        return null;

    
    }

}
Please let me know if you have any issue

Rahul: Please mark it as a  BEST SOLUTION .

Regards
Mukesh

All Answers

Suraj TripathiSuraj Tripathi
Hi Rahul,

Here is a good example of a VF page and a controller:

http://www.salesforce.com/docs/developer/pages/Content/pages_controller_custom.htm

But if you have a custom object with some fields, you can use the standard page layout and the standard edit page to save this information, there is no need to add custom code if you just want to save some basic data, usually a visual force page + controller its when you have a custom page with some special requirements. 

Here is a video (kind of old but its shows the basic) about how to create a custom object and use the pagelayout to insert data:

https://www.youtube.com/watch?v=uD5t8rCvqlE

Kindly mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Regards
Suraj
mukesh guptamukesh gupta
Hi Rahul,
first you need to create fileType picklist custom field on Activity object, Because we can't add any new custom object in task, so by Activity we can add new field in Task, FileType, Task Comment,

visualforce page 
<apex:page standardController="Task" extensions="TaskExtension">

<apex:form >
    <apex:pageBlock >
      <apex:selectList size="1" value="{!selectedFileType}">
             <apex:selectOption itemLabel="--None--" itemvalue=""></apex:selectOption>
             <apex:selectOption itemLabel="DOC" itemvalue="DOC"></apex:selectOption>
             <apex:selectOption itemLabel="RAR" itemvalue="RAR"></apex:selectOption>
             <apex:selectOption itemLabel="ZIP" itemvalue="ZIP"></apex:selectOption>
    </apex:selectList> 
    <apex:pageBlockSection > 
            <apex:inputTextarea value="{!comment}" label="Task Comment"/>   
    </apex:pageBlockSection>
    Apex
     <apex:commandButton action="{!taskInsert}" Value="Add Task"/>

    </apex:pageBlock>

</apex:form>
</apex:page>

Apex controller:-
public class TaskExtension {

 
public string comment{get;set;}
public string selectedFileType{get;set;}
    public TaskExtension(ApexPages.StandardController controller) {
    
    
    
    

    }
    
    public PageReference taskInsert() {
            
      /*  Task t = new Task();
    t.FileType__c = selectedFileType;
    t.Task_Comment__c = comment;
    
    insert t;*/
    String userId = UserInfo.getUserId();
    
    Task t = new Task();
    t.OwnerId = userId;
    t.Subject = 'Hello World';
    t.Status = 'Open';
    t.Priority = 'Normal';
    t.WhatId = '0012800001a4VGL';//i am using static Account id for insert in particular Account;
    t.FileType__c = selectedFileType;
    t.Task_Comment__c = comment;
    
    insert t;
    
    
     // pagereference ref = new pagereference('/apex/messsagePage');
       // ref.setredirect(true);
        return null;

    
    }

}
if you need any help let me know

PLEASE MARK AS A BEST ANSWER!!!!!!!!!

Regards
Mukesh
Rahul Singh 1384Rahul Singh 1384
Hi Mukesh,
Thank you so much!
This is working absolutely perfect,
You have given account id statically, no problem
one more and last help i need, can you help me in showing that account's 2 field ('Name' and 'Account Number') on same VF Page.
any assistance?
mukesh guptamukesh gupta
Hi Rahul,

Please use this code:-

Visualforce page:-
<apex:page standardController="Task" extensions="TaskExtension">

<apex:form >
    <apex:pageBlock >
<p> FIleType
           <apex:selectList size="1" value="{!selectedFileType}">
             <apex:selectOption itemLabel="--None--" itemvalue=""></apex:selectOption>
             <apex:selectOption itemLabel="DOC" itemvalue="DOC"></apex:selectOption>
             <apex:selectOption itemLabel="RAR" itemvalue="RAR"></apex:selectOption>
             <apex:selectOption itemLabel="ZIP" itemvalue="ZIP"></apex:selectOption>
           </apex:selectList> 
</p>            
    <p>Task Comment    </p>       
                               <apex:inputTextarea value="{!comment}" label="Task Comment"/>   

 <p></p>

            
                <apex:commandButton action="{!taskInsert}" Value="Add Task"/>
 

       
     
    
    
      <apex:pageBlockTable value="{!acc}" var="a">
     
             <apex:column > 
                <apex:facet name="header">Account Name</apex:facet> 
                <apex:outputText value="{!a.Name}"/> 
            </apex:column> 

           <apex:column > 
                <apex:facet name="header">Account Number</apex:facet> 
                <apex:outputText value="{!a.AccountNumber}"/> 
            </apex:column> 

    
    
    </apex:pageBlockTable>
    

    </apex:pageBlock>

</apex:form>
</apex:page>

APex controller:-
 
public class TaskExtension {

 
public string comment{get;set;}
public string selectedFileType{get;set;}
public List<Account>acc {get;set;}

    public TaskExtension(ApexPages.StandardController controller) {
    
    
    
    

    }
    
    public PageReference taskInsert() {
            
    String userId = UserInfo.getUserId();
    
    Task t = new Task();
    t.OwnerId = userId;
    t.Subject = 'Hello World';
    t.Status = 'Open';
    t.Priority = 'Normal';
    t.WhatId = '0012800001a4VGL';//o.Id;
    t.FileType__c = selectedFileType;
    t.Task_Comment__c = comment;
    
    insert t;
    
    
      
    acc = [select name, AccountNumber from Account where id='0012800001a4VGL'] ;
     // pagereference ref = new pagereference('/apex/messsagePage');
       // ref.setredirect(true);
        return null;

    
    }

}
Please let me know if you have any issue

Rahul: Please mark it as a  BEST SOLUTION .

Regards
Mukesh
This was selected as the best answer