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
Rodolfo Calvo 3Rodolfo Calvo 3 

How to get a simple Account?

I have the following code: 
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);
Matt GarnettMatt Garnett
Account acc = [Id, Name FROM Account WHERE name = :myVariable LIMIT 1];
Example.
Rodolfo Calvo 3Rodolfo Calvo 3
Look the follwing code: 
public Account masterAcc = [Select name from account where name = :sAccount LIMIT 1];
public void displaySelectedAccountNumbers(){ 
          //selAccountNames.clear();
          //masterAcc = [Select name from Account where name = :sAccount];
          hasSelAcct = false;
          for(AccountWrapperCls cWrapper : acctList){
               if(cWrapper.isSelected)
               {
                    hasSelAcct = true;
                    selAccountNames.add(cWrapper.cAccount.name);
                   
                   	//showMessage = true;
                   	//message = 'The following error has ocurred: \n' + cWrapper.cAccount.id;
                   	//accts.add(cWrapper.cAccount);
                   	//
                   acct = [Select id, name, phone FROM Account where id = :selAccountNames];
                   
                   merge masterAcc acct;
                   Database.MergeResult[] results = Database.merge(masterAcc, acct, false);
                   showMessage = true;
                   message = 'Master Account' + masterAcc + 'duplicate Query ' + acct + 'Selected accounts' +  selAccountNames;
                   
               }

I tried what you said but the system showed me the following error. 
List has no rows for assignment to SObject 
An unexpected error has occurred. Your development organization has been notified.

 
Mahesh DMahesh D
Hi

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
Rahul Kumar 151Rahul Kumar 151
Account acc = [Select Id, Name FROM Account WHERE name = :myVariable LIMIT 1];
Matt GarnettMatt Garnett
Apologies for missing the SELECT part of my query! How funny. But you got the jist of it.

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.