0

I have an extension that allows users to input information and I am trying to add delete functionality to each record in apex:pageBlockTable. How can I edit my extension code to add this?

What I've tried:

    public with sharing class ecrjcClass{
    
    public class participantWrapper{
        public participant__c par {get;set;}
        public Boolean checked {get;set;}
        
        public participantWrapper(participant__c par, Boolean checked){
            this.par = par;
            this.checked = checked;
        }
    }
    
    public Participant__c record {get;set;}
    public List<Participant__c> allrecords {get;set;}
    public Referral__c ref {get;set;}
    public Boolean checked {get;set;}


    public ecrjcClass(){
        record = new Participant__c();
        allrecords = new List<Participant__c>();
        ref = new Referral__c();
    }   

    //Add a record row
    public pageReference Add_Row(){

        Participant__c anotherRecord = new Participant__c();   
        anotherRecord.Name = record.Name;
        anotherRecord.Last_Name__c = record.Last_Name__c;
        anotherRecord.Age__c = record.Age__c;
        anotherRecord.Victim_Offender__c = record.Victim_Offender__c;
        anotherRecord.Phone__c = record.Phone__c;
        
        insert ref;
        
        anotherRecord.Referral__c = ref.Id;
        //Issue: Need to insert ref first
        allrecords.add(new participantWrapper(anotherRecord, false));
        return null;        
        
    }
    
    //Save and insert records in list
    public pageReference save(){
        
        insert allrecords;
        return null;
            
    }
    
    //Delete button on each record on apex table
public void deleteChecked(){
    List<participantWrapper> wrappers = new List<participantWrapper>();        
    List<Participant__c> dltCase = new List<Participant__c>();
    for(participantWrapper cc: allrecords) {
        if(cc.checked) {
            dltCase.add(cc.par);
        } else {
            wrappers.add(cc);
        }
    }
    delete dltCase;
    allrecords = wrappers;
    }
}

VF page

    <apex:page controller="ecrjcClass" lightningStyleSheets="true">

<!-- Logo and header -->
<br/>
<br/>
<div style="text-align: center">
    <apex:image url="{!URLFOR($Resource.ecrjcLogo)}" height="300" width="300"> 
    </apex:image>            
<br/>
<br/>

<h1>Referral Form</h1>
</div>

<!--Form begins-->
<!--General info-->

<apex:form >
    <apex:pageBlock >
    <apex:pageBlockSection title="General information" columns="2">
        <apex:inputField value="{!Ref.Date__c}"/>
        <apex:inputField value="{!Ref.Referral_Date__c}"/>
        <apex:inputField value="{!Ref.Referring_Agency__c}"/>
        <apex:inputField value="{!Ref.Mailing_Street__c}"/>
        <apex:inputField value="{!Ref.Mailing_City__c}"/>
        <apex:inputField value="{!Ref.Mailing_Country__c}"/>
        <apex:inputField value="{!Ref.Mailing_State_Province__c}"/>
        <apex:inputField value="{!Ref.Mailing_ZIP_Postal__c}"/>
    </apex:pageBlockSection>
    </apex:pageBlock>
    
    <!--Referrer Information-->
    <apex:pageBlock >
    <apex:pageBlockSection title="Referrer Information">
        <apex:inputField value="{!Ref.Referrer_Name__c}"/>
        <apex:inputField value="{!Ref.Referrer_Phone__c}"/>
        <apex:inputField value="{!Ref.Referrer_Email__c}"/>
    </apex:pageBlockSection>
    </apex:pageBlock>
    
    <apex:pageBlock >
    <!--Participant info-->
    <apex:pageBlockSection title="Partipicant Information">
        <apex:inputField value="{!record.Name}"/>
        <apex:inputField value="{!record.Last_Name__c}"/>
        <apex:inputField value="{!record.Age__c}"/>
        <apex:inputField value="{!record.Phone__c}"/>
        <apex:inputField value="{!record.Victim_Offender__c}"/>
    </apex:pageBlockSection>     
    <!--Add participant to record list-->    
        <apex:pageBlockButtons location="bottom">
        <apex:commandButton action="{!Add_Row}" value="Add Participant"/>
    </apex:pageBlockButtons>
    </apex:pageBlock>   
    
            <apex:pageBlock title="Added Participants">
        <apex:pageBlockTable id="Participants" value="{!allrecords}" var="Participant">
            <apex:column>
                <apex:inputCheckbox value="{!Participant.checked}"/>
            </apex:column>
            <apex:column headerValue="First Name" value="{!Participant.par.Name}"/>
            <apex:column headerValue="Last Name" value="{!Participant.par.Last_Name__c}"/>
            <apex:column headerValue="Age" value="{!Participant.par.Age__c}"/>
            <apex:column headerValue="Phone" value="{!Participant.par.Phone__c}"/>
            <apex:column headerValue="Victim/Offender" value="{!Participant.par.Victim_Offender__c}"/>
        </apex:pageBlockTable>
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton action="{!save}" value="Submit"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>
</apex:page>

Thank you.

Edit: Errors:

  • Invalid field checked for SObject Participant__c
  • Method does not exist or incorrect signature: void add(ecrjcClass.participantWrapper) from the type List<Participant__c>
  • Invalid loop variable type expected Participant__c was ecrjcClass.participantWrapper
  • Illegal assignment from List<ecrjcClass.participantWrapper> to List<Participant__c>
1

You need to add the wrapper to the list:

allrecords.add(new caseWrapper(record, false));

Your page block needs to reference the case, and allow for the checkbox:

        <apex:column>
            <apex:inputCheckbox value="{!Participant.checked}" />
        </apex:column>
        <apex:column headerValue="First Name" value="{!Participant.cas.Participant_First_Name__c}"/>
        <apex:column headerValue="Last Name" value="{!Participant.cas.Participant_Last_Name__c}"/>
        <apex:column headerValue="Age" value="{!Participant.cas.Participant_Age__c}"/> 
        <apex:column headerValue="Phone" value="{!Participant.cas.Participant_Phone__c}"/>
        <apex:column headerValue="Victim/Offender" value="{!Participant.cas.Participant_is_victim_or_offender__c}"/>

Finally, you'll want to remove the items after deletion:

//Delete button on each record on apex table
public void deleteChecked(){
    List<casewrapper> wrappers = new List<caseWrapper>();        
    List<Case> dltCase = new List<case>();
    for(caseWrapper cc: allrecords) {
        if(cc.checked) {
            dltCase.add(cc.cas);
        } else {
            wrappers.add(cc);
        }
    }
    delete dltCase;
    allrecords = wrappers;
}
1
  • Thanks. I am getting quite a few errors using this method. Please see updated code Oct 4 '20 at 16:25

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.