1

Given next code:

PAGE

<apex:page Controller="Account" showHeader="false">
    <apex:form >
        <apex:pageBlock title="Account Team Allocation Request" >
            <apex:pageBlock >
                <apex:pageBlockButtons >
                    <apex:commandButton value="ADD" action="{!add}"/>
                    <apex:commandButton value="DELETE" action="{!erase}"/>
                </apex:pageBlockButtons>
            </apex:pageBlock>
            <apex:pageBlockSection title="Tranfer Information">
                <apex:inputCheckbox value="{!accteam.Check__c}"/>
                <apex:inputField value="{!accteam.Function__c}"/>
                <apex:inputfield value="{!accteam.Team_Role__c}" />
                <apex:inputfield value="{!accteam.Account__c}"/>
                <apex:repeat >
                    <apex:panelGrid title="uuu" columns="2"></apex:panelGrid>
                </apex:repeat>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

CONTROLLER:

public class Account {
public Integer count = 1;
public Account_team__c accteam{get ; set;}
public Account()
{
accteam=new Account_team__c ();
}

    public PageReference cancel() {
     PageReference page = new PageReference('/'+accteam.account__c);
    page.setRedirect(true);
    return page;
        return null;
    }


    public PageReference save() {
    insert accteam;
    pagereference pageref=new pagereference('/'+accteam.id);
        return pageref;
        return null;
    }


    public PageReference erase() {
           return null;
    }


    public PageReference add() {
     /*count = count+1;
        addMore();   */  

        return null;
    }
}

I have to add a new row of account team when i click on add button Please suggest me the code

0

Considering the requirement, you will have to change the accTeam to List<Account_team__c> so that when you click your add button, a new blank initialized record will be added and also so that the list can hold multiple records of account team object.

While displaying on the page, you should use <apex:pageBlockTable> and iterate over your account team.

<apex:pageBlockTable id="myTable" value="{!accTeam}" var="at">
     <apex:inputCheckbox value="{!at.Check__c}"/>
     <apex:inputField value="{!at.Function__c}"/>
     <apex:inputfield value="{!at.Team_Role__c}" />
     <apex:inputfield value="{!at.Account__c}"/>
</apex:pageBlockTable>

Change your constructor to accTeam= new List<Account_Team__c>() from accteam=new Account_team__c (); or query the existing accounts.

Now you have to add an implementation of addMore(); method

public void addMore(){
     accTeam.add(new Account_team__c ());
}

Also rerender pageBlockTable when you click add:

<apex:commandButton value="ADD" action="{!add}" rerender="myTable"/>

You can take reference from this link: Add multiple records

| improve this answer | |
  • If you still recieve any errors, you can update the question. – manjit5190 Oct 14 '15 at 15:24
  • I need to delete records too how to do that – Rujuta Mokashi Oct 14 '15 at 17:54
  • how to delete multiple records on a visualforce page using a checkbox – Rujuta Mokashi Oct 15 '15 at 5:09

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