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
Travis Lee 1Travis Lee 1 

Visualforce pop-up window notice to user w/o button

I'm attempting to adjust an existing visualforce page from a previous regime by adding a couple conditions to it. Here is the existing code:

<apex:page standardController="Opportunity">
<apex:form >
<script>

window.document.onload = new function(e)
{
  if({!Opportunity.Has_Revenue_Schedule__c==FALSE})
   {
    alert("Please establish a revenue schedule for this opportunity.");
  } 
}
</script>
</apex:form>
</apex:page>

Right now, when you open up an Opportunity and the custom field Has Revenue Schedule is unchecked, a window pops up with an alert asking the user to please establish a revenue schedule for this opportunity. And all I want to do is add a condition that says this alert pops up when Has Revenue Schedule is unchecked AND the Current User's ID is equal to the Opportunity Owner's ID. Thought it should look something like this:

<apex:page standardController="Opportunity">
<apex:form >
<script>

window.document.onload = new function(e)
{
  if({!Opportunity.Has_Revenue_Schedule__c==FALSE} AND {!$User.Id == !Opportunity.OwnerId})
   {
    alert("Please establish a revenue schedule for this opportunity.");
  } 
}
</script>
</apex:form>
</apex:page>

But I'm very green when it comes to Visualforce/Apex and I can't figure out what I'm doing wrong syntactically. It's probably something simple but I just can't seem to see past it. Any help would be much appreciated!
Best Answer chosen by Travis Lee 1
pconpcon
It's probably just easier to do it this way
 
<apex:outputPanel layout="none" rendered="{!AND(NOT(Opportunity.Has_Revenue_Scheduled__c), $User.Id == Opportunity.OwnerId">
    <script>
        window.document.onload = new function (e) {
            alert('hello world');
        }
    </script>
</apex:outputPanel>

This way the javascript won't even render on the page if the conditions aren't right.  Also if you get much more complicated than that with your logic I would suggest moving the logic into the controller.

All Answers

pconpcon
It's probably just easier to do it this way
 
<apex:outputPanel layout="none" rendered="{!AND(NOT(Opportunity.Has_Revenue_Scheduled__c), $User.Id == Opportunity.OwnerId">
    <script>
        window.document.onload = new function (e) {
            alert('hello world');
        }
    </script>
</apex:outputPanel>

This way the javascript won't even render on the page if the conditions aren't right.  Also if you get much more complicated than that with your logic I would suggest moving the logic into the controller.
This was selected as the best answer
Travis Lee 1Travis Lee 1
Works perfect! Thanks pcon. Would you mind explaining how to move this logic into the controller? I read a similar solution in another post but the situation was a lot more chaotic than my own so I got a little bogged down. It would require a custom controller correct?
pconpcon
Great! So you would create a controller extension [1] and then create a method in that extension called something like shouldShowAlert and then use that in your rendered.  For example:
 
public class OpportunityController {
    public Opportunity opp {
        get;
        set;
    }

    public OpportunityController(ApexPages.StandardController stdController) {
        this.opp = (Opportunity) stdController.getRecord();
    }

    public Boolean getShouldShowAlert() {
        if (this.opp == null) {
            return false;
        }

        Boolean isCurrentUserOwner = (UserInfo.getUserId() == this.opp.OwnerId);
        Boolean hasRevenueScheduled = (this.opp.Has_Revenue_Scheduled__c == null) ? false : this.opp.Has_Revenue_Scheduled__c;

        return !hasRevenueScheduled && isCurrentUserOwner;
    }
}
NOTE: This code has not been tested and may contain typographical or logical errors

You'll note that I'm using a ternary [2] on line 17.  This is because Booleans behave really weirdly in Apex because they can be true, false or null.  And we want null to be false.

[1] https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_extension.htm
[2] https://en.wikipedia.org/wiki/%3F:#Java
Travis Lee 1Travis Lee 1
Awesome! Thanks so much, I'm going to test out this solution in my dev org for future reference. I appreciate you taking the extra time!