You need to sign in to do that
Don't have an account?
Rodolfo Calvo 3
How to get a simple Account?
I have the following code:
But this is for inserting a new account. How can I get an existing account with the same structure?
Something like:
Account master = new Account(Name='Account1') insert master;
But this is for inserting a new account. How can I get an existing account with the same structure?
Something like:
Account acc = account(Where name = :myVariable);
I tried what you said but the system showed me the following error.
There are 2 ways to write the SOQL query.
(1) You can capture the results into a list even variable like below:
List<Account> accList = [Select Id, Name from Account];
or
List<Account> accList = [Select Id, Name from Account LIMIT 1];
You will never get the above error even if there are no records.
(2) You can capture the results into a single object like below when you want to retrieve only one record.
Account acc = [Select Id, Name from Account LIMIT 1];
This will give you an error if there are no records in the system or if you have where condition and if there is no record matching to the where clause.
public Account masterAcc = [Select name from account where name = :sAccount LIMIT 1];
Change it to
public List<Account> masterAccList = [Select name from account where name = :sAccount LIMIT 1];
Regards,
Mahesh
Yes Mahesh is right, with apex you should always use the List<sObject> form for return results from queries as you avoid errors and this is explicitly "bulkifying" your code. The error you're getting states that no return rows were found in the SOQL query meaning it didn't retrieve any records. Can you post all your code please as I notice variable names that aren't declared in that snippet. It could be that the variable you're passing to your WHERE clause is wrong.