You need to sign in to do that
Don't have an account?
jls_74_tx
Add the parent object to a wrapper class
I have a wrapper class for a custom object Referral__c but I need to add the parent object so only the child object display. The purpose for the wrapper class is so when looking at the ClientDetails__c object, a user can delete child records with a check box. I have displayed the child records previously with an extention, but I'm not able to duplicate the functionality with this wrapper class. Can you help?
__________________________________________________
public class ApartmentDeleteWrapperCLs{
public ClientDetails__c CID {get; set;}
public Referral__c Ref {get; set;}
public List<ApartmentRecordCls> accWrapperRecordList {get;set;}
public ApartmentDeleteWrapperCLs(){
//Fetch Referrals
List<Referral__c> accList= new List<Referral__c>();
accWrapperRecordList = new List<ApartmentRecordCls>();
accList = [select id, Apartment__r.Complex_Name__c, Apartment__r.Warning_Image__c, Apartment__r.Phone__c, Apartment__r.Year_Built__c,
Apartment__r.Submarket__r.Name, Apartment__r.Address__c, Apartment__r.City__c, Apartment__r.State__c, Apartment__r.Zip_Code__c,Apartment__r.Commission_Bonus__c,
Unit__r.Bedrooms__c, Unit__r.Bathrooms__c, Unit__r.Layout__c, Unit__r.Square_Feet__c, Unit__r.Rent__c, Unit__r.CR_6M_Final__c,
Unit__r.CR_6M_Final_Percent__c, Unit__r.CR_12M_Final__c, Unit__r.CR_12M_Final_Percent__c, Unit__r.Status__c,
ClientDetails__r.ID, ClientDetails__r.Referring_Locator__r.ID, ClientDetails__r.First_Name__c
from Referral__c WHERE ClientDetails__c = :CID];
//For loop to set data
if(!accList.isEmpty()) {
for(Referral__c acc: accList){
ApartmentRecordCls arcls = new ApartmentRecordCls();
arcls.isSelected = false;
arcls.accObj = acc;
accWrapperRecordList.add(arcls);
} //end of for loop.
} //end of if condition.
}
/*
Delete Apartment functionality based on the selected records.
*/
public PageReference DeleteApartment(){
List<Referral__c> accToDelete = new List<Referral__c>();
//To hold the unselected Apartment records.
List<ApartmentRecordCls> listUnSelectedRecords = new List<ApartmentRecordCls>();
if(accWrapperRecordList !=null && accWrapperRecordList.size()>0) {
for(ApartmentRecordCls wrapObj : accWrapperRecordList){
if(wrapObj.isSelected == true){
accToDelete.add(wrapObj.accObj);
}else{
listUnSelectedRecords.add(wrapObj);
}
}//end of for.
/*
checking the delete list size and assign the unselected values to
original wrapper list.
*/
if(accToDelete !=null && accToDelete.size()>0){
delete accToDelete;
accWrapperRecordList.clear();
accWrapperRecordList.addAll(listUnSelectedRecords);
}
}else{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.info, 'Records were not there to delete.');
ApexPages.addMessage(myMsg);
}
return null;
}
/* Wrapper class with checkbox and Apartment object.
this is also called as inner class
*/
public class ApartmentRecordCls{
public boolean isSelected {get;set;}
public Referral__c accObj {get;set;}
}
}
__________________________________________________
public class ApartmentDeleteWrapperCLs{
public ClientDetails__c CID {get; set;}
public Referral__c Ref {get; set;}
public List<ApartmentRecordCls> accWrapperRecordList {get;set;}
public ApartmentDeleteWrapperCLs(){
//Fetch Referrals
List<Referral__c> accList= new List<Referral__c>();
accWrapperRecordList = new List<ApartmentRecordCls>();
accList = [select id, Apartment__r.Complex_Name__c, Apartment__r.Warning_Image__c, Apartment__r.Phone__c, Apartment__r.Year_Built__c,
Apartment__r.Submarket__r.Name, Apartment__r.Address__c, Apartment__r.City__c, Apartment__r.State__c, Apartment__r.Zip_Code__c,Apartment__r.Commission_Bonus__c,
Unit__r.Bedrooms__c, Unit__r.Bathrooms__c, Unit__r.Layout__c, Unit__r.Square_Feet__c, Unit__r.Rent__c, Unit__r.CR_6M_Final__c,
Unit__r.CR_6M_Final_Percent__c, Unit__r.CR_12M_Final__c, Unit__r.CR_12M_Final_Percent__c, Unit__r.Status__c,
ClientDetails__r.ID, ClientDetails__r.Referring_Locator__r.ID, ClientDetails__r.First_Name__c
from Referral__c WHERE ClientDetails__c = :CID];
//For loop to set data
if(!accList.isEmpty()) {
for(Referral__c acc: accList){
ApartmentRecordCls arcls = new ApartmentRecordCls();
arcls.isSelected = false;
arcls.accObj = acc;
accWrapperRecordList.add(arcls);
} //end of for loop.
} //end of if condition.
}
/*
Delete Apartment functionality based on the selected records.
*/
public PageReference DeleteApartment(){
List<Referral__c> accToDelete = new List<Referral__c>();
//To hold the unselected Apartment records.
List<ApartmentRecordCls> listUnSelectedRecords = new List<ApartmentRecordCls>();
if(accWrapperRecordList !=null && accWrapperRecordList.size()>0) {
for(ApartmentRecordCls wrapObj : accWrapperRecordList){
if(wrapObj.isSelected == true){
accToDelete.add(wrapObj.accObj);
}else{
listUnSelectedRecords.add(wrapObj);
}
}//end of for.
/*
checking the delete list size and assign the unselected values to
original wrapper list.
*/
if(accToDelete !=null && accToDelete.size()>0){
delete accToDelete;
accWrapperRecordList.clear();
accWrapperRecordList.addAll(listUnSelectedRecords);
}
}else{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.info, 'Records were not there to delete.');
ApexPages.addMessage(myMsg);
}
return null;
}
/* Wrapper class with checkbox and Apartment object.
this is also called as inner class
*/
public class ApartmentRecordCls{
public boolean isSelected {get;set;}
public Referral__c accObj {get;set;}
}
}
Since the page is using a custom controller instead of a standard controller, we will need to set the CID value in the URL string. So I am assuming this page will be called from a custom button or link. So in order for this page to work the calling button or link should look similar to:
So all we need to do is grab the id value from the URL and use that to set the CID member in the class.
Hope that helps!
All Answers
For example, your wrapper class is: And all you need to do is change it to: Where PARENT_TYPE is the parent's type. Then, whenever you are constructing your wrappers just add the parent to the wrapper.
Thank you for trying to help me. This is the first time I've built a wrapper so most likely I have the structure wrong. Let me explain a little more.
I have a custom object ClientDetails__c and each record has many child records in a custom object Referral__c. I have a VF page published on a SalesForce Site for each ClientDetails__c record and at the bottom of the VF page is a list of Referral__c child records. My reason for building the wrapper was so my clients could open the VF page and delete any child records using a check box.
I found an example online to build the wrapper but once it was completed I realized it displays every Referral__c record in the object. I have built the wrapper a couple of different ways, but every time it displays all 9,595 records in the Referral__c record. I know I am not pulling in the ClientDetails__c information correctly but I don't know how to add it.
My previous experience building the VF page is use an extension and the from Referral__c WHERE ClientDetails__c = :CID.id but clearly I'm missing a step building the wrapper.
I added my Parent_Type as your suggested, but it is still showing all of the Referral__c records, what am I missing here?
public class ApartmentRecordCls{
public boolean isSelected {get;set;}
public Referral__c accObj {get;set;}
public ClientDetails__c cdObj {get;set;}
Thank you in advance!
To verify this, just add a debug statement before your accList query to dump the CID value like so:
I am just freehanding this so hopefully there are no syntax errors.
System.NullPointerException: Attempt to de-reference a null object
Class.ApartmentDeleteWrapperCLs.<init>: line 20, column 1
Here are the code changes I made:
If you can post the VF page code and the page's controller code it will help us look into how the wrapper is being constructed.
VF Page:
Controller:
Since the page is using a custom controller instead of a standard controller, we will need to set the CID value in the URL string. So I am assuming this page will be called from a custom button or link. So in order for this page to work the calling button or link should look similar to:
So all we need to do is grab the id value from the URL and use that to set the CID member in the class.
Hope that helps!
You're the best! Thank you so much for your help on a weekend!