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

How to create new object using Custom Button

I used to create Work Order object using a Custom Button (create work order) in Service Order page. This used to call one S-Control which had sforce.connection.create([workorder]);


How can I do similar thing in visualforce, it seems I can't execute any DML like that.


Any suggestion would be appreciated.





You will need an apex:commandbutton or apex:commandlink with an action binding to a method in your controller, either a custom controller or a controller extension.


In that method you can creat the new object and fire off any dml you need.


So the visual force button will look like so:



<apex:commandButton value="New" action="{!CreateNewWorkOrder}"/>

 And the method in your controller / extension like this:



public Pagereference CreateNewWorkOrder(){WorkOrder__c wo = new WorkOrder__c();wo.Property1 = "a value";insert wo;return null;}






Message Edited by rhsumner on 06-17-2009 07:40 AM

I dont want to create button in Visualforce page. I want to add a Custom button which can invoke a Visualforce page and invoke DML.


This is possible in S-Control, want to know the alternate.





What you have to do is create a blank visualforce page with an action="" attribute on it that will create the record you want.






<apex:page action="{!createRecord}" controller="myController"> </apex:page>




public class myController { public PageReference createRecord() { Account a = new Account(); a.Name = 'My New Account'; insert a; return new PageReference(ApexPages.currentPage().getParameters().get('retURL')); } }


I have done this actions, but now I don't see any visualforce pages it the list while creating custom button.
Where is the problem?