[SalesForce] Display popup message only once using vf page

This question is based on the link:
How to display a message when the trigger finishes?

I want to use the same functionality and have already framed the code which is below:

VF Page:

<apex:page standardController="Object" extensions="displayWarning">
<apex:outputpanel rendered="{!Object.MessageShown__c}"> 
<script>
<!--if({!Object.ShowWarning__c} && {!Object.MessageShown__c})-->
{
    alert("My custom message");
    actfun();
}
</script>
</apex:outputpanel>
<apex:form>
<apex:actionfunction name="actfun" action="{!markread}" />
</apex:form>
</apex:page>

Controller:

public with sharing class displayWarning {
    ApexPages.StandardController controller;

    public displayWarning (ApexPages.StandardController controller) {
        this.controller = controller;
    }

    public void showMessage() {
        Object m = (Object)controller.getRecord();
        m.MessageShown__c = True;
        update m;
    }


    public void markread() {
        Object m = (Object)controller.getRecord();
        m.MessageShown__c = False;
        update m;
    }
}

Trigger:

trigger checkWeekends on Object (after insert) {

    public static Integer daysBetweenExcludingWeekends(Datetime startDate, Datetime endDate) {
    Integer i = 0;

    while (startDate < endDate) {
        if (startDate.format('E') != 'Sat' && startDate.format('E') != 'Sun') {
            i++;
        }
        startDate = startDate.addDays(1);
    }

    return i;
}


    List<Id> newList = new List<Id>();
    Integer normalDays;
    Integer daysExcluded;
    for(Object tsk: Trigger.new) {
        newList.add(tsk.Id);
    }
    system.debug('newList.......'+newList);

    Object newtskList = new Object(); //Change later to bulkify
    newtskList = [SELECT Id, Start_Date__c, Due_Date__c,ShowWarning__c,MessageShown__c FROM Object WHERE Id IN : newList];   
    system.debug('newtskList........'+newtskList);
    newtskList.ShowWarning__c = False;

  normalDays = newtskList.Start_Date__c.daysBetween(newtskList.Due_Date__c);
    system.debug('normal'+normalDays);
    daysExcluded = daysBetweenExcludingWeekends(newtskList.Start_Date__c,newtskList.Due_Date__c);
    system.debug('excluded'+daysExcluded);
    if(normalDays!=daysExcluded) {
         newtskList.ShowWarning__c = True;

        system.debug('showarning flag'+newtskList.ShowWarning__c);
  }
            update newtskList;
 }

Messageshown and ShowWarning are Checkbox fields on the "Object". The basic functionality which I need to achieve is that when the DueDate field is updated and between the duedate and startdate there are weekends, the user should get a popup message just warning him but not an error message – as the record needs to be saved.

I have included the VF Page in a Standard Page layout and made the width and height as 0 as I only need the popup functionality once when the particular date condition is satisfied (from the trigger).

Now once the trigger is called (after the save of record), the page also gets called which in turn calls the controller's function and marks the Messsageshown flag as true which displays my popup correctly.
The problem is the popup gets shown each time the record is opened/updated which I don't want – I only want it to show it once during creation as a Warning and then maybe I will also use it for update functionality as well. Can anyone help me out to achieve this?

Best Answer

you can easily use rendered here.

<apex:page standardController="Obj ect" extensions="displayWarning"> 

    <apex:outputpanel rendered=" {!Object.ShowWarning__c} && {!O bject.MessageShown__c}"> <script>  { alert("My custom message!"); actfun(); </script> </apex:outputpanel>
<apex:actionfunction name="actfun" action="{!markread} />
</apex:page>

Controller

public with sharing class displayWarning {
    ApexPages.StandardController controller;

    public displayWarning (ApexPages.StandardController controller) {
        this.controller = controller;
    }

    public void markread() {
        Object m = (Object)controller.getRecord();
        m.MessageShown__c = false;
        update m;
    }
}

in Next line of alert call a actionfunction and set the messagesShown__c to false so your message will only display once. don't Set this from your Page's action

Check the condition again and update them as required and it will solve your problem.

Related Topic