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
Susan ThomlinsonSusan Thomlinson 

Creating an Apex class that return Account objects challenge?

I am having issues with Creating  an Apex class that returns Account objects for the Mapping.net concepts challenge.  I am a VB and Javascript programmer and I can't seem to get the syntax right.   
public class AccountUtils {
    
    public static void accountsByState(String st){
       List<String> accts = [SELECT Id, Name FROM Account WHERE billingState = :st];
 }
  
}
I believe the first part is correct, but I cannot seem to get a return value.  I am confused by the constructors.  I have tried the documentation but I can't get a return value.  Please help.  I need to get this challenge complete.
 
Best Answer chosen by Susan Thomlinson
Mahesh DMahesh D
Hi Susan,

You can try this:
 
public class AccountUtils {
    public static List<Account> accountsByState(String st){
       List<Account> acctList = [SELECT Id, Name FROM Account WHERE billingState = :st];
	   return acctList;
	}
}


Please look into the below useful links:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining_methods.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining.htm

http://www.tutorialspoint.com/apex/apex_methods.htm

Please do let me know if it helps you.

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Hi Susan,

You can try this:
 
public class AccountUtils {
    public static List<Account> accountsByState(String st){
       List<Account> acctList = [SELECT Id, Name FROM Account WHERE billingState = :st];
	   return acctList;
	}
}


Please look into the below useful links:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining_methods.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining.htm

http://www.tutorialspoint.com/apex/apex_methods.htm

Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer
Susan ThomlinsonSusan Thomlinson
That worked. I messed up by not using the List in the constructor. Thank you!!!
Mahesh DMahesh D
Thanks good Susan, Please mark it as resolved so that it will be helpful to others in the future.

Regards,
Mahesh
Susan ThomlinsonSusan Thomlinson
How to I mark this as resolved? I am very new to the forum.
Mahesh DMahesh D
You just need to select the Best Answer, that will automatically marked it as resolved.

Regards,
Mahesh
Andrew EversleyAndrew Eversley
Thanks to Mahesh D and Susan Thomlinson, 

I'm pretty new to the development arena myself and I was having issues on how to proceed on this module. I thank you Mahesh for providing some guidance and Susan for posting the question. I'm in the learning and knowing phase here lol. 
Verma, ManishVerma, Manish
Hi,

Try this code, it works for me.
 
public class AccountUtils {
    
    public static List<Account> accountsByState (String accountState){
        
        List<Account> searchedResults = [SELECT Name FROM Account WHERE BillingState= :accountState];
        
        return searchedResults;
    }

}

Hope this helps!!
Kavya N 9Kavya N 9
While trying out this challenge I came across this error which stopped me from completing the challenge.
User-added image
When I looked into the page layout of the Account object its seen that oly 'Account Name' is a required field and also i dont have any validation rule that is active which could be possibly causing this error.

Kindly Help ?

Regards,
Kavya.N
Kishan MalepuKishan Malepu
Hi Kavya,
 Unable to view your Error Message.Can you check for Tiggers on Account object which are in active mode?
If it is in active mode then de activate it and try to verify the challenge.

Thanks,
Kishan
Sahil Bhandekar 4Sahil Bhandekar 4
Hi Kavya, 

Try  Bellow Code
public class AccountUtils
{
    
    public static List<Account> accountsByState (String state)
    {
        return [SELECT Id,Name From Account WHERE BillingState =:state];
    }
    
}

 
Dhanik Lal SahniDhanik Lal Sahni
Use this 
 
public class AccountUtils {
    public static List<Account> accountsByState (String state)
    {
        return [SELECT Name From Account WHERE BillingState =:state];
    }
}

 
Manmeet Singh 52Manmeet Singh 52
Not working
 
Nandigam RajeshNandigam Rajesh
Hi Manmeet,
it may helps you:

