You need to sign in to do that
Don't have an account?
Jamurai
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);
}
}
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);
}
}
Then within your controller class you can do something like
<pre>
public string accountName {
get {
return theEsc.Account__r.Name;
}
}
</pre>
All Answers
<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>
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?
<pre>
<apex:outputField value="{!Escalation__c.Account__r.Name}"/>
</pre>
Is that what you're looking for?
Then within your controller class you can do something like
<pre>
public string accountName {
get {
return theEsc.Account__r.Name;
}
}
</pre>