Michael H.
Michael H.

Reputation: 47

Passing SF variable value to jQuery value to use in link

I need some assistance on this issue because I have searched online and tried different things for almost three hours and so far nothing has even slightly worked.

Here is what I am trying to do: I built a custom VF page that uses custom css. The page sections are contained in components. In my header component I have a HTML link that is formatted to look like a button. When the user clicks that button then they should be taken back to the Opportunity record they came from. I am unable to retrieve the ID of the opportunity using jQuery and no matter how many times/things I try, this is what the link looks like (/%7B!oppId%7D)

I would appreciate any help on this matter as I seem to be stuck!

Thanks :)

PAGE CODE:

<apex:page standardController="Opportunity" extensions="DEMO_ScheduleOpportunityController" showHeader="false" sidebar="false" standardStylesheets="false">
<head>
</head>
<body>
    <header id="header-section">
        <c:DEMOscheduleHeader opportunity="{!opportunity}" />
    </header>
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js" />
    <apex:includeScript value="{!URLFOR($Resource.DEMO_Schedule, 'js/page.js')}" />
</body>
</apex:page>

COMPONENT CODE:

<apex:component>
<apex:attribute name="opportunity" type="Opportunity" required="true" description="This is Opportuinity used by the Schedule" />
    <a href="#" id="exit" class="btn">Exit</a>
</apex:component>

CONTROLLER CODE:

public with sharing class DEMO_ScheduleOpportunityController {
    public Opportunity opportunity {get; set;}

    public DEMO_ScheduleOpportunityController(ApexPages.StandardController controller) {
        Initialize();
    }
    public void Initialize() {
        opportunity = [SELECT Id FROM Opportunity WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }
}

JS CODE:

$(document).ready(function() {
    var oppId = '{!oppId}'; // the Opportunity Id
    var exitURL = "/" + oppId; // creating and forming the exitURL
    document.getElementById('exit').href = exitURL; // assigning the exitURL to the Exit Button
});

Upvotes: 2

Views: 1632

Answers (1)

Daniel Ballinger
Daniel Ballinger

Reputation: 13537

The apex:includeScript component is for bringing in static resources. That is, it will not merge in variables from the apex controller.

You could set the variable in Javascript directly in the Visualforce page code.

E.g.

<script type="text/javascript">
    var orgId = '{!opportunity.Id}';
</script>

If you add the loadOnReady="true" attribute to the includeScript you will then be able to use orgId in the static resource.

Upvotes: 1

Related Questions