0
<apex:page controller="AccountTeamcontroller" showHeader="false" >
<apex:form >
<apex:pageBlock title="Account Team Allocation Request" >

<apex:pageBlock >

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

</apex:pageBlock>



<apex:pageBlock >
<apex:pageBlockTable id="myTable" value="{!accTeam}" var="at">
<apex:column headerValue="Check">
 <apex:inputCheckbox value="{!at.Check__c}"/>
 </apex:column>
 <apex:column headerValue="Function">
 <apex:inputField value="{!at.Function__c}"/>
 </apex:column>
 <apex:column headerValue="Team Role">
 <apex:inputfield value="{!at.Team_Role__c}" />
 </apex:column>
 <apex:column headerValue="Account">
 <apex:param value="{!acc.id}" assignTo="{!at.Account__c}" />
 <apex:inputfield value="{!at.Account__c}"/>
 </apex:column>
 </apex:pageBlocktable>
</apex:pageBlock>


 <apex:commandButton value="Save" action="{!save}"/>
 <apex:commandButton value="Cancel" action="{!cancel}"/>


</apex:pageBlock>

</apex:form>

</apex:page>

Controller

public class AccountTeamcontroller {



public Account acc{get;set;}
public Integer count = 1;



Account_team__c acc1=new Account_team__c();
public list<Account_team__c> accteam{get ; set;}

public AccountTeamcontroller ()
{
   acc=[select id from account where id=:'00128000005PFhw' limit 1];
   accteam=new list<Account_team__c> ();
   accteam.add(acc1);
   for(Integer i=0; i<accteam.size(); i++){
       accteam[i].account__C=acc.id;

   }

}

public PageReference cancel() {
  for(Integer i=1; i<accteam.size(); i++){
 PageReference page = new PageReference('/'+accteam[i].account__c);

page.setRedirect(true);
return page;
}
    return null;
}
public void addMore(){
 accTeam.add(new Account_team__c ());
 }


  public PageReference save() {
   for(Integer i=0; i<accteam.size(); i++)
  {
   insert accteam;
   pagereference pageref=new pagereference('/'+accteam[i].id);

    return pageref;      


    }
  return null;
 }


 public void erase() {

 accteam=[select id,function__c,check__c,team_role__c,account__c from                                           
 Account_team__c ];
 if(accteam[0].check__c==true)
{
  delete accteam;
 }

 }



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

    return null;
}

public String getAccteam() {
    return null;
}

}

Ho do i delete multiple rows in this page?

1

This code will delete any rows that have the Check__c field set when the DELETE button is clicked (though that should also re-render myTable):

public void erase() {
    Account_team__c[] deletes = new Account_team__c[] {};
    for (Integer i = accteam.size() - 1; i >= 0; i--) {
        Account_team__c at = accteam[i];
        if (at.Check__c) {
            deletes.add(at);
            accteam.remove(i);
        }
    }
    delete deletes;
}

You have to start at the end of the list so one remove call doesn't affect the index used in other remove calls of the list. This code both removes the row from the displayed table and removes the row from the database. The collection worked on is the one populated in the constructor and updated from the page - requerying isn't helpful.

  • this deletes the new records only not the existing ones – Rujuta Mokashi Oct 15 '15 at 11:03
  • @RujutaMokashi If you want the page to show (and so be able to edit and delete) the existing Account_team__c objects you should query them in the constructor. – Keith C Oct 15 '15 at 18:03
0

Does this work?

public void erase() {
   accteam=[SELECT id,function__c,check__c,team_role__c,account__c from                                           
   Account_team__c WHERE check_c=TRUE];
   delete accteam;
}
  • no that doesnt work the row still shows on the page – Rujuta Mokashi Oct 15 '15 at 10:58

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.