0

I have a custom button on a record page that executes javascript, calls a method1 in a Utilities class.

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
var result = sforce.apex.execute("Utilities","Method1",{Id:"{!Account.Id}"});
alert(result);

There is another menthod too in the class (method2).

Now I want the ability for the user to choose which method to call in the javascript pop-up and then call the appropriate method.

Yes, I tried these

  • Visualforce page to redirect from the custom button with selectOptions to choose from and call the appropriate method and it works.
  • creating a new picklist field on the object with those options and in javascript checks this picklist value and calls the method. It works too.

But is there a way that I can do completely in the custom button without using extra Visualforce page/fields at all?

2 Answers 2

3

Custom buttons support basic javascript. You can use a prompt box to ask for user's input

var methodSelection = prompt("Please enter which method you want to execute. Available options are : 1. MethodA 2. MethodB. Select 1 or 2);
if (person != null) {
    if(methodSelection == '1')
        var result = sforce.apex.execute("Utilities","Method1",{Id:"{!Account.Id}"});
    if(methodSelection == '2')
        var result = sforce.apex.execute("Utilities","Method2",{Id:"{!Account.Id}"});
}

Hope this helps.

1
  • I like, but dropdown/checkboxes not possible?
    – sfdcFanBoy
    Commented Jan 19, 2017 at 6:18
2

Yes you can implement the complete functionality in custom button. I can think of two solutions as below.

You can create an HTML element with different buttons based on your requirement. And then use Salesforce SimpleDialog() to show that element in a popup. Here is a link describing how to use SimpleDialog() in a page. http://blogforce9.blogspot.in/2014/01/simple-popup-in-salesforce-standard-vf.html

If you want a jQuery solution you can use jQuery Dialog to show HTML element inside a modal dialog. Please refer this SFSE link for more details on how to use jQuery Dialog in a standard page Open jQuery Dialog from Custom Button.

Update: jQuery Dialog can be used to show any HTML including VF page. The below example describes how to customize jQuery Dialog based on the output of webservice call from a OnClick JavaScript button.

OnClick JS code

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js')} 
{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js')} 
var html = '<div id="dialog" style="display: none" title="Generate Account"><p id="dlgbody">Please wait...</p></div>'; 
var selMethod = '';
var $g = jQuery.noConflict(); 
$g(function() { 
    $g('head').append('<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" type="text/css"/>'); 
    $g('body').append(html); 
    $g("#dialog").dialog({ 
        autoOpen: true, 
        modal: true,
        width: 325,
        height: 100,
        open: function(event, ui){
            $g('.ui-dialog').css('z-index',103);
            $g('.ui-widget-overlay').css('z-index',102);
        },
        buttons: { 
            "Submit": function() { 
                setTimeout(function(){
                    var result = sforce.apex.execute("sarojkbera.Utilities",$g("#methname").val(),{arg:"arg"});
                    alert("Result :"+result);
                }, 100);
                $g(this).dialog("close"); 
            } 
        } 
    }); 
});
setTimeout(function(){
    var result = sforce.apex.execute("sarojkbera.Utilities","method1",{arg:"arg"});
    $g("#dlgbody").html('Please select a method : <select id="methname"></select>');
    var data = JSON.parse(result);
    for(var val in data) {
        $g('<option />', {value: val, text: data[val]}).appendTo($g("#methname"));
    }
}, 100);

Controller Code

global class Utilities {
    webservice static String method1(String arg) {
        String msg ='{ "delmethod":"Delete Account", "updatemethod":"Update Account"}';
        return msg;
    }
    webservice static String delmethod(String arg) {
        return 'Delete Account';
    } 
    webservice static String updatemethod(String arg) {
        return 'Update Account';
    }    
}

Waiting popup

enter image description here

Popup with dropdown

enter image description here

Response from Methods

enter image description here enter image description here

1
  • 1
    @sfdcFanBoy jQuery Dialog is a highly customizable component. You can modify it as per your need. Please find my updated answer, I have tried to explain on how to create a dropdown in jQuery Dialog. But you can create any HTML element like radio or checkbox as per your requirement. Hope this helps.
    – Saroj Bera
    Commented Jan 19, 2017 at 19:28

You must log in to answer this question.

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