You need to sign in to do that
Don't have an account?
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!
<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!
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
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.
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