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
SFDCDevQASFDCDevQA 

Turn off Pop Up Alert

I'm trying to do a popup alert that just lets people know that a record has been created when they do a certain action.  It's kind of an FYI thing.  So I tried a VF page embedded in the Opp layout and that worked but it showed up everytime they went to the record once it met the criteria.  So then I tried adding a controller to the page that would check a box saying that the alert was shown.  The controller works and checks the box but now the popup doesn't show.  Is there any way to get BOTH of these working as one solution?

Thanks so much!
Amanda

Here's the first VF page I tried which works to show the popup:
<apex:page standardcontroller="Opportunity" rendered="{!Opportunity.stagename=='Commitment' && Opportunity.Opportunity_Rolled__c==True && Opportunity.Alert_Shown__c==False}" >
  <script type="text/javascript">
  {
  window.alert("This Opportunity has Been Rolled to the next applicable Semester");
  }
  </script>
</apex:page>

And here's the second one I tried that will check the box but not actually show the alert.

<apex:page standardController="Opportunity" action="{!markread}" extensions="CloneAlertExtension">
<script type="text/javascript">
if({!Opportunity.stagename=='Commitment' && Opportunity.Opportunity_Rolled__c==True && Opportunity.Alert_Shown__c==False}) {
    window.alert("This Opportunity has Been Rolled to the next applicable Semester");
}
</script>
</apex:page>

public with sharing class CloneAlertExtension {
    ApexPages.StandardController controller;
    public CloneAlertExtension(ApexPages.StandardController controller) {
        this.controller = controller;
    }
    public void markread() {
        Opportunity o = (Opportunity)controller.getRecord();
        o.Alert_Shown__c = true;
        update o;
    }
}
Best Answer chosen by SFDCDevQA
Hargobind_SinghHargobind_Singh
Try this. I've made a few changes:
  • Renamed extension property to Opp1, to avoid confusion with standard controller property-name 
  • calling javascript only if the condition matches, so that alert box is not updated if stagename is not commitment 

<apex:page standardController="Opportunity" extensions="CloneAlertExtension">

	<apex:form>
	<apex:actionFunction action="{!markread}" name="updateStatus"/>
    </apex:form>
<script type="text/javascript">
    if({!opp1.stagename=='Commitment' && opp1.Opportunity_Rolled__c==True && opp1.Alert_Shown__c==False}) {
	    window.alert("This Opportunity has Been Rolled to the next applicable Semester");
		updateStatus(); 
    }
</script>
</apex:page>

public class CloneAlertExtension {

    Public Opportunity opp1      {get;set;}
    public CloneAlertExtension(ApexPages.StandardController controller) {
        ID oppID = controller.getID();

        opp1 = [Select Id, Name, AccountId, Alert_Shown__c, Opportunity.StageName, Opportunity_Rolled__c 
            From Opportunity
            Where Id =:oppID ];
    }

    public void markread() {
        Opportunity o = this.opp1;
        o.Alert_Shown__c = true;
        update o;
    }
}


All Answers

Hargobind_SinghHargobind_Singh
I think by the time your javascript executes and checks Opportunity.Alert_Shown__c==False, the flag has already been set as True. 

Action method is called before the page is rendered, after the constructor, so your flag is already true by the time your script executes. 

You should try to call your action-method using ActionScript after your alert javascript
SFDCDevQASFDCDevQA
I've been trying to do this multiple different ways with no success.  For some reason the first page
<apex:page standardcontroller="Opportunity" rendered="{!Opportunity.stagename=='Commitment' && Opportunity.Opportunity_Rolled__c==True && Opportunity.Alert_Shown__c==False}" >
  <script type="text/javascript">
  {
  window.alert("This Opportunity has Been Rolled to the next applicable Semester");
  }
  </script>
</apex:page>
Is the only one that I can get to show the popup itself.  I found a few examples of people calling action methods on buttons and such but as soon as I try adding my extension into this code it stops working.  And if I take out the action code of the second one that I had it still doesn't show the popup.  Could you give me a more specific example of what you meant by using ActionScript after my alert javascript?  As you can probably tell I'm pretty new to Javascript.

Thanks so much,
Amanda
Hargobind_SinghHargobind_Singh
Here is the updated code:
  • Removed the "Action" from apex:page 
  • Created an ActionFunction named updateStatus that calls markread() in controller
  • Called this ActionFunction from your javascript after the alert. 

So the result would be, that markread would be called after the alert has displayed. 

Hope you get the idea. 




<apex:page standardController="Opportunity" extensions="CloneAlertExtension">
<actionFunction name="updateStatus" action="{!markread}"/>
<script type="text/javascript">
if({!Opportunity.stagename=='Commitment' && Opportunity.Opportunity_Rolled__c==True && Opportunity.Alert_Shown__c==False}) {
    window.alert("This Opportunity has Been Rolled to the next applicable Semester");
}
updateStatus(); 
</script>
</apex:page>

public with sharing class CloneAlertExtension {
    ApexPages.StandardController controller;
    public CloneAlertExtension(ApexPages.StandardController controller) {
        this.controller = controller;
    }
    public void markread() {
        Opportunity o = (Opportunity)controller.getRecord();
        o.Alert_Shown__c = true;
        update o;
    }
}


SFDCDevQASFDCDevQA
Thanks.  I tried this but keep getting
Error: Unknown property 'OpportunityStandardController.markread'
I tried changing my controller class to the below code but it still keeps saying unknown property

public class CloneAlertExtension {
    Public Opportunity opportunity      {get;set;}
   
    public CloneAlertExtension(ApexPages.StandardController controller) {
        this.opportunity = (Opportunity) controller.getRecord();
        opportunity = [Select Id, Name, AccountId, Alert_Shown__c
            From Opportunity
            Where Id =: opportunity.id];
    }

    public void markread() {
        Opportunity o = this.opportunity;
        o.Alert_Shown__c = true;
        update o;
    }
}
Hargobind_SinghHargobind_Singh
Try this. I've made a few changes:
  • Renamed extension property to Opp1, to avoid confusion with standard controller property-name 
  • calling javascript only if the condition matches, so that alert box is not updated if stagename is not commitment 

<apex:page standardController="Opportunity" extensions="CloneAlertExtension">

	<apex:form>
	<apex:actionFunction action="{!markread}" name="updateStatus"/>
    </apex:form>
<script type="text/javascript">
    if({!opp1.stagename=='Commitment' && opp1.Opportunity_Rolled__c==True && opp1.Alert_Shown__c==False}) {
	    window.alert("This Opportunity has Been Rolled to the next applicable Semester");
		updateStatus(); 
    }
</script>
</apex:page>

public class CloneAlertExtension {

    Public Opportunity opp1      {get;set;}
    public CloneAlertExtension(ApexPages.StandardController controller) {
        ID oppID = controller.getID();

        opp1 = [Select Id, Name, AccountId, Alert_Shown__c, Opportunity.StageName, Opportunity_Rolled__c 
            From Opportunity
            Where Id =:oppID ];
    }

    public void markread() {
        Opportunity o = this.opp1;
        o.Alert_Shown__c = true;
        update o;
    }
}


This was selected as the best answer
SFDCDevQASFDCDevQA
Awesome thank you so much!  I should have realized that the double use of opportunity might be what was causing an issue.  Sorry about that.  Thanks so much for helping me get this working.