+ 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.