+ Start a Discussion
Adriana Reyes 7Adriana Reyes 7 

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!
 
Best Answer chosen by Adriana Reyes 7
Raj VakatiRaj Vakati
try this code
 
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);
            }  
    }

 

All Answers

Raj VakatiRaj Vakati
Try this
 
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[i].Name);
            }  
    }

 
Raj VakatiRaj Vakati
Or try this
 
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 (Invoice_Detail__c ins :detailList) {
                detailNames.add(ins.Name);
            }  
    }

 
Adriana Reyes 7Adriana Reyes 7
Raj,

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.
 
<apex:page standardController="Service_Call__c" extensions="retrieveInvoice" action="{!getInvoices}">
   <apex:sectionHeader title="Service Call Edit" subtitle="New Service Call"/>

    <apex:form >
        <apex:pageBlock title="Service Call Edit" mode="maindetail">
            <apex:pageBlockTable value="{!detailNames}" var="n">
                <apex:column value="{!n}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>   
</apex:page>

This is what the VF page looks like at the moment. Do you know what could be causing this?
Raj VakatiRaj Vakati
try this code
 
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);
            }  
    }

 
This was selected as the best answer
Raj VakatiRaj Vakati
give me whihc line you are getting error
Adriana Reyes 7Adriana Reyes 7
That last code that you sent me worked! Would you mind explaining to me what you changed?
Raj VakatiRaj Vakati
I initilize the detailList  and detailNames list values .. 
Raj VakatiRaj Vakati
Mark it as solved!
Amit Chaudhary 8Amit Chaudhary 8
Hi Adriana Reyes 7,

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