You need to sign in to do that
Don't have an account?
Shruthi 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.
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.
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
Try this -
Replace:
Accountlist.add(new Account(a));
With:
Accountlist.add(new account(name = a.name));
Thanks.
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
You can say simply Accountlist.add(a);
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
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). :)