What I want to do is have a button on the page layout for one of my custom objects. The only action this button should take is to change the record owner Id to a specified queue Id and return the page, refreshed,to show the updated values. Is it possible for me to have this code run from a custom button that can retrieve values like the Record Id from the page it's on

how can I do this on a standard page and not on a Visualforce page? I have some simple controller code written:

public with sharing class VF_ChangeOrderOwner {

    private final OrdersLite__c ord ;

     public OrdersLite__c getOrderId() {

        return [SELECT Id FROM OrdersLite__c WHERE Id = :ApexPages.currentPage().getParameters().get('Id')];


    public VF_ChangeOrderOwner(ApexPages.StandardController stdController) 
        this.ord = getOrderId();

    public PageReference changeOwner(){

        ord.OwnerId = '00G20000001BH55';   /*My queue Id*/

        PageReference pref = new PageReference('https://emea.salesforce.com/'+ord.Id) ;
        return pref;

And the button is just an url that calls the class URL with the order Id, like so: '/apex/VF_ChangeOrderOwner?Id={!OrdersLite__c.Id}'

This doesn't do anything as is, so i'm just wondering whether I'm on the right track?

3 Answers 3


With out any Visualforce Page this can be done using Ajax Toolkit.

Below example Shows updating one of the field on click of Button..Create a button -> Execute Javascript & on Click Javascript.


 var newTask= new sforce.SObject("Task");

 newTask.id = "{!Task.Id}";


 var result = sforce.connection.update([newTask]);

 if (result[0].getBoolean("success"))
   // Refresh window
   alert("something broke");
  • Thanks for that, Ajax Toolkit is the best solution for this, since all I'm doing is updating a single field on the object.
    – akarnid
    Commented Aug 14, 2013 at 15:57
  • @akarnid Glad that helped!! Commented Aug 16, 2013 at 3:30

Yes, I think you want a button on detail page of a custom object record. when ever this button will click you want to change the owner of that record.

For this you have to crate a button on that custom object. The button display should be detail type. Behavior should be new window and content type url. The url just open a new window with a vifsual force page and you need to pass the id of record in the url. The visual force page shuold have custom controller. That controller get the id of that record and fetch the record. "We know we can't use DML in constructor ". When ever our page loaded we can call a apex function by javascipt that function call in our controller then our controller processed that and change the owner of that record. and the apex action call a function on oncomplete attr. that function is written in script and close the open window and refresh the parent window...........

I know I have written a complex thing but when you try to implement this it very easy.


Although the "correct" method is through the creation of a VF page as described, it's quicker and easier to create a webservice class instead and put your code in there. I'll warn you that some people will argue against the method, but I'd suggest you read this blog post for really simple instructions, and the comments below it so you can make an educated decision on whether to do it that way. Go here: http://salesforcesource.blogspot.co.uk/2009/06/triggering-apex-method-with-custom.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .