function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tanmay SahaiTanmay Sahai 

Help triggering a popup alert using checkbox

Hi all,

I am trying to display a pop-up alert with a message on the uncheck of checkbox. I am using Standard controller and need to implement this functionality on the Contact Page Layout. I have a custom checkbo field: Customer__c (default unchecked). What my requirement is that when I check this,it should save normally (this is happenning) but when some user tries to uncheck it, that user should get a pop-up alert with the message.

I have created a VF page for the same and a controller class. I have placed the VF page on the Standard Contact Page Layout keeping the Height as 0. But whenever I uncheck and try to save the record, the pop-up doesn't showup. What am I missing in my code (I don't get any compile/debug errors). Am I doing anything wrong??

Below is the code for VF:

<apex:page standardcontroller="Contact" rendered="{!(Contact.Customer__c)}" >
<script type="text/javascript">
    var msg="Unchecking this box will imply that the user has churned and will automatically de-activate in the Admin Panel.If you dont want that to happen, kindly check the box again";
    function throwalert{
       if ( {!Contact.Customer__c} == false)
       {
          alert(msg); setAlertVal();
       }
     }
     window.onclick = throwalert();
</script>
</apex:page>

This is the code for the controller:

public class PopUpAlert
{
public Contact cont;

  public PopUpAlert(ApexPages.StandardController controller){

   Contact cont = (Contact)controller.getRecord();
    cont=[select id,Customer__c from Contact where Id=: cont.ID];
    
  }
  public void setAlertVal(){
     cont.Customer__c = false;
     update cont;
  }
}

Look forward for quick and valuable comments/recommendations.
Thanks!