You need to sign in to do that
Don't have an account?
Alessandro 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:
Here the extension:
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(); } }
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
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.
Hope this helps
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
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
Thanks very much,
Alessandro