public class AccountUtils {
    public static List<Account>accountsByState(String st){     List<Account> acctList =[SELECT Id, Name FROM Account WHEREbillingState = :st];
    return acctList;
   }
}

 
Manmeet Singh 52Manmeet Singh 52
Thanks for your response
Manmeet Singh 52Manmeet Singh 52
public class AccountUtils {
    public static List<Account> accountsByState (String accountState){
        List<Account> searchedResults = [SELECT Name FROM Account WHERE BillingState= :accountState];

         

        return searchedResults;

    }

}

This works fine for meHope It will work fine for you
Manmeet Singh 52Manmeet Singh 52
Thanks to all Salesforce Developer that help me in achieving my goal
KapavariVenkatramanaKapavariVenkatramana
Thanks to all Salesforce Developer that help me in achieving my goal.Thanks a lot.
Meghraj KurmiMeghraj Kurmi
public class AccountUtils  {
    public static list<Account> accountsByState (String state){
        list<Account> act=[select ID, Name from account where BillingState=:state];
        return act;
        }
}
Muralikrishnan B RMuralikrishnan B R
@meghraj, i am still getting this error 
Executing the 'accountsByState' method on 'AccountUtils' failed. Make sure there are no required fields other than Name for the Account object, the method exists with the name 'accountsByState', is public and static, accepts a String and returns a List of Account objects.
Munkhtegsh MunkhbatMunkhtegsh Munkhbat
Hi Muralikrishnan B R, 

I was having a similar issue. It solved after I saved my class in the Dev Console. (Go to File -> Save)
Qiuzhen CaiQiuzhen Cai

Hi Everyone I just use the same code as above but  when I check following errors popped out:

There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, A country/territory must be specified before specifying a state value for field: [BillingState]

My class is as follows:

public class AccountUtils {
    public static List<Account> accountsByState(String stateAbr) {
        List<Account> listOfAccounts = [Select Id, name from Account where BillingState = :stateAbr]; 
        return listOfAccounts;
    }
}

Is there any problem with my environment or something? Thanks!

Qiuzhen CaiQiuzhen Cai
I got above errors solved by creating a new play ground, so may be its environmental issues regarding languages.
Julio OrtizJulio Ortiz
Hi, how to solve ko in  Create an Apex class that returns Account objects?,  When validating the challenge, it generates the following message:(Executing the 'accountsByState' method on 'AccountUtils' failed. Make sure there are no required fields other than Name for the Account object, the method exists with the name 'accountsByState', is public and static, accepts a String and returns a List of Account objects.)
User-added image


Thank you!!
Julio OrtizJulio Ortiz

Hello again, I have managed to solve the KO, in the end I had to create another (Trailhead Playground).

Best regards.
ani ghoani gho
verified in console challange passed answer
public class AccountUtils {
    public static   List<Account> accountsByState(String State){
        return [SELECT Id, Name FROM Account where BillingState=:state];      
    }
}

 
Ram S 49Ram S 49
2 queries regarding this challenge, need your help folks!
1. I couldn't find the Billing state field in accounts object why?
2. How to test this is working as expected(can anyone provide the debug code) in dev console.
Janis Ian SantiagoJanis Ian Santiago
Hi Ram, I have to agree i couldn't find as well the BillingState under Fields of the object Account. I've found documents though detailing the available fields for Account. This is the returned variable list in the log when i execute the and given the parameter state available in my org. User-added image
Chandan Kumar 270Chandan Kumar 270
Hey Guys, I am getting the below error. Any idea?
Illegal assignment from List<Account> to List<Account>
 User-added image
 
kuldeep Runwal 4kuldeep Runwal 4
Test class working code
@isTest
public class AccountTriggerTest {

    @isTest static void TestCreate200Records(){

        // Test Setup data
        List<Account> accts = new List<Account>();

        // Create 200 new Accounts
        for(Integer i=0; i < 200; i++) {
            Account acct = new Account(Name='Test Account ' + i, BillingState='CA');
            accts.add(acct);
        }

        Test.startTest();

        // Insert the accounts
        insert accts;

        Test.stopTest();

        // After the test runs, assert the results
        for (Account a : accts) {
            System.assertEquals('CA', a.BillingState, 'Billing State should be CA');
        }
    }
}