0

My vf page doesn't load correctly. I have to create a button just to load the contents of it. can anyone check it for me? here's the code:

VF PAGE:

<apex:page controller="NightFlexList2Con">
  <style>
    body{background-color:#D3D3D3;
    }
  </style>
  <apex:sectionHeader title="NIGHTFLEX" />

  <apex:form >
    <apex:commandButton action="{!Nigview}" value="Movie"/>
    <c:MyComponent MovName="{!Niglist}">
    </c:MyComponent>

  </apex:form>

</apex:page>

VF COMPONENT:

<apex:component controller="NightFlexList2Con" >
  <apex:attribute name="WatName" type="Watchlist__c[]" description="Display watchlist"/>
  <apex:attribute name="MovName" type="Nightflex__c[]" description="Display watchlist"/>


  <apex:repeat var="mo" value="{!MovName}">
    <div class="row">
      <div class="column" style="background-color:#D3D3D3;">
        <apex:outputField value="{!mo.Poster__c}"/><br />

      </div>
      <div class="column" style="background-color:#D3D3D3;">
        <div style="text-align:center; font-size: 20px;">
        <span style="color:black;font-weight:bold">
              <apex:outputField value="{!mo.Name}"/><br />
            </span>
          <apex:outputField value="{!mo.Genre__c}"/><br />
          <apex:outputText value="{!mo.Description__c}"/>
          <apex:commandButton action="{!showPopup}" value="Add to Watchlist" rerender="tstpopup"/><br />
        </div>
      </div>
    </div>
  </apex:repeat>
</apex:component>

CONTROLLER:

public class NightFlexList2Con {
    public List<NightFlex__C> Niglist { get; set; }
    public List<Watchlist__c>Watlist { get; set; }
    public boolean displayPopup { get; set; }

    public void Nigview() {
        Niglist = [Select Name, Genre__c, Description__c, Poster__c From NightFlex__c];
    }
    public void Watview() {
        Watlist = [Select Name from Watchlist__c];
    }
    public void showPopup() {
        displayPopup = true;

    }
    public void closePopup() {
        displayPopup = false;
    }
    public Watchlist__c WatchlistToCreate { get; set; }
    public NightFlexList2Con() {
        WatchlistToCreate = new Watchlist__c();
    }
    public void save() {
        WatchlistToCreate.ownerId = userinfo.getUserId();
        insert WatchlistToCreate;
        closePopup();
    }
}
3
  • At a quick glance: are you missing a constructor in the controller? There doesn't seem to be any apex that fires as the page loads. – JimG Dec 7 '20 at 13:24
  • can you elaborate more on that constructor? I'm new into salesforce. – xoxo4yaps Dec 7 '20 at 13:39
  • your VF component should have a separate APEX class for its controller – cropredy Dec 7 '20 at 20:05
0

My guess is that you are missing a contructor. IE: when the page loads, there's nothing that fires the Apex and creates an instance of the controller.

See the standard docs for custom controllers here: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_custom.htm

public class MyController {

    private final Account account;

    public MyController() {
        account = [SELECT Id, Name, Site FROM Account 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public Account getAccount() {
        return account;
    }

    public PageReference save() {
        update account;
        return null;
    }
}

Line 5 is the constructor. It basically creates an instance of the page controller, and serves to tie the controller to the page. In the example, it's simply querying for the record. In your case, you'd get whatever data you needed. Or do nothing and wait for the user to click the commandButton.

0

what i did was i put my apex: action into apex:page itself. not separately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.