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
kiran.ykiran.y 

Doubt regarding collection List

Hi,

 

i am new to salesforce.

 

i have doubt on collection list

 

1. I need to retrieve records from an object(Ex: Account)

2. Then I have to add those records  to collection LIST dynamically one by one

 

i have use below code But it's giving error So i don't know how to achive it

 

list<string> s = new list<string>();
for (Account tmp : [SELECT name FROM Account order by name])

{
        s.add(tmp);// to add records to list
    system.debug(tmp);
 
}

 

sanjdevsanjdev

Hi ,

 

Try this

 

List<Account> listAccount = new list<Account>();
for (Account tmp : [SELECT name FROM Account order by name])

{
        listAccount.add(tmp.name);// to add records to list
    system.debug(tmp);
 
}

 

 

If the above ans helps you, mark it as resolved.

 

Cheers

Sanj

yvk431yvk431

The list declaration should be of type string only, if you wanted to add Account's Name, if you want to add the whole record then it should be List<Account>

 

 

--yvk

Praveen_K.ax1208Praveen_K.ax1208

just try this,

 

List<Account> Acc=new List<Account>();

Acc=[SELECT name FROM Account order by name];

 

Now "Acc" will have the complete list of accounts.

 

Thanks,

Praveen K.