Suyash Sinha
Suyash Sinha

Reputation: 1

View operation in salesforce using apex and visualforce

Below are the code of my VF and controller class The save and cancel method works fine but my search method is not working correctly. After saving the objects when i click the view button it says to enter the field name instead of showing the list of objects. VF code

<apex:page controller="c5">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection columns="1">
            <apex:inputField value="{!a.Name}"/>
            <apex:inputField value="{!a.First_Name__c}"/>
            <apex:inputField value="{!a.Last_Name__c}"/>
            <apex:inputField value="{!a.Email__c}"/>
            <apex:inputField value="{!a.Contact_Number__c}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
        <apex:commandButton value="save" action="{!save}"/>
        <apex:commandButton value="cancel" action="{!cancel}"/>
        <apex:commandButton value="view" action="{!search}"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>
</apex:page>

controller class

public class c5
{

    public PageReference cancel() 
    {
        PageReference pageRef = new PageReference('/apex/Reg');
        pageRef.setRedirect(true);
        return pageRef;

    }

    List<Register__c> b = new List<Register__c>();
    Register__c a = new Register__c();

    public Register__c geta(){
    return a;
    }

    public PageReference save(){
    insert a;
    return null;
    }

    public List<Register__c> getb(){
    return b;
    }
    public PageReference search(){
    b = (List<Register__c>)[select Name, First_Name__c, Last_Name__c, Email__c, Contact_Number__c  from Register__c];
    return null;
    }

}

Upvotes: 0

Views: 127

Answers (1)

Tiaan Swart
Tiaan Swart

Reputation: 624

You're assigning the result of your search to the property b but you don't display / use this property in the VisualForce page.

Try something like this:

<apex:page controller="c5">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection columns="1">
            <apex:inputField value="{!a.Name}"/>
            <apex:inputField value="{!a.First_Name__c}"/>
            <apex:inputField value="{!a.Last_Name__c}"/>
            <apex:inputField value="{!a.Email__c}"/>
            <apex:inputField value="{!a.Contact_Number__c}"/>
        </apex:pageBlockSection>
        <apex:pageBlockButtons >
            <apex:commandButton value="save" action="{!save}"/>
            <apex:commandButton value="cancel" action="{!cancel}"/>
            <apex:commandButton value="view" action="{!search}" rerender="bData"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection columns="1" id="bData">
            <apex:dataTable value="{!b}" var="theSObject" rendered="{!NOT(ISBLANK(b))}">
                <apex:column value="{!theSobject.Name}"/>
            </apex:dataTable
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

Upvotes: 0

Related Questions