+ Start a Discussion
Aidel BruckAidel Bruck 

visual force page should display alert only when the field changes values

I wrote the following page and controller. 
The page displays an alert when field equals a specific value.
I want the alert to display when the value is changed to that value.

Here is the page

<apex:page standardController="Opportunity" extensions="OpportunityController"> <script> window.onload = function() { var servicestatus= "{!servstatus}"; var servicename = "{!servname}"; var hasprcomment= "{!hasprcomments}"; if(typeof(servicestatus) != "undefined" && typeof(servicename) != "undefined") { if( hasprcomment== 'false') { alert('Please be aware that you are closing a service that does not have any pr comments in the related account'); } } }; </script> </apex:page>

This is the controller
public with sharing class OpportunityController 
{
  public id serviceID;
    
  
    Public opportunity servRec{get;set;}
    Public account accountRec{get;set;}
    public string Servname{get; set;}
    public string ServStatus{get;set;}
    public boolean hasprcomments{get;set;}
    
    public OpportunityController(ApexPages.StandardController controller) 
    {     
     Serviceid = ApexPages.currentPage().getParameters().get('id');
      if(serviceid != null)
      {
          ServRec= [select id, account_name__c, stagename, Service_status__C from Opportunity where id =:serviceId];
 
         if(servrec.account_name__c!= null)  
       {
         accountRec = [select id,pr_comment__C from account where id =:servrec.account_name__C];
         if(accountrec.pr_comment__C==null&& (servrec.StageName== 'closed'|| servrec.Service_Status__c== 'closed'))
           hasprcomments= false;
         else
           hasprcomments= true;
    
       }  
     }
    
    }

}
ShirishaShirisha (Salesforce Developers) 
Hi Aidei,

Greetings!

Please try the below sample code and change accordingly.
 
<script>
function saveClick() {
var caseStatus = document.getElementById('j_id0:j_id2:CaseStatus');

if(caseStatus.value == 'Closed')
{
    alert("Case is Closed.");
}
}
</script>

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri