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
sheila srivatsavsheila srivatsav 

quicksave issue in visualforce page

Hi
I am testing my visualforce & apex coding skills .
I have implemented a extension class with standard controller to mimic the functionality of SAVE , SAVE&NEW and QUICKSAVE.
public with sharing class MimicSaveQuickSave {

    ApexPages.StandardController sController;
    
    public MimicSaveQuickSave(ApexPages.StandardController stdcontroller) {

         sController=stdcontroller;
         
    }

    public PageReference saveAndNew()
   {
   
      sController.save();
      PageReference pref=new PageReference('/003/o');
      pref.setRedirect(true);
      
      return pref;
   }
 }
<apex:page standardController="Contact" extensions="MimicSaveQuickSave">
  <apex:form >
  
    <apex:pageBlock title="My Content" mode="edit">
    
      <apex:pageBlockButtons >
      
        <apex:commandButton action="{!save}" 
                            value="Save"/>
                            
      <apex:commandButton action="{!QuickSave}" 
                            value="QuickSave"/> 

       <apex:commandButton action="{!saveAndNew}" 
                            value="SaveAndNew"/>
                            
       <apex:commandButton action="{!cancel}" 
                            value="cancel"/>

      </apex:pageBlockButtons>
      
      <apex:pageBlockSection title="My Content Section" columns="2">
        <apex:inputField value="{!contact.firstname}"/>
        <apex:inputField value="{!contact.lastname}"/>
       </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>
The SAVE &  SAVE&NEW is working but I am unable to code for QUICKSAVE
a) when I click SAVE&NEW button is it possible to redirect to the same visualforce page, so the enduser can enter the input values again.
b) how to implement QUICKSAVE functionality

Pls help me out

Thanks
sheila

 
Best Answer chosen by sheila srivatsav
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Sheila,

Try the below code in your controller ,this will work as expected:
 
public PageReference saveAndNew()
   {
   
      sController.save();
      PageReference pref=new PageReference('/apex/SaveFunctionality');
      pref.setRedirect(true);
      
      return pref;
   }
    public PageReference QuickSave()
    {
        sController.save();
        PageReference p=new PageReference('/apex/SaveFunctionality?id='+sController.getId());
        
        return p;
    }

Here the saveAndNew will again redirect back to same vf page and the QuickSave will run as expected.

Thanks
Hope this will be helpful.

All Answers

v varaprasadv varaprasad
Hi Sheila,

Try like this :
 
public PageReference saveNew() { 
PageReference pr; 
try{ 
con.save(); 
Schema.DescribeSObjectResult describeResult = con.getRecord().getSObjectType().getDescribe(); 
pr = new PageReference('/' + describeResult.getKeyPrefix() + '/e'); 
pr.setRedirect(true); 
return pr; 
}catch(Exception e){ 
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, e.getMessage())); return null; 
}

}




Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Salesforce Project Support: varaprasad4sfdc@gmail.com

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Sheila,

Try the below code in your controller ,this will work as expected:
 
public PageReference saveAndNew()
   {
   
      sController.save();
      PageReference pref=new PageReference('/apex/SaveFunctionality');
      pref.setRedirect(true);
      
      return pref;
   }
    public PageReference QuickSave()
    {
        sController.save();
        PageReference p=new PageReference('/apex/SaveFunctionality?id='+sController.getId());
        
        return p;
    }

Here the saveAndNew will again redirect back to same vf page and the QuickSave will run as expected.

Thanks
Hope this will be helpful.
This was selected as the best answer
AshishkAshishk
Change the code, add query in constructor, if parameter is passed to page then query and set contact global variable. Else page will act like a new contact save page.

Call the same page after save with contact id, it will work for quick save.
sheila srivatsavsheila srivatsav
Hi varaprasad
I need to understand how to deal with metadata, so I will debug and check.

Thanks for sharing