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
Lukesh KarmoreLukesh Karmore 

I received an error

" create SOQL query in apex classes"
here  challange is create class AccountUtility
My code is :
public class AccountUtility { 
public static void viewAnnualRevenue () {
List<Account> accountsList = [SELECT Name, AnnualRevenue FROM Account]; 
String acctRev;
for(Account a : accountsList) { acctRev = a.Name + ': ' + a.AnnualRevenue;
system.debug(acctRev);
 } } }
I received an error plz help here to clear any one ,
Thank you.
Soundar Rajan PonpandiSoundar Rajan Ponpandi
Hi Lukesh,

Please update your code like below.
 
public class AccountUtility { 

public static void viewAnnualRevenue () {
list<account> accList = new list<account>();
String acctRev;
for(Account acc : [SELECT id,Name, AnnualRevenue FROM Account]){
account a = new account();
a.id = acc.id;
a.AnnualRevenue = acc.Name + ': ' + acc.AnnualRevenue;
accList.add(a);
}
if(accList.size() > 0){ update accList;}
}

}

Please Mark this one as best answer, Kinldy let me know if you are facing any issue.

Regards,
Soundar.
Lukesh KarmoreLukesh Karmore
Thank u soundar,
why you use" id after select "
And any other easy method to write this code it seems hard to me
Lukesh KarmoreLukesh Karmore
And where is system.debug statement .
it shows me error
Lukesh KarmoreLukesh Karmore
 Read thi soundar and reply plzz hope u create it error free thank u
Create class

Name: AccountUtility
Create a method
Name: viewAnnualRevenue
Keywords: public, static, and void
Create a list
Name: accountsList
Create a query and assign the results to a list
Fields: Name and annual revenue (Hint: Use API names, not field names or labels)
Object: Account
Create a for loop that iterates through the query results
Object: Account
List name: accountsList
For each item, concatenate the account name, followed by a colon, followed by the account’s annual revenue: <account name> : <annual revenue>
Store the concatenated string in a variable named acctRev
Print the acctRev variable to the debug log: