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
HayaHaya 

How do I clear values from page?

I have 2 buttons on a page: Save and Clear.

How do I clear values from the page if the Clear button is clicked?

 

Help would be greatly appreciated

 

My page:

<apex:page standardController="Case" showHeader="true" sidebar="true" extensions="RequestNewExtension"  tabStyle="New_Request__tab">
  <apex:form >
  <apex:sectionHeader subtitle="Create a Request">
  <div style="float:right">
        <span class="requiredExample">&nbsp;</span>
        <span class="requiredMark">*</span>
        <span class="requiredText"> = Required Information</span>
  </div>
    </apex:sectionHeader>


  <apex:pageMessages />  
    <apex:pageBlock >
        <apex:pageBlockButtons location="both">
                <apex:commandButton value="Save" action="{!Save}" />
                <apex:commandButton value="Clear" action="{!Cancel}" />
            </apex:pageBlockButtons>
<apex:pageBlockSection id="cas14" title="Request Subject/Description" collapsible="false" columns="2">

                 <apex:pageBlockSectionItem >
                <apex:outputLabel >Subject</apex:outputLabel>                
                <apex:inputField id="caseSubject" value="{!case.Subject}" style="width:90%" required="true" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem > </apex:pageBlockSectionItem>
              
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Description</apex:outputLabel>                  
                    <apex:inputField id="caseDescription" value="{!case.Description}" style="width:90%; height: 75px" required="true" />
                </apex:pageBlockSectionItem>               
                <apex:pageBlockSectionItem ></apex:pageBlockSectionItem>
                 </apex:pageBlockSection>
   
  <apex:pageBlockSection id="cas5" title="Request Detail:" collapsible="false" columns="2">
        <apex:pageBlockSectionItem >
                   <apex:outputLabel >Request Type</apex:outputLabel>                  
                   <apex:inputField id="caseType" value="{!case.Type}" required="true" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem ></apex:pageBlockSectionItem>                            
                <apex:pageBlockSectionItem ></apex:pageBlockSectionItem>
                 </apex:pageBlockSection>
      <apex:inputHidden value="{!Comment}" id="theHiddenInput"/> 
                 
  </apex:pageBlock>
  </apex:form>
</apex:page>

 

My Extension:

 

public with sharing class RequestNewExtension {
    public String Comment
    {
        get;
        set;
    }
    public String Description
    {
        get;
        set;
    } 
    public String Subject
    {
        get;
        set;
    }
    public Boolean UseAssignmentRules
    {
        get;set;
    }
   
    public Case cs;
    ApexPages.StandardController controller;
   
    public RequestNewExtension (ApexPages.StandardController con)
    {
        controller = con;
        this.cs = (Case) con.getRecord();
    }
   
    public PageReference Save()
    {
        CaseComment com = new CaseComment();
        if(Comment.Length() > 0)
        {
            com.commentBody = Comment;
            com.ParentId = cs.Id;
            if(UseAssignmentRules == true)
            {
                AssignmentRule  AR = new AssignmentRule();
                AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];
               
                //Creating the DMLOptions for "Assign using active assignment rules" checkbox
                Database.DMLOptions dmlOpts = new Database.DMLOptions();
                dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;
                controller.getRecord().setOptions(dmlOpts);               
            }
            insert com;
        }
        String retURL = ApexPages.currentPage().getParameters().get('retURL');
        String CurrentId = ApexPages.currentPage().getParameters().get('id');
        PageReference redirectPG;
        if(retURL != null)
            redirectPG = new PageReference('/' + retURL);
        else if(CurrentId != null)
            redirectPG = new PageReference('/' + CurrentId);
       
        controller.Save();
       
         return Page.Request_Create_Thankyou;
    }
    public PageReference Cancel(){
return null;
}
 }

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Tim BarsottiTim Barsotti

Hi, you might want to try this: 

 

     public PageReference Cancel(){ 
         PageReference pageRef = ApexPages.currentPage();
         pageRef.setRedirect(true);         
         return pageRef;
    }

 

All Answers

hitesh90hitesh90

Hi,

 

try to use following code.

 

Visualforce Page:

