+ Start a Discussion
JamuraiJamurai 

Getting Account Name from child Object

Does anyone know how I can get the Account Name and use it for the String Customer without doing a SOQL Query?

I'm trying to populate the String 'Customer' with Account Name, which is a look up from the Escalation__c object.

This is my Apex controller extension:

public with sharing class addCasesToEscalation{
    public Escalation__c theEsc {get;set;}
    public String searchString {get;set;}
    public Case[] shoppingCart {get;set;}
    public Case[] AvailableCases {get;set;}
    
    public String toSelect {get; set;}
    public String toUnselect {get; set;}
    public Decimal Total {get;set;}

    public Boolean overLimit {get;set;}
    public Id EscId {get;set;}
    public Id AcctId {get;set;}
   
    //using this for PDF report attachment
    public String pdfName {get;set;}
    public String Customer {get;set;}
 
    private Case[] forRemoval = new Case[]{};
    private Set<Case> clean = new Set<Case>();

    public addCasesToEscalation(ApexPages.StandardController controller) {

       
        // Get information about the Escalation being worked on
            system.debug(controller.getRecord());
            Escalation__c theEsc = (Escalation__c)controller.getRecord();
            EscId = theEsc.Id;
           
           
        // If cases were previously selected need to put them in the "selected cases" section to start with
        shoppingCart = [select Id, CaseNumber, Subject, Status, Priority, IsClosed, CreatedDate, Escalation__c from Case where Escalation__c=:EscId order by CreatedDate Desc];

            }
   

    public void updateAvailableList() {
   
        // We dynamically build a query string and exclude items already in the shopping cart
        String qString = 'select Id, CaseNumber, Subject, Status, Priority, CreatedDate, Escalation__c, IsClosed From Case WHERE AccountId = :AcctId';
       
        // note that we are looking for the search string entered by the user in the name OR description
        // modify this to search other fields if desired
        if(searchString!=null){
            qString+= ' and (Subject like \'%' + searchString + '%\' or CaseNumber like \'%' + searchString + '%\')';
        }
       
        Set<Id> selectedEntries = new Set<Id>();
        for(Case c : shoppingCart){
            selectedEntries.add(c.Id);
        }
       
        if(selectedEntries.size()>0){
            String tempFilter = ' and Id not in (';
            for(Id i : selectedEntries){
                tempFilter+= '\'' + (String)i + '\',';
            }
            String extraFilter = tempFilter.substring(0,tempFilter.length()-1);
            extraFilter+= ')';
           
            qString+= extraFilter;
        }
              
        qString+= ' order by CreatedDate Desc';
        qString+= ' limit 101';
       
        system.debug('qString:' +qString);       
        AvailableCases = database.query(qString);
       
        // We only display up to 100 results... if there are more then we let the user know (see vf page)
        if(AvailableCases.size()==101){
            AvailableCases.remove(100);
            overLimit = true;
        }
        else{
            overLimit=false;
        }
     
    }
   
    public void addToShoppingCart(){
   
        // This function runs when a user hits "select" button next to a product
   
        for(Case c : AvailableCases){
            if((String)c.Id==toSelect){
                c.Escalation__c = EscId;
                shoppingCart.add(c);
                break;
            }
        }
       
        updateAvailableList(); 
    }
   

public PageReference removeFromShoppingCart(){
   
        // This function runs when a user hits "remove" on an item in the "Selected Cases" section
   
        Integer count = 0;
        for(Case ce : shoppingCart){

            if((String)ce.Id==toUnselect){
               
                if(ce.Id!=null&&ce.Escalation__c != null){
                    ce.Escalation__c = null;
                    forRemoval.add(ce);}
                shoppingCart.remove(count);
                break;            
            }
            count++;
        }
       
        updateAvailableList();
       
        return null;
    }
   
    public PageReference onSave(){
   
        // If previously selected devices are now removed, we need to delete them
        if(forRemoval.size()>0)
            //this is a somewhat hackey way to clean the list just in case there are duplicate cases in the list
            clean.addAll(forRemoval);
            forRemoval.clear();
            forRemoval.addAll(clean);
            update(forRemoval);
   
        // Update cases in shoppingCart
            if(shoppingCart.size()>0)
                update(shoppingCart);
       
        // After save return the user to the Escalation
        return new PageReference('/' + EscId);
    }
   
    public PageReference onCancel(){

        // If user hits cancel we commit no changes and return them to the Escalation  
        return new PageReference('/' + EscId);
    }
   
   //pdf attach extension
    public PageReference savePdf() {

    PageReference pdf = Page.escalationReport;
    // add parent id to the parameters for standardcontroller
    pdf.getParameters().put('id',EscId);

    // create the new attachment
    Attachment attach = new Attachment();

    // the contents of the attachment from the pdf
    Blob body;

    try {

        // returns the output of the page as a PDF
        body = pdf.getContent();

    // need to pass unit test -- current bug   
    } catch (VisualforceException e) {
        body = Blob.valueOf('Something went wrong');
    }
       
    pdfName = Customer + ' escalation report as of '+String.valueOf(System.today());
    attach.Body = body;
    // add the user entered name
    attach.Name = pdfName;
    attach.IsPrivate = false;
    // attach the pdf to the account
    attach.ParentId = EscId;
    insert attach;

    // send the user to the account to view results
    return new PageReference('/'+EscId);

  }
   
}
Best Answer chosen by Jamurai
willardwillard
You can also do <apex:outputText value = "{!Escalation__c.Account__r.Name}" rendered="false"/>
Then within your controller class you can do something like
<pre>
public string accountName {
  get {
    return theEsc.Account__r.Name;
  }
}
</pre>

All Answers

willardwillard
OK - you posted a lot of code (i'd snip it down to the relevant parts for next time to get better responses), but if you're trying to get the account name from Case, then you can just do 
<pre>
select Account.Name from Case
</pre>
Which you can then access from your Case object like so:
<pre>
Case myCase;
// some code to populate myCase
myCase.Account.Name;
</pre>
JamuraiJamurai
Thanks @willard. Duly noted on snipping it down the the relevant parts.

I guess what I was asking if there's a way to do something like getParameter() to get the related Account Name or do I HAVE to use a Select statement?
willardwillard
If you just want to display the name, then you can just do it straight within your VF page:
<pre>
<apex:outputField value="{!Escalation__c.Account__r.Name}"/>
</pre>

Is that what you're looking for?
JamuraiJamurai
Thanks for the reply. No, I was trying to populate a string in the controller extension apex class. Looks like I need to make a select statement and some mumbo jumbo. Thanks again!
willardwillard
You can also do <apex:outputText value = "{!Escalation__c.Account__r.Name}" rendered="false"/>
Then within your controller class you can do something like
<pre>
public string accountName {
  get {
    return theEsc.Account__r.Name;
  }
}
</pre>
This was selected as the best answer