I have a custom list button on contact with behavior as shown below: enter image description here

And this custom button is added to the contact related list on account enter image description here

When I select 2 or more contacts and click on custom button, it redirects me to the visualforce page with the selected contacts and everything looks good. I am trying to put a validation (may be javascript alert message) that before redirecting to visualforce page, atleast 2 contacts are selectes.

Here is my visualforce page:

<apex:page standardController="Contact" recordSetVar="Contacts">
  <apex:form >
     <apex:pageBlock >
       <apex:pageBlockTable value="{!selected}" var="con">
         <apex:column value="{!con.name}"/>



I too faced this issue

Try the below method

1) Choose the Onclick Javascript in the content source for custom button

2) Paste the below code for execution of javascript

var records={!GETRECORDIDS($ObjectType.**(Your Related List Object - Contact)**)};
if(records.length > 2){
// Call your VF Page like below
// window.open('/apex/VFPAGENAME?id={!Account.Id}','_self');
// Alerts

3) Save the custom button and check

If this solved your problem, Please check this one as accepted answer so it might be easily helpful to someone...

  • Call your VF Page using (sforce.apex.execute) ... can you please let me know how to do this as the documentation says only classes and its methods can be called using "sforce.apex.ececute" – sf_user Apr 19 '16 at 14:37
  • Sorry, my mistake. I updated my answer... please check like that. – Subhash Apr 20 '16 at 5:31
  • Thanks Subhash. Is there any reason for using "_self" ? and also when mentioning window.open, we are kind of hardcoding url and this didnt work for me. Can you please let me know if I am going wrong anywhere. Thanks – sf_user Apr 20 '16 at 14:57
  • I think, you can find more about window.open in this link... w3schools.com/jsref/met_win_open.asp and the URL is not at all a hardcoding url. It will work for sure... Why it is not working for yours. Please let me know if it is showing any specific reason or like error...? – Subhash Apr 21 '16 at 5:34
var accountIds = {!GETRECORDIDS($ObjectType.Account)};

if (accountIds.length > 1 ) {
 //you code
} else {
 alert('At least two accounts should be selected');
  • I tried this ilya. But what recordsetvar is doing I am unable to achieve that. Any suggestions? – sf_user Apr 19 '16 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.