1

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?

0

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.

  • This seems like a good idea. I am completely new to JavaScript though. Could you please help me out as to how I will check and update the warningalreadyshown condition? – Sid Feb 16 '16 at 7:54
  • @Sid check my updated answer. – Tushar Sharma Feb 16 '16 at 10:13
  • I tried using what you gave. Still the flag is not resetting back to false. I don't understand why. I have updated the Controller and Page code in my original question. Can you please have a look and let me know what is wrong? – Sid Feb 16 '16 at 10:44
  • @Sid check if record is updated correctly. – Tushar Sharma Feb 16 '16 at 12:21
  • Record is showing both flags to be true and hence the popup is occurring. Its not updating the Messageshown__c to false after first execution! – Sid Feb 16 '16 at 12:37

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.