2

I am trying to find a workaround for an issue using Javascript and Visualforce, but I am not very familiar with Javascript. Essentially, when a user creates a new Opportunity or tries to edit an existing Opportunity whose related Account is a certain type and a particular field on the Opportunity is empty, the user should be re-directed to a Visualforce page. Otherwise, they should just be directed to the standard edit page for the Opportunity. I am doing it this way rather than replacing the Edit button with a VF page because I want to also maintain the ability of in-line editing on the Opp, which cannot be done if the edit button is overridden. So I am trying to embed the VF page below in my Opp to fire whenever the Opp is edited and the conditions are met. It's not working, as it doesn't do anything. Does anyone see an issue why it would not pull up the VF page?

<apex:page standardController="Opportunity">
    <apex:includeScript value="/soap/ajax/34.0/connection.js" />
    <script type="text/javascript">
        var AgeAdv = new Object();

        AgeAdv.openWizard = function(){
            if(
                {!(Opportunity.Account.Type == 'Ag') &&
                ISBLANK(Opportunity.Ad__c) &&
                CONTAINS(Opportunity.RecordType.Name,'New')
                }
            )
                {
                window.parent.location.href = "/apex/OpportunityEditOverride?Id={!Opportunity.Id}";                    );
                }else
                {
                window.parent.location.href = "/{!Opportunity.Id}/e?retURL=%2F{!Opportunity.Id}";
                }
            return this;
        };
        AgeAdv.openWizard();
    </script>
</apex:page>
5
  • 1
    As I read your code you have defined a function but have not invoked it. I also don't really follow how you intent to get this "to fire whenever the Opp is edited".
    – Keith C
    Mar 3, 2016 at 20:14
  • @KeithC -- I actually just re-tooled some Javascript that was written for another page update. I did miss one line, which I've added in above. However, the re-direct is still not happening. I don't intend to invoke the Javascript upon the "Edit", but when edits are saved to then re-direct as appropriate.
    – J. Neilan
    Mar 3, 2016 at 20:23
  • 1
    Makes sense - that is the function invocation (call). You have a syntax error causing ); over on the far right of your post; if that is in your page it will stop the JavaScript from working. Check your browser's console for JavaScript errors.
    – Keith C
    Mar 3, 2016 at 20:29
  • @Keith C -- Thanks, I didn't see that extraneous parenthesis, however, it didn't make a difference. It's still not working.
    – J. Neilan
    Mar 3, 2016 at 20:35
  • If this is Visualforce embedded in a standard layout page then as they are served from different domains the Visualforce can't change the href of the standard layout page AFAIK.
    – Keith C
    Mar 3, 2016 at 20:45

1 Answer 1

0

I have updated your code and it works in my DE. The code:

<apex:page standardController="Opportunity">
    <apex:includeScript value="/soap/ajax/34.0/connection.js" />
<script type="text/javascript">
        var AgeAdv = new Object();

        AgeAdv.openWizard = function(){
            if(
                {!(Opportunity.Name == 'D6') 
                }
            )
                {
                window.parent.location.href = "/apex/OpportunityEditOverride?Id={!Opportunity.Id}";                    
                }else
                {
                window.parent.location.href = "/{!Opportunity.Id}/e?retURL=%2F{!Opportunity.Id}";
                }
            return this;
        };
        AgeAdv.openWizard();
    </script>
</apex:page>

Output: When I open an Opportunity with name D6, I get redirected to https://ServerName/apex/OpportunityEditOverride?Id=00628000007wcxBAAQ

When I open an Opportunity with name D1, the standard Opportunity page opens https://ServerName/00628000007wcDAAAY/e?retURL=%2F00628000007wcDAAAY

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .