+ Start a Discussion
santhosh konathala 17santhosh konathala 17 

Hi friends I am new to this environment could you please help me how to fetch records from recycle bin which are deleted recently using Apex code?

list<account> a= new list<account>();
List<Account> acclist = [select id, name,Isdeleted from account  limit 4];
for(account acc:acclist)
{
   a.add(acc); 
}

undelete a;


Above is my code getting error please help me where I was written wrong....
Best Answer chosen by santhosh konathala 17
Ishwar ShindeIshwar Shinde
list<account> a= new list<account>();
List<Account> acclist = [select id, name,Isdeleted from account where isDeleted = true ALL ROWS  limit 4  ]);];
for(account acc:acclist)
{
   a.add(acc); 
}

undelete a;

Using All Rows you can fetch all records i.e. even deleted records. IsDeleted flag helps you to identify is that record is deleted or not. 

Please mark this as best ans if helps!!

All Answers

Ishwar ShindeIshwar Shinde
list<account> a= new list<account>();
List<Account> acclist = [select id, name,Isdeleted from account where isDeleted = true ALL ROWS  limit 4  ]);];
for(account acc:acclist)
{
   a.add(acc); 
}

undelete a;

Using All Rows you can fetch all records i.e. even deleted records. IsDeleted flag helps you to identify is that record is deleted or not. 

Please mark this as best ans if helps!!
This was selected as the best answer
Vigneshwaran GurunathanVigneshwaran Gurunathan
You need to use ALL ROWS clause in your SOQL query to include the records from Recycle bin. Please go through the document here. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_query_all_rows.htm

If you want to use only the records which are available in Recyle bin, you need to use a WHERE condition of isDeleted = TRUE along with ALL ROWS.

list<account> a= new list<account>();
List<Account> acclist = [select id, name,Isdeleted from account WHERE isDeleted = TRUE limit 4 ALL ROWS];
for(account acc:acclist)
{
   a.add(acc); 
}

undelete a;

Hope it helps.
santhosh konathala 17santhosh konathala 17
Thanks a lot ..
Now the above code works fine