[SalesForce] Hyperlink formula to click Javascript Button

I need to create an "action column" style button on Case listviews, that accomplishes the same thing as a custom javascript button that I put on the actual Case records. I am confused by some of the methods out there, but essentially get the gist that it should be a Hyperlink formula field, and needs to look like this:

Example

I need that link to either execute the same code as my button and then open to the record page, or to have it open the record and click the button that will run the javascript, at the same time. (I've also created a "custom link" version of this button to try to do this, but Salesforce keeps telling me I can't use Javascript in hyperlink formulas)

My button javascript is as follows:

 {!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")} 

var caseObj = new sforce.SObject("Case"); 
caseObj.Id = '{!Case.Id}'; 
caseObj.OwnerId = '{!$User.Id}'; 
caseObj.One_Stop_Counter__c = "{!TEXT($User.Assigned_Counter__c)}"; 
var result = sforce.connection.update([caseObj]); 

if (result[0].success=='false') { 
alert(result[0].errors.message); 
} else { 
window.parent.location.href="/{!Case.Id}"; 
}

Is this not possible at all?

Any help is appreciated.

Best Answer

You can achieve this with the help of a Visual Force page and a Controller. In the Visual Force page put the action name in the apex:page tag itself so that the action method invoked when this page is requested by the server. Pass the Case Id and current (list view) page URL to the Controller on the click of HYPERLINK Formula field. The current Page URL will be used to return to the same page where the HYPERLINK formula field is clicked. In the Controller update the Case and create a PageReference based on the retURL parameter. At the end forward to this PageReference and you will get the updated list view.

HYPERLINK Formula field

HYPERLINK("javascript:function encURI(){return encodeURIComponent(window.location);};javascript:window.location='/apex/casepage?caseid="+ Id +"&retURL='+encURI()" ,'Accept Case', '_self')

Visualforce Page

<apex:page controller="MyCaseController" action="{!updateCase}" >
</apex:page>

Controller

public class MyCaseController {
    public String retUrl {get;set;}
    public String caseId {get;set;}
    public MyCaseController(){
        retUrl = ApexPages.currentPage().getParameters().get('retURL');
        caseId = ApexPages.currentPage().getParameters().get('caseid');
    }
    public PageReference updateCase(){
        PageReference retPage = new PageReference(retUrl);
        //Put your own logic here
        Case cs = new Case(Id=caseId);
        cs.Subject = 'New Case Subject';
        update cs;
        return retPage;
    }
}