Search Here

Apex Undelete Cutom object DML

 In Apex, the undelete statement is used to recover previously deleted records from the Salesforce Recycle Bin. 

However, please note that the undelete statement is typically used for standard objects, like Leads, Contacts,

 and Accounts, and it may not work with custom objects.


To recover deleted records using the undelete statement for standard objects, you can use code like the following:


// Query deleted Contact records from the Recycle Bin

List<Contact> deletedContacts = [SELECT Id FROM Contact WHERE IsDeleted = true LIMIT 10];


if (!deletedContacts.isEmpty()) {

    // Use the undelete statement to recover the deleted records

    undelete deletedContacts;

}



We query for deleted Contact records by checking the IsDeleted field using SOQL. This query retrieves up to 10 deleted Contact records.


We check if there are any deleted Contact records found (the deletedContacts list is not empty).


If deleted records are found, we use the undelete statement to recover those records from the Recycle Bin.


Please note that the undelete statement is designed to work with standard objects that have a Recycle Bin feature. When working with custom objects, the behavior of the undelete statement may not be the same, and it may not be available.




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.