0

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.

4
  • 1
    One more reason Visualforce buttons are superior. You could just link to the page.
    – Adrian Larson
    Mar 16, 2017 at 17:20
  • How do I turn this into a Visualforce button? Mar 16, 2017 at 17:22
  • what is your hyperlink formula? Mar 16, 2017 at 17:36
  • all of my hyperlink formulas failed as soon as I put "javascript:" in them, being told by Salesforce that it was not allowed.. but I did create this formula to try and click the "Custom Link" I made that executed the javascript: HYPERLINK('javascript:window.invokeOnClickJS_00bK0000000EFk7%28this%29', "Accept Case","_self") Mar 16, 2017 at 17:52

2 Answers 2

2

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;
    }
}
0

I don't think its possible to do the approach you want exactly. However when I have run into similar requests I use the following approach:

  1. Make a List type custom button. Name it something like "Approve Selected Cases"
  2. Get all checked cases and do the logic you have above in a for loop

Code would look something like:

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

var selectedIds = {!GETRECORDIDS($ObjectType.Case)}; 

if (selectedIds[0] == null) { 
    alert("Please select at least one Case.")
} 
else {
    var cases2update = [];
    for(i=0;i<selectedIds.length;i++){

        //your code from above
        var caseObj = new sforce.SObject("Case"); 
        caseObj.Id = 'selectedIds[i]'; 
        caseObj.OwnerId = '{!$User.Id}'; 
        caseObj.One_Stop_Counter__c = "{!TEXT($User.Assigned_Counter__c)}";
        cases2update.push(caseObj);
    }
    sforce.connection.update([cases2update];

}
1
  • I tried this and in most cases it works, except when you have 3 list views on a single visualforce page (via a Visualforce tab) and you get errors about having 0 records selected if you chose a record that's not in the topmost listview. Mar 16, 2017 at 21:25

You must log in to answer this question.

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