<apex:page standardController="Case" showHeader="true" sidebar="true" extensions="RequestNewExtension"  >
  <apex:form >
  <apex:sectionHeader subtitle="Create a Request">
  <div style="float:right">
        <span class="requiredExample">&nbsp;</span>
        <span class="requiredMark">*</span>
        <span class="requiredText"> = Required Information</span>
  </div>
    </apex:sectionHeader>


  <apex:pageMessages />  
    <apex:pageBlock >
        <apex:pageBlockButtons location="both">
                <apex:commandButton value="Save" action="{!Save}" />
                <apex:commandButton value="Clear" action="{!Cancel1}" />
            </apex:pageBlockButtons>
<apex:pageBlockSection id="cas14" title="Request Subject/Description" collapsible="false" columns="2">

                 <apex:pageBlockSectionItem >
                <apex:outputLabel >Subject</apex:outputLabel>                
                <apex:inputField id="caseSubject" value="{!cs.Subject}" style="width:90%" required="true" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem > </apex:pageBlockSectionItem>
              
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Description</apex:outputLabel>                  
                    <apex:inputField id="caseDescription" value="{!cs.Description}" style="width:90%; height: 75px" required="true" />
                </apex:pageBlockSectionItem>               
                <apex:pageBlockSectionItem ></apex:pageBlockSectionItem>
                 </apex:pageBlockSection>
   
  <apex:pageBlockSection id="cas5" title="Request Detail:" collapsible="false" columns="2">
        <apex:pageBlockSectionItem >
                   <apex:outputLabel >Request Type</apex:outputLabel>                  
                   <apex:inputField id="caseType" value="{!cs.Type}" required="true" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem ></apex:pageBlockSectionItem>                            
                <apex:pageBlockSectionItem ></apex:pageBlockSectionItem>
                 </apex:pageBlockSection>
      <apex:inputHidden value="{!Comment}" id="theHiddenInput"/> 
                 
  </apex:pageBlock>
  </apex:form>
</apex:page>

 

 

 

 

 Apex Class:

public with sharing class RequestNewExtension {
    public String Comment
    {
        get;
        set;
    }
    public String Description
    {
        get;
        set;
    } 
    public String Subject
    {
        get;
        set;
    }
    public Boolean UseAssignmentRules
    {
        get;set;
    }
   
    public Case cs{get; set;}
    ApexPages.StandardController controller;
   
    public RequestNewExtension (ApexPages.StandardController con)
    {
        controller = con;
        this.cs = (Case) con.getRecord();
    }
   
    public PageReference Save()
    {
        CaseComment com = new CaseComment();
        if(Comment.Length() > 0)
        {
            com.commentBody = Comment;
            com.ParentId = cs.Id;
            if(UseAssignmentRules == true)
            {
                AssignmentRule  AR = new AssignmentRule();
                AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];
               
                //Creating the DMLOptions for "Assign using active assignment rules" checkbox
                Database.DMLOptions dmlOpts = new Database.DMLOptions();
                dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;
                controller.getRecord().setOptions(dmlOpts);               
            }
            insert com;
        }
        String retURL = ApexPages.currentPage().getParameters().get('retURL');
        String CurrentId = ApexPages.currentPage().getParameters().get('id');
        PageReference redirectPG;
        if(retURL != null)
            redirectPG = new PageReference('/' + retURL);
        else if(CurrentId != null)
            redirectPG = new PageReference('/' + CurrentId);
       
        controller.Save();
       
         return Page.Request_Create_Thankyou;
    }
     public PageReference Cancel(){
         this.cs = new case();
         return null;
    }

 }

 

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

 

HayaHaya

Thank you, Hitesh.

I made the changes you recommended but the values that were inputted to the form were not cleared.

Any other ideas?

Thank you again,

Haya

 

Tim BarsottiTim Barsotti

Hi, you might want to try this: 

 

     public PageReference Cancel(){ 
         PageReference pageRef = ApexPages.currentPage();
         pageRef.setRedirect(true);         
         return pageRef;
    }

 

This was selected as the best answer
HayaHaya

Thank you so much.

Haya