0

I had a VF page created, that shows related list records on the page, and was supposed to be able to delete the related list records. All the page does is remove the record from view instead of deleting it. When you reload the page, it's there again. I can't get the person who created the page to fix it, so I would really appreciate any help fixing it.

VF Page (Edited down)

<apex:page standardController="Reproduction_Management__c" extensions="Dog_ReproductionManagementExtension">
    <apex:form >
        <apex:pageBlock mode="edit">
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!saveAndRedirect}"/>
                <apex:commandButton value="Save & Create Dog Records for Puppies" action="{!saveAndCreatePuppies}"/>
                <apex:commandButton value="Cancel" action="{!Cancel}" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection id="infoSection" title="Information" collapsible="false">
                <apex:inputField value="{!Reproduction_Management__c.Dame__c}"/>
            </apex:pageBlockSection>

            <apex:outputPanel id="section5">
                <!-- Section 5 : Birth_Record__c -->
                <apex:pageBlockSection id="birthRecordSection" title="Birth Records" columns="1" collapsible="false" rendered="{!Reproduction_Management__c.Labor_Start__c <> null}">
                    <apex:commandButton value="Add record" action="{!addBirthRecord}" reRender="birthRecordSection"/>
                    <apex:pageBlockTable value="{!birthRecordList}" var="br" >
                        <apex:column headerValue="Remove" >
                            <apex:commandLink value="Remove" action="{!delBirthRecord}" rerender="birthRecordSection">
                                <apex:param name="birthRowToBeDeleted" value="{!br.recCount}" assignTo="{!birthSelectedRowIndex}"></apex:param>
                            </apex:commandlink>
                        </apex:column>
                        <apex:column headerValue="Puppy Number" >
                            <apex:inputfield value="{!br.birthRecord.Puppy_Number__c}"/>
                        </apex:column>
                        <apex:column headerValue="Birth Date/Time" >
                            <apex:inputfield value="{!br.birthRecord.Birth_Date_Time__c}"/>
                        </apex:column>
                        <apex:column headerValue="Weight (Ounces/Grams)" >
                            <apex:inputfield value="{!br.birthRecord.Weight_Ounces_Grams__c}"/>
                        </apex:column>
                        <apex:column headerValue="Gender" >
                            <apex:inputfield value="{!br.birthRecord.Gender__c}"/>
                        </apex:column>
                        <apex:column headerValue="Color and Marking" >
                            <apex:inputfield value="{!br.birthRecord.Color_and_Markings__c}"/>
                        </apex:column>
                        <apex:column headerValue="Naming Theme" >
                            <apex:inputfield value="{!br.birthRecord.Naming_Theme__c}"/>
                        </apex:column>
                    </apex:pageBlockTable>                    

                </apex:pageBlockSection>
            </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller (Sorry it's so long. The deleteBirthRecord class is near the bottom)

public with sharing class Dog_ReproductionManagementExtension {

    public Reproduction_Management__c reproductionManagement {get; set;}

    public List<BirthRecordWrapper> birthRecordList {get; set;}
    public List<WhelpingMedWrapper> whelpingMedicationList {get; set;}  

    public String whelpingSelectedRowIndex {get; set;} 
    public String birthSelectedRowIndex {get; set;} 

    //Records count
    public Integer whelpingCount = 0;
    public Integer birthCount = 0;

    //Constructor
    public Dog_ReproductionManagementExtension(ApexPages.StandardController controller) {
        //Standard Controller is the dame dog record
        reproductionManagement = (Reproduction_Management__c)controller.getRecord();

        //Initialize the Reproduction_Management__c record
        string dameId = '';
        if(apexpages.currentpage().getparameters().containsKey('dameid')) {
            dameId = apexpages.currentpage().getparameters().get('dameid');
            reproductionManagement.Dame__c = dameId;
        }

        //Lists for whelpingMedication and BirthRecord
        whelpingMedicationList = new List<WhelpingMedWrapper>();
        birthRecordList = new List<BirthRecordWrapper>();

        //default values for row index variables used in apex:repeat
        whelpingSelectedRowIndex = '0';
        birthSelectedRowIndex = '0';

        //If not new record, populate WhelpingMedicationList and BirthRecordList
        if(!string.isEmpty(reproductionManagement.Id)) {
            for(Health_Wellness__c whelpingMed : [Select Id, Medication__c, Other_Medication__c, Dosage_Used__c from Health_Wellness__c where Whelping_Record__c = :reproductionManagement.Id]) {
                whelpingCount = whelpingCount + 1;
                whelpingMedicationList.add(new WhelpingMedWrapper(whelpingMed, whelpingCount));
            }
            for(Birth_Record__c birthRecord : [Select Id, Birth_Date_Time__c, Color_and_Markings__c, Naming_Theme__c, Puppy_Name__c, Puppy_Number__c, Gender__c, Weight_Ounces_Grams__c from Birth_Record__c where Reproduction_Management_Record__c = :reproductionManagement.Id]) {
                birthCount = birthCount + 1;
                birthRecordList.add(new BirthRecordWrapper(birthRecord, birthCount));
            }
        }
    }
    public boolean saveRecords(boolean createDogRecords) { 
        Savepoint sp = Database.setSavepoint();
        try {
            //If Sire field is empty, show error
            if((birthRecordList.size() > 0 || reproductionManagement.X1st_Mating_Date__c <> NULL || reproductionManagement.X2nd_Mating_Date__c <> NULL || reproductionManagement.X3rd_Mating_Date__c <> NULL) && string.isEmpty(reproductionManagement.sire__c) ) {
                ApexPages.Message errorMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Sire field is required when you add Mating Dates.');
                ApexPages.addMessage(errorMsg);
                return false;
            }


            //Upsert Reproduction_Management__c record
            upsert reproductionManagement;


            //Get Whelping Medication Administered Record Type Id
            string recTypeId = 'Need to add Record Type to Health & Wellness object with the Record Type Name of \'Whelping_Medication_Administered\'';
            for(RecordType rt : [Select Id from RecordType where DeveloperName = 'Whelping_Medication_Administered' and sObjectType = 'Health_Wellness__c']) {
                recTypeId = rt.Id;
            }

            //Upsert Whelping Medication List
            List<Health_Wellness__c> wList = new List<Health_Wellness__c>();     
            for(WhelpingMedWrapper ww : whelpingMedicationList) {
                ww.whelpingMedRecord.Whelping_Record__c = reproductionManagement.Id;
                ww.whelpingMedRecord.Dog__c = reproductionManagement.Dame__c;
                if(recTypeId != ''){
                    ww.whelpingMedRecord.RecordTypeId = recTypeId;
                }
                wList.add(ww.whelpingMedRecord);
            }
            upsert wList;

            //List of puppies
            List<Dog__c> puppiesList = new List<Dog__c>();
            //List of Birh Records
            List<Birth_Record__c> bList = new List<Birth_Record__c>();
            for(BirthRecordWrapper bw : birthRecordList) {
                //Add lookup values for related fields
                bw.birthRecord.Reproduction_Management_Record__c = reproductionManagement.Id;
                bw.birthRecord.Dame__c = reproductionManagement.Dame__c;
                bw.birthRecord.Sire__c = reproductionManagement.Sire__c;

                //create new dog record for puppy
                if(createDogRecords && string.isEmpty(bw.birthRecord.puppy_name__c)) {
                    Dog__c dog = new Dog__c();
                    dog.Name = bw.birthRecord.Name;
                    dog.Gender__c = bw.birthRecord.Gender__c;
                    dog.Dame__c = bw.birthRecord.Dame_ID__c;
                    dog.Sire__c = bw.birthRecord.Sire_ID__c;
                    dog.Naming_Theme__c = bw.birthRecord.Naming_Theme__c;
                    dog.Birthdate__c = Date.valueOf(bw.birthRecord.Birth_Date_Time__c);   
                    //add puppy to list
                    puppiesList.add(dog);
                }
                //add birth record to list
                bList.add(bw.birthRecord);
            }
            //Upsert puppies and set puppy lookup value for the related birth records
            if(puppiesList.size() > 0) {
                upsert puppiesList;

                for(integer index=0; index < bList.size(); index++)
                    bList[index].Puppy_Name__c = puppiesList[index].Id;
            }
            //Upsert birth record list
            upsert bList;

            if(puppiesList.size() > 0) {
                //Set puppy name with the auto birth record name
                Map<string, string> puppyNameMap = new Map<string, string>();
                for(Birth_Record__c br : [Select Id, Name, puppy_name__c from Birth_Record__c where Id in :bList]) {
                    puppyNameMap.put(br.puppy_name__c, br.Name);
                }

                List<Dog__c> dogList = new List<Dog__c>([Select Id, Name from Dog__c where Id in :puppyNameMap.keyset()]);
                for(Dog__c puppy : dogList)
                    puppy.Name = puppyNameMap.get(puppy.Id);
                update dogList;
            }

            return true;
        }
        catch (Exception ex) {
            //Rollback in case of error
            Database.rollback(sp);
            reproductionManagement.clone(false, false, false, false);
            ApexPages.Message errorMsg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
            ApexPages.addMessage(errorMsg);
            return false;
        }
    }
    public PageReference saveAndRedirect() {
        //Save only
        if(saveRecords(false))
            return new PageReference('/' + reproductionManagement.Id);

        return null;
    }
    public pageReference saveAndCreatePuppies() { 
        //Save and create puppy records
        if(saveRecords(true))
            return new PageReference('/' + reproductionManagement.Id);

        return null;
    }
    public pageReference addWhelpingMedication() { 
        //add whelping medication record
        whelpingCount = whelpingCount + 1;
        whelpingMedicationList.add(new WhelpingMedWrapper(whelpingCount));
        return null;
    }
    public void delWhelpingMedication()
    {
        //remove whelping medication record
        whelpingMedicationList.remove(Integer.valueOf(whelpingSelectedRowIndex)-1);
        whelpingCount = whelpingCount - 1;
        integer index=1;
        for(WhelpingMedWrapper ww : whelpingMedicationList) {
            ww.recCount = index;
            index++;
        }
    }
    public pageReference addBirthRecord() { 
        //add birth record
        birthCount = birthCount + 1;

        BirthRecordWrapper bw = new BirthRecordWrapper(birthCount);
        birthRecordList.add(bw);
        return null;
    }
    public void delBirthRecord()
    {
        //remove birth record
        birthRecordList.remove(Integer.valueOf(birthSelectedRowIndex)-1);
        birthCount = birthCount - 1;
        integer index=1;
        for(BirthRecordWrapper bw : birthRecordList) {
            bw.recCount = index;
            index++;
        }
    }

    //Wrapper Class for Whelping Medication
    public class WhelpingMedWrapper {
        public integer recCount {get;set;}
        public Health_Wellness__c whelpingMedRecord {get; set;}

        public WhelpingMedWrapper(integer count) {
            recCount = count;
            whelpingMedRecord = new Health_Wellness__c();
        }
        public WhelpingMedWrapper(Health_Wellness__c record, integer count) {
            recCount = count;
            whelpingMedRecord = record;
        }
    }
    //Wrapper Class for Birth Record
    public class BirthRecordWrapper {
        public integer recCount {get;set;}
        public Birth_Record__c birthRecord {get; set;}

        public BirthRecordWrapper(integer count) {
            recCount = count;
            birthRecord = new Birth_Record__c();
        }
        public BirthRecordWrapper(Birth_Record__c record, integer count) {
            recCount = count;
            birthRecord = record;
        }
    }
}

1 Answer 1

1
public void delBirthRecord()
{
    BirthRecordWrapper brw = birthRecordList.get(Integer.valueOf(birthSelectedRowIndex)-1);
    Delete brw.birthRecord;
    //remove birth record
    birthRecordList.remove(Integer.valueOf(birthSelectedRowIndex)-1);
    birthCount = birthCount - 1;
    integer index=1;
    for(BirthRecordWrapper bw : birthRecordList) {
        bw.recCount = index;
        index++;
    }
}
4
  • Glad it helped. Happy to help. Apr 7, 2016 at 7:17
  • Please make sure you check the user has the relevant permissions before performing the delete.
    – Mark Keats
    Apr 7, 2016 at 7:33
  • Is there a way to tell if the record already has an Id? Because if it doesn't have an Id, I'd only want to do the 2nd part of the function, otherwise it throws an error since it's trying to delete something that doesn't exist.
    – Jennifer
    Apr 7, 2016 at 16:57
  • Before delete add a if statement as if(brw.birthRecord.Id != null) Apr 7, 2016 at 17:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .