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
Alessandro Guarnieri 4Alessandro Guarnieri 4 

Visuaforce page as a list button to create a new account

Hi,

is there a way to write a method in a controller extension which saves the new account i'm creating with the visualforce page below? The standard controller save action doesn't work so i need a method on the controller extension which saves the account

Here the visualforce page:
<apex:page standardController="Account" extensions="testcontroller123" recordSetVar="accounts">
   <apex:form >
       <apex:pageBlock >

       <apex:pageBlockSection >
       
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Account Name</apex:outputLabel>
                <apex:inputField id="actName" value="{!account.name}"/>
            </apex:pageBlockSectionItem>     

           <apex:commandButton value="Save" action="{!saveRecord}"/>


       </apex:pageBlockSection>
           
       </apex:pageBlock>           
   </apex:form>
</apex:page>

Here the extension:
public class testcontroller123 {

    public Account acct;
    
    public testcontroller123(ApexPages.StandardController stdController) {
        this.acct = (Account)stdController.getRecord();
    }
    
public ApexPages.PageReference saveRecord() {
  // I can do custom logic here before I save the record.
  ApexPages.StandardController controller = new ApexPages.StandardController(acct);
  try {
    controller.save();
  }
  catch(Exception e) {
    return null;
  }
  return controller.view();
}

    

}

 
Best Answer chosen by Alessandro Guarnieri 4
Ragava reddyRagava reddy
Hi,

Please check the below code, I think it will hepls for you,

public class recordsetvarsavecon {
    Public Account accobj{get;set;}
    public recordsetvarsavecon(ApexPages.StandardSetController controller) {
        accobj= new Account();
    }
    
    Public void save(){
        Account acc=new Account();
        acc.Name=accobj.Name;
        insert acc;
    }

}

<apex:page standardController="Account" extensions="recordsetvarsavecon" recordSetVar="accounts">
   <apex:form >
       <apex:pageblock >
            <apex:inputField value="{!accobj.name}"/>
       <apex:commandButton value="Save" action="{!save}"/>
        </apex:pageblock>
   </apex:form>
</apex:page>

Thanks,
Raghavendra Reddy.D
 

All Answers

Agustina GarciaAgustina Garcia
What do you exactly need to do? I think you have a mix of concepts.

1st - If you are going to show a single Account, you don't need the recordSetVar attribute in the page. And as you do, use the StandardController (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_pages_standardcontroller.htm) if you need to show several Accounts, it is when you need to use this attribute but need a StandardSetController (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_pages_standardsetcontroller.htm) instead.
2nd - if you want to create a new account with its standard save button, you do not need the controller.
3rd - If you want to use a custom controller as extension and save, use the insert DML instead.

Find below the code with 2 fields. One of them just retrieve the Account Name inserted and create a new account via insert DML. The second use the standard save for that.
 
<apex:page standardController="Account" extensions="testcontroller123">
   <apex:form >
       
       <apex:pageBlock >

           <apex:pageBlockButtons >
               <apex:commandButton value="Save" action="{!saveRecord}"/>
               <apex:commandButton value="Save2" action="{!save}"/>
           </apex:pageBlockButtons>
       
       
           <apex:pageBlockSection >
       
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Account Name</apex:outputLabel>
                    <apex:inputText id="actName" value="{!acctName}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Account Name2</apex:outputLabel>
                    <apex:inputField id="actName2" value="{!account.name}"/>
                </apex:pageBlockSectionItem>     

           </apex:pageBlockSection>
           
       </apex:pageBlock>           
   </apex:form>
</apex:page>
 
public with sharing class testcontroller123
{
    public String acctName {get; set;}
    ApexPages.StandardController controller;
    
    public testcontroller123(ApexPages.StandardController stdController)
    {
        controller = stdController;
    }
    
    public ApexPages.PageReference saveRecord()
    {
        Account newAcc = new Account();
        newAcc.Name = acctName;
        
        try
        {
            insert newAcc;
        }
        catch(Exception e) {
            return null;
        }
        
        return controller.view();
    }
}

Hope this helps
 
Alessandro Guarnieri 4Alessandro Guarnieri 4
Thanks for your reply,

the thing is I want to use the recordSetVar because my final goal is to have this visualforce page as a custom list button, so in order for the visualforce page to be available as a custom list button I need to add a standard list controller using recordSetVar="accounts". But as you saw if I add this standard list controller then the save action doesn't save anymore,so I thought to write a save method in a controller extension, but i'm new to apex and visualforce so i'm not sure all this is possible.

Thanks,

Alessandro
Ragava reddyRagava reddy
Hi,

Please check the below code, I think it will hepls for you,

public class recordsetvarsavecon {
    Public Account accobj{get;set;}
    public recordsetvarsavecon(ApexPages.StandardSetController controller) {
        accobj= new Account();
    }
    
    Public void save(){
        Account acc=new Account();
        acc.Name=accobj.Name;
        insert acc;
    }

}

<apex:page standardController="Account" extensions="recordsetvarsavecon" recordSetVar="accounts">
   <apex:form >
       <apex:pageblock >
            <apex:inputField value="{!accobj.name}"/>
       <apex:commandButton value="Save" action="{!save}"/>
        </apex:pageblock>
   </apex:form>
</apex:page>

Thanks,
Raghavendra Reddy.D
 
This was selected as the best answer
Alessandro Guarnieri 4Alessandro Guarnieri 4
Thanks, this worked for me perfectly. Then all you need to do is to add all the fields you need.

Thanks very much,

Alessandro