function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sowjanya SarideSowjanya Saride 

Can I get Account Name using AccountID of a CASE?

Hello All,
As a newbee, trying to debug Account Name of Cases whose status = NEW with the help of Case.AccountID in logs the following:
My approach is as below:
public class NewCaseListController {

    public List<Case> getNewCases()
    {
        String n = 'NEW';
        List<Case> results = [SELECT ID, 
                              CaseNumber, 
                              Status ,
                              AccountId    
                              from CASE
                              WHERE Status=:n];
        System.debug(results.size());
        for(Case c : results)
        {
         System.debug('CASE ID = '+ c.Id); 
         System.debug('CASE NUMBER = '+ c.CaseNumber);
         System.debug('Account ID = '+ c.AccountId);
         System.debug('CASE STATUS = '+ c.Status);
        }

        return results;
    }
}

My attempt failed.
Can I get Account Name using AccountID of a CASE? If so, Please please do enlighten how this can be done?

Thanks in Advance.
-LS
Best Answer chosen by Sowjanya Saride
Shaik Naga janiShaik Naga jani
Hi Sowjanya,
Case object is the child of account, you can write the child to parent SOQL to get the parent data.
Check below Example
Child to Parent Query
List<Case> results = [SELECT ID, CaseNumber, Status ,AccountId, Account.Name, Account.Industry from CASE WHERE Status= 'New'];
for(Case caseiterator : results) {
     System.debug('Account Name =====>' +caseiterator.Account.Name);
}
Parent to Child Query
list<Account> lstAccs = [Select id,Name, (Select id, CaseNumber, Status, AccountId From Cases) from Account];
Kindly mark this as solved if the reply was helpful.
Thanks
Shaik

All Answers

Shaik Naga janiShaik Naga jani
Hi Sowjanya,
Case object is the child of account, you can write the child to parent SOQL to get the parent data.
Check below Example
Child to Parent Query
List<Case> results = [SELECT ID, CaseNumber, Status ,AccountId, Account.Name, Account.Industry from CASE WHERE Status= 'New'];
for(Case caseiterator : results) {
     System.debug('Account Name =====>' +caseiterator.Account.Name);
}
Parent to Child Query
list<Account> lstAccs = [Select id,Name, (Select id, CaseNumber, Status, AccountId From Cases) from Account];
Kindly mark this as solved if the reply was helpful.
Thanks
Shaik
This was selected as the best answer
Sowjanya SarideSowjanya Saride
Thank you Shaik!