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
Shruthi GM 4Shruthi GM 4 

Am facing the following error!

public List<Account> Accountlist { get; set; }

public SelectOrganization(ApexPages.StandardSetController controller) {

    }

public List<Account> getAccounts() {
if(Accountlist == null) {
Accountlist = new List<Account>();
for(Account a: [select Id, Name from Account limit 25]) {

// As each Account is processed we create a new Account object and add it to the accountList
Accountlist.add(new Account(a));
}
 return Accountlist;

}


    }
Error:SObject constructor must use name=value pairs at line 16 column 17
Please help.
What needs to be done.
 
Best Answer chosen by Shruthi GM 4
Amit Chaudhary 8Amit Chaudhary 8
update your code like below

Accountlist = new List<Account>();
for(Account a: [select Id, Name from Account limit 25])
{
    Accountlist.add( a );
}
 return Accountlist;

Let us know if this will help you

All Answers

JP2016JP2016
Hi,

Try this - 

Replace:
Accountlist.add(new Account(a));

With:
Accountlist.add(new account(name = a.name));

Thanks.

 
Amit Chaudhary 8Amit Chaudhary 8
update your code like below

Accountlist = new List<Account>();
for(Account a: [select Id, Name from Account limit 25])
{
    Accountlist.add( a );
}
 return Accountlist;

Let us know if this will help you
This was selected as the best answer
kiranmutturukiranmutturu
Accountlist.add(new Account(a) this is the line where you need to change .

You can say simply Accountlist.add(a);
ShivaKrishna(Freelancer)ShivaKrishna(Freelancer)
Hi Shruthi,

I am not sure on your requirement but one of the below approach can help you. 

Accountlist.add(new Account(a)); this is the line causing the issue as you are trying to create new Acccount instance where constructor does not support account instance inside.

Approach 1 :
if you really want to create a new list of account then do like this
for(Account a: [select Id, Name from Account limit 25])
{
Account acc = new Account();
acc = a;
Accountlist.add( acc);
}

Approach 2 :
if you are just want to return list of queried records or doing trail and error on apex then you can just do like this

for(Account a: [select Id, Name from Account limit 25])
{
Accountlist.add( a);
}

let me know, if it helps you or need any help  :)

shiva.sfdc.backup@gmail.com
 
Allen li 3Allen li 3
Agreed with above answers. Note: we declare an object instance by New Account(Name='xxx', OtherField='xxx').

FYI, you can try a new salesforce development IDE named Metaforce on chrome application platform. it's a great tool for coding, deploying, etc.. you can try to search and install it in chrome webstore or click this link (https://chrome.google.com/webstore/detail/metaforce/hhnkaakhlhngcdckdiogpkjihnmgodep). :)
Salesforce Development Tool