0

What I am trying to do is make a page where if i click on button then all accounts with type "prospect" to be shown. below is all my code :

Controller Class

Public class q4{

List<account> acts;

public list<account> getacts(){

return null;
}


public pagereference type_prospect(){
acts = [Select name from account where type = 'Prospect'];
return null;
}

}

VF page code

<apex:page Controller="q4" >

     <apex:form >
     <apex:pageBlock title="Opportunity Record Display">
                <apex:commandButton value="type_prospect" action="{ ! type_prospect }"/>
                  <apex:pageblockSection>

                        <apex:pageBlockTable value="{! acts }" var="acc" title="Account table">

                            <apex:column value="{! acc.type }"/>

                            <apex:column value="{! acc.Name }"/>

                            <apex:column value="{! acc.Industry }"/>


                        </apex:pageBlockTable>


                  </apex:pageblockSection>        

      </apex:pageBlock>

    </apex:form>

</apex:page>

The page first get loaded up with "Opportunity Record Display" title and below the "type_prospect" button.

when i click on the button i expect it to acts to be filled with soql query and then table display the values of acts in each row.

but on clicking it shows

Visualforce Error:

Formula Expression is required on the action attributes.

3 Answers 3

0

try this

public with sharing class q4
{ 
    public list<account> acts {get;set;}

    public pagereference type_prospect(){
    acts = [Select name,Type,Industry  from account where type = 'Prospect'];
    return null;
    }  
}

Page

<apex:page Controller="q4" >

     <apex:form >
     <apex:pageBlock title="Opportunity Record Display">
                <apex:commandButton value="type_prospect" action="{!type_prospect}" rerender="accountBlockID"/>
                  <apex:pageblockSection id="accountBlockID">

                        <apex:pageBlockTable value="{! acts }" var="acc" title="Account table">

                            <apex:column value="{! acc.type }"/>

                            <apex:column value="{! acc.Name }"/>

                            <apex:column value="{! acc.Industry }"/>


                        </apex:pageBlockTable>


                  </apex:pageblockSection>        

      </apex:pageBlock>

    </apex:form>

</apex:page>
4
  • oops didnt see your answer above. Yes it worked Bharath. Dont mind but can you please explain me what i was doing wrong.
    – Niknit
    May 31, 2017 at 10:24
  • I had a simple understanding of this, create a page, create table and columns for the fields to be displayed . in the controller class, fill the list acts when the button is clicked. apart from the spacing mistake where did i go wrong logically,I dont know about rerender or ID in pageblocksection and sharing. and how is acts responding in page getting affected this line public list<account> acts {get;set;}, Thanks if you could explain me this only, ill be happy, i am trying this since yesterday.
    – Niknit
    May 31, 2017 at 10:28
  • You displayed the query field type and Industry in page without querying in the apex code. Also re-render help to get the data updated in the particular page section. Which actually re-render the particular block. Please go through and try a lot...developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/…
    – bharath
    May 31, 2017 at 10:39
  • Teach a man to fish! This answer is still just a code dump with no explanation. That's not how this site is supposed to work. Please take a minute to reread How to Answer.
    – Adrian Larson
    May 31, 2017 at 13:04
0

Please try with the below code where i have removed the getter method and included get set in same line.

Public class q4{

List<account> acts{get;set;}

public pagereference type_prospect(){
acts = [Select name from account where type = 'Prospect'];
return null;
}

}
1
  • Niknit, did it help you? If yes, then you accept it as answer, it will help other members in future in case they have similar issue. Jun 1, 2017 at 6:38
0

I think you need to remove the space between the { and the !.

Try rewriting the action as:

action="{!type_prospect}"/>

Nitish has a good point too - my code should fix your immediate problem, but I think that you won't get your list of accounts until you implement the getter/setter combo as suggested by Nitish.

You must log in to answer this question.

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