0

I have created a lightning app page with type Record Page and override default Opportunities page. I have added a new tab to it's content and added a visualforce page as it component. Please see the image below.

enter image description here

The visualforce page contains codes to display documents and download link. The code is as below.

<apex:page controller="stageDocus">
    <apex:panelGrid columns="5" id="theGrid" width="80%" style="margin:30px auto; text-align:center;">
        <apex:repeat var="sld" value="{!docs}">
            <div style="float:left; width:20%;">
            <apex:panelGroup id="theGroup1">
                <apex:image width="100%" id="theImage1" value="http://icons.iconarchive.com/icons/alecive/flatwoken/512/Apps-Google-Drive-Slides-icon.png"/>
                <apex:outputLink value="/servlet/servlet.FileDownload?file={!sld.Id}" target="_blank" id="theLink1">{!sld.Folder.Name}/{!sld.Name}</apex:outputLink>
            </apex:panelGroup>
            </div>
        </apex:repeat>
    </apex:panelGrid>
</apex:page>

The page is working fine, if I display all the documents and when the folder name is given hard coded.

Our goal is to display the documents under the 'Folder' with name same as the 'StageName' of the opportunity under this tab.

The apex class code I used is a below

public  class stageDocus
{
    public String currentRecordId {get;set;}
    public Opportunity Oppo{get;set;}
    currentRecordId  =  this. ApexPages.currentPage().getParameters().get('Id'); 
    Oppo = [SELECT Id, Name, StageName, Amount FROM Opportunity WHERE Id = :currentRecordId];
    public List<Document> getdocs()
    {
            List<Document> doc = [SELECT Id, Name, FolderId, Folder.Name FROM Document WHERE Folder.Name like :Oppo.StageName];
            return doc;
    }
}

This is returning error

unexpected token: '='

How can I access the Opportunity Id or Stage Name inside this controller? Please help me. I am stuck.

2 Answers 2

3

You can't generally just put statements wherever you want to. As an exception, you can declare an initial value when you declare the variable, but only if you don't use get/set. Usually, we use the "constructor" for initializing the page state. Your code should work as follows:

public  class stageDocus
{
    public String currentRecordId {get;set;}
    public Opportunity Oppo{get;set;}
    public stageDocus() // Default constructor
    {
        currentRecordId  =  this. ApexPages.currentPage().getParameters().get('Id'); 
        Oppo = [SELECT Id, Name, StageName, Amount FROM Opportunity WHERE Id = :currentRecordId];
    }
    public List<Document> getdocs()
    {
            List<Document> doc = [SELECT Id, Name, FolderId, Folder.Name FROM Document WHERE Folder.Name like :Oppo.StageName];
            return doc;
    }
}

However, I strongly recommend against this design. It's usually more practical to use an Extension instead. Your page's first couple lines would change as follows:

<apex:page standardController="Opportunity" extensions="stageDocus">
    <apex:outputText value="{!Opportunity.StageName}" rendered="false" />

The second line causes Visualforce to automatically include the StageName field in the Visualforce's automatic query.

Your class would then change as follows:

public  class stageDocus
{
    ApexPages.StandardController ctrl;
    Opportunity Oppo;

    public stageDocus(ApexPages.StandardController c) // Extension constructor
    {
        ctrl = c;
        Oppo = (Opportunity)c.getRecord();
    }
    public List<Document> getdocs()
    {
            List<Document> doc = [SELECT Id, Name, FolderId, Folder.Name FROM Document WHERE Folder.Name like :Oppo.StageName];
            return doc;
    }
}
3
  • Thank you so much. Will update my code as per your suggestion. :)
    – AeJey
    Commented Mar 16, 2017 at 8:16
  • You could just remove oppo entirely, no?
    – Adrian Larson
    Commented Mar 18, 2017 at 1:57
  • @AdrianLarson It's usually good (but not always) to have a direct reference to the record anyways, since you'll almost always need it at some point. This is a trivial example, mostly copy-pasted and modified as needed, rather than a total rewrite. If I wrote this class to my usual coding conventions, it'd have been 10 lines at most.
    – sfdcfox
    Commented Mar 18, 2017 at 8:09
2

You're trying to set values into variables outside any form of methods/constructor.

Constructors are great to initialize variables, use them ! Their syntax can be found here

Below is the code which can be used for your use case

public  class stageDocus
 {
    public String currentRecordId {get;set;}
    public Opportunity Oppo{get;set;}

 public stageDocus() {
    currentRecordId  =  ApexPages.currentPage().getParameters().get('Id'); 
    Oppo = [SELECT Id, Name, StageName, Amount FROM Opportunity WHERE Id = :currentRecordId];
 }

 public List<Document> getdocs()
 {

        List<Document> doc = [SELECT Id, Name, FolderId, Folder.Name FROM Document WHERE Folder.Name like :Oppo.StageName];
        return doc;
 }
}
0

You must log in to answer this question.

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