2

I added a custom account list button via SalesForce setup.

So, when I click on Accounts tab, then on All accounts on selectList, I have all my accounts and on the top my custom button.

I want to know if there is a way to disable this button until at least one account is checked and enable it when user check one or many accounts ?

EDIT : It seems to be impossible to do.

So I did a validation like this : display an alert if no object is selected and redirect to a custom page if at least one object is selected :

var ids = {!GETRECORDIDS($ObjectType.Account)};

if(ids.length > 0) {
    window.location='../apex/MyCustomPage?ids='+ids;
} else {
    alert("No selection !");
}

But the problem encountered is if I have more than one ID, I have an error message saying me than an ID is composed by 15 characters only.

So how can I pass my ID (one or more) to my visualForce page to get them in my controller ?

SF_user
  • 3,616
  • 10
  • 41
  • 56

1 Answers1

2

Yes, you can! If you use a button which executes javascript, you can use require-script as described here http://help.salesforce.com/HTViewHelpDoc?id=customize_functions_i_z.htm#REQUIRESCRIPT to include a javascript e. g. from a static resource. The script will be executed right when the page loads and even if the button is not pressed at all.

So you could set the visibility with plain js or do it the convenient way and include jquery with some logic and event handler to toggel the buttons visibility.

If your respective button does not execute javascript, that's no problem either: Just introduce a second button or link which does and make this one control the visibility of the other on and hide itself always.

edit

There was another (now deleted) answer which pointed to that link:http://abhithetechknight.blogspot.com.au/2013/09/concept-of-standardsetcontroller-for.html?m=1 since it shows how to pass ids to a set controller via a js button it's possibly helpful.

Uwe Heim
  • 28,350
  • 18
  • 115
  • 283
  • I don't use JavaScript. But if I want to add a hidden button that control the visibility, how can I retreive my first button ? I don't have any ID to get it ! – SF_user Jul 28 '14 at 13:03
  • Not necessarily. You can use jquery to scan for the label (which is value attr) or figure out selectors to get the sibling button by it's position in the DOM. Both not perfect but will work. But for most usecases you can simply switch to execute js and use this to do what ever you need, e.g. navigate to a visualforce page. – Uwe Heim Jul 28 '14 at 13:09
  • Ok, thanks. I understand I can have a hidden button that hide my first button on page load. But how can I know when user check one or several checkboxes to show my button ? – SF_user Jul 28 '14 at 13:15
  • Again jquery is your best friend. Although it could be done with pure JS, it's easy with jquery to scan for the checkboxes and determine if one-or-more or none is checked. And again, you won't need the exact IDs. It's even better to keep it loose-coupled, so the whole patter is reusable elsewhere. I use firebug to figure out selectors. Its fun! And yes it theoretically could break if salesforce may change the markup in the future but this happens rarely and even if it's very easy to fix. – Uwe Heim Jul 28 '14 at 13:24
  • you'll probably apply a click() handler on the checkboxes where you toggle the buttons visibility as necessary. – Uwe Heim Jul 28 '14 at 13:32
  • It this way is correct to include my static resource : {!REQUIRESCRIPT('/resource/myResource.js')} ? – SF_user Jul 28 '14 at 13:48
  • 1
    Put the invocation of test() at the end but inside the resource. Verify that the resource is loaded. Check out this http://salesforce.stackexchange.com/questions/38918/end-of-javascript-sidebar-hacks/41840#41840 on how to use require. It will work. – Uwe Heim Jul 28 '14 at 14:10
  • My resource is loaded, I did your trick. I have my custom button with {!REQUIRESCRIPT("/resource/myResource")} And in myResource, I have alert('test'). It works when I did your trick to ensure my resource is loaded but when I go to accounts list I don't have any alert. What am I doing wrong ? – SF_user Jul 28 '14 at 14:31
  • Not sure... try it with a custom link instead. Remove everything from the resource but the alert. Watch your script console for errors. Firebug is my absolute favor for missions like that. And there is an old abandoned plugin for firefox called jsview wich shows you what is really loaded an what not. Dorp me an email @ heim@elastify.eu and I'll share jsview via dropbox if you can't find it. – Uwe Heim Jul 28 '14 at 14:39
  • It's ok now, I did a mistake. I have my alert. Now I will try to hide my button and show it if an account is checked. Thnak you for your help and I will inform you if I can do what I want ! – SF_user Jul 28 '14 at 14:51
  • Good luck, you will make it for sure! I know this stuff to work. Always. – Uwe Heim Jul 28 '14 at 14:54
  • The second button I add to control the visibility of the first one is visible during the page load and then disappear. It's not really smart ! And it's not possible to have a popup like Contact when we try to add a campaign without select an object ? – SF_user Jul 29 '14 at 12:32
  • You didn't try hard enough ;-) look at the link I already provided above ( http://salesforce.stackexchange.com/questions/38918/end-of-javascript-sidebar-hacks/41840#41840 ) - There at my answer and the comment of deva and my reply: Since this happens after an async load, the box is typically visible for a very short time. This could be avoided if you place some inline JS right into the {!REQUIRESCRIPT(data:application/javascript;base64,...)}. To encode rot47.net/base64encoder.html - then it should be totally hidden from the very beginning. It gets trickier - as always with the good stuff! – Uwe Heim Jul 29 '14 at 12:40
  • And with jquery for sure you can get all kinds of popups... even very fancy ones, you just have to code it. – Uwe Heim Jul 29 '14 at 12:43
  • I don't know if I'm doing well what you said but I encode the code below : document.getElementsByName('nameOfMyButton')[0].style = 'display: none'; and I still see my button desappear ! – SF_user Jul 29 '14 at 12:52
  • Can't debug it for you, sorry. Why not change your primary button to be a JS-button? And then go with the Ids as said here http://abhithetechknight.blogspot.com.au/2013/09/concept-of-standardsetcontroller-for.html?m=1 ? From my side I provided you all I can, so now you are on your own. If you try it hard enough, sooner or later it will work. Or you follow Pareto and go with 80% of perfection and ignore minor flaws like the short visibility. It doesn't really do harm, does it? You are still avoiding jquery? Also remember: this a trick only! By default you really can't hide the button at all... – Uwe Heim Jul 29 '14 at 13:15
  • I finally did it with JS and the link you shared. Thank you for your help ! – SF_user Jul 30 '14 at 07:19
  • Cool! But be careful with this technique in the future. It's kind of Pandoras box and you should only use it where it really matters. – Uwe Heim Jul 30 '14 at 07:40