You need to sign in to do that
Don't have an account?
Adriana Reyes 7
Right now I'm getting this error: "Variable does not exist: Name" on the "detailNames.add(detailList.Name);" line. Can anyone point me in the right direction on how I can achieve this? Thank you!
Save specific parts of a List<SObject> into a List<String>?
public class retrieveInvoice { private final Service_Call__c sc; public retrieveInvoice(ApexPages.StandardController stdController) { this.sc = (Service_Call__c)stdController.getRecord(); } public List<Invoice_Detail__c> detailList {get;set;} public List<String> detailNames {get;set;} public void getInvoices() { detailList = [Select Id, Name from Invoice_Detail__c where Invoice__c = :sc.Invoice__c]; for (Integer i=0; i<detailList.size(); i++) { detailNames.add(detailList.Name); } }So what I'm trying to do is loop through the Invoice Details list and save the name of each one in the list of strings. Then I would do that for other fields as well. For example, I would like to do one for description. The idea is to eventually create a table in a visualforce page, and each list of strings would be a column. My use case requires them to be saved as strings. Otherwise I would just create a table of Invoice Details.
Right now I'm getting this error: "Variable does not exist: Name" on the "detailNames.add(detailList.Name);" line. Can anyone point me in the right direction on how I can achieve this? Thank you!
All Answers
Thank you so much! Both of those seem to have gotten rid of the error. I feel like I'm one step closer. On my VF page I'm getting a "System.NullPointerException: Attempt to de-reference a null object" error on that same line.
This is what the VF page looks like at the moment. Do you know what could be causing this?
You was getting the null pointer Exception because you was using the detailNames var without creating the object. So After creating the object of detailNames in Constructor resolved your issue.
public class retrieveInvoice
{
private final Service_Call__c sc;
public List<Invoice_Detail__c> detailList {get;set;}
public List<String> detailNames {get;set;}
public retrieveInvoice(ApexPages.StandardController stdController) {
detailNames= new List<String>();
detailList=new List<Invoice_Detail__c>();
this.sc = (Service_Call__c)stdController.getRecord();
}
public void getInvoices() {
detailList = [Select Id, Name from Invoice_Detail__c where Invoice__c = :sc.Invoice__c];
for (Invoice_Detail__c ins :detailList) {
detailNames.add(ins.Name);
}
}
}
Let us know if you need more detail