function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Oleg NikitchukOleg Nikitchuk 

VF page with Apex logic

Hi all! This is my first post on this community forum, I am an absolute beginner and currently studying Salesforce development by my own.

I am trying to create a Visualforce page with Positions and the ability to filter by Position status ( Opened, Closed). The Status field should be editable and have a Save button.

Like this:
User-added image
This is my code VF:
<apex:page controller="PositionCustomController" showHeader="true" sidebar="true">
<apex:form>
<apex:pageBlock title="Positions list">
<apex:pageBlockTable value="{!positions}" var="pos">
<apex:inputField value="{!pos.Status__c}">
<apex:commandButton action="{! save}" value="Save"></apex:commandButton>
</apex:inputField>
<apex:column value="{!pos.Name}"></apex:column>
<apex:column value="{!pos.Status__c}"></apex:column>
<apex:column value="{!pos.Opening_date__c}"></apex:column>
<apex:column value="{!pos.Closing_date__c}"></apex:column>
<apex:column value="{!pos.Salary_max__c}"></apex:column>
<apex:column value="{!pos.Salary_min__c}"></apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

Apex:
public with sharing class PositionCustomController{
public List<Position__c> getPositions(){
List<Position__c> results = Database.query(
'SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c from Position__c' );
return results;
}
}

I know this code is shitty, but I am struggling and need help, it is super discouraging at the moment. Thanks!
Best Answer chosen by Oleg Nikitchuk
ShivankurShivankur (Salesforce Developers) 
Hi Oleg,

I have built/implemented similar visualforce page in my dev org with Case object, you can try to use something like below:

My VF page:
<apex:page controller="CaseFilterCustomController" sidebar="true" showHeader="true">
    <apex:form >
        <apex:pageBlock title="My Cases">
            <apex:outputLabel value="View: "/>
            <apex:selectList value="{!filterId}" size="1">
                <apex:actionSupport event="onchange" action="{!processRequests}"  rerender="cases_table"/>
                <apex:selectOptions value="{!items}"/>
            </apex:selectList>
            <apex:pageBlock >
                <apex:pageBlockTable value="{!results}" var="c" rows="50" id="cases_table" >
                    <apex:column headerValue="Case Id">
                        <a target="_parent" href="/{!c.id}">{!c.CaseNumber}</a>
                        <apex:facet name="header">
                            {!$ObjectType.Case.Fields.CaseNumber.Label}
                        </apex:facet>
                    </apex:column>
                    
                    <apex:column >
                        <a target="_parent" href="/{!c.id}">{!c.Subject}</a>
                        <apex:facet name="header">Subject</apex:facet>
                    </apex:column>
                    
                    <apex:column headerValue="Status">
                        <apex:outputField value="{!c.Status}">
                            <apex:inlineEditSupport showOnEdit="OnClick"/>
                        </apex:outputField>
                    </apex:column>
                    
                </apex:pageBlockTable>
                
                <apex:pageBlockButtons html-align="left" location="top">
                    <apex:commandButton value="Save" action="{!save}" />
                </apex:pageBlockButtons>
            </apex:pageBlock>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Apex:
public with sharing class CaseFilterCustomController{
    public List<Case> results {get;set;}
    public String filterId {get;set;}
    private Id accountId;
    public CaseFilterCustomController()
    {
        init();
    }
    public void init() {
        
        results = [select Id, Subject, Status,CaseNumber from Case ORDER BY Status LIMIT 100];   
        
    } 
    public PageReference processRequests()
    {
        //previousSortField = 'CaseNumber';
        //write the query for getting the data based on filterId
        if (filterId == 'All') {
            results = [select Id, Subject, Status,CaseNumber from Case where Status!=null order by CaseNumber desc limit 100];
        } else {
            results = [select Id, Subject, Status,CaseNumber from Case where Status = :filterId order by CaseNumber desc limit 100]; 
        }
        return null;
    }
    
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Open', 'Open'));
        options.add(new SelectOption('Closed', 'Closed'));
        options.add(new SelectOption('All', 'All'));
        return options;
    }
    public PageReference save(){      
        try{ 
            update results;
            
        }
        catch(DmlException ex){
            ApexPages.addMessages(ex);
        }
        init();
        return null;
    }
}
The UI looks as expected by you:
Result

Please modify the same visualforce to fit with your custom object and it should work for you too..

Please mark as Best Answer so that it can help others in the future.

Thanks.

All Answers

ShivankurShivankur (Salesforce Developers) 
Hi Oleg,

I have built/implemented similar visualforce page in my dev org with Case object, you can try to use something like below:

My VF page:
<apex:page controller="CaseFilterCustomController" sidebar="true" showHeader="true">
    <apex:form >
        <apex:pageBlock title="My Cases">
            <apex:outputLabel value="View: "/>
            <apex:selectList value="{!filterId}" size="1">
                <apex:actionSupport event="onchange" action="{!processRequests}"  rerender="cases_table"/>
                <apex:selectOptions value="{!items}"/>
            </apex:selectList>
            <apex:pageBlock >
                <apex:pageBlockTable value="{!results}" var="c" rows="50" id="cases_table" >
                    <apex:column headerValue="Case Id">
                        <a target="_parent" href="/{!c.id}">{!c.CaseNumber}</a>
                        <apex:facet name="header">
                            {!$ObjectType.Case.Fields.CaseNumber.Label}
                        </apex:facet>
                    </apex:column>
                    
                    <apex:column >
                        <a target="_parent" href="/{!c.id}">{!c.Subject}</a>
                        <apex:facet name="header">Subject</apex:facet>
                    </apex:column>
                    
                    <apex:column headerValue="Status">
                        <apex:outputField value="{!c.Status}">
                            <apex:inlineEditSupport showOnEdit="OnClick"/>
                        </apex:outputField>
                    </apex:column>
                    
                </apex:pageBlockTable>
                
                <apex:pageBlockButtons html-align="left" location="top">
                    <apex:commandButton value="Save" action="{!save}" />
                </apex:pageBlockButtons>
            </apex:pageBlock>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Apex:
public with sharing class CaseFilterCustomController{
    public List<Case> results {get;set;}
    public String filterId {get;set;}
    private Id accountId;
    public CaseFilterCustomController()
    {
        init();
    }
    public void init() {
        
        results = [select Id, Subject, Status,CaseNumber from Case ORDER BY Status LIMIT 100];   
        
    } 
    public PageReference processRequests()
    {
        //previousSortField = 'CaseNumber';
        //write the query for getting the data based on filterId
        if (filterId == 'All') {
            results = [select Id, Subject, Status,CaseNumber from Case where Status!=null order by CaseNumber desc limit 100];
        } else {
            results = [select Id, Subject, Status,CaseNumber from Case where Status = :filterId order by CaseNumber desc limit 100]; 
        }
        return null;
    }
    
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Open', 'Open'));
        options.add(new SelectOption('Closed', 'Closed'));
        options.add(new SelectOption('All', 'All'));
        return options;
    }
    public PageReference save(){      
        try{ 
            update results;
            
        }
        catch(DmlException ex){
            ApexPages.addMessages(ex);
        }
        init();
        return null;
    }
}
The UI looks as expected by you:
Result

Please modify the same visualforce to fit with your custom object and it should work for you too..

Please mark as Best Answer so that it can help others in the future.

Thanks.
This was selected as the best answer
Oleg NikitchukOleg Nikitchuk
Shivankur, Thanks a lot!