+ Start a Discussion
chikpeachikpea 

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.

 

Thanks

Bhaskar

rhsumnerrhsumner

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
chikpeachikpea

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.

 

Thanks

Bhaskar

XactiumBenXactiumBen

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

 

example:

 

VF:

 

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

 

Controller:

 

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')); } }

 

samarskysamarsky
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?
 
Thanks.