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
SFDC 2017SFDC 2017 

How to convert or add a set(Ids) into a list<Sobject> in apex?

Hi All,

I have a scenario where i am adding some records to a set <Id> and that Set<Id> we have to add into a List using Addall but that is not possible.I need this list to be passed for Database.delete(List,False).Can i get soem ideas how to pass the set<Id> into a List<Sobject>

Example code:
Set<Id> parSet = new Set<Id>();
List<Account> DelAccLst= new List<Account>();
In this parSet i have some records and then after the forloop we need to add it to the list DelAccLst and this list i have to pass fro Deletion
DelAccLst.addAll(parSet );
Database.delete(DelAccLst,false);

How can i get this done please give me some ideas.

Thanks in advance,
v varaprasadv varaprasad
Hi 

Try this:
 
Set<Id> parSet = new Set<Id>();
List<Account> DelAccLst= [select id from account where id in : parSet];
Database.DeleteResult[] drList = Database.delete(DelAccLst, false);

// Iterate through each returned result
for(Database.DeleteResult dr : drList) {
    if (dr.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully deleted account with ID: ' + dr.getId());
    }
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : dr.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Account fields that affected this error: ' + err.getFields());
        }
    }
}


Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi

Set<Id> parSet = new Set<Id>();
List<Account> DelAccLst= new List<Account>();

In this parSet i have some records and then after the forloop we need to add it to the list DelAccLst and this list i have to pass fro Deletion --- If this list is only used for deletion purpose then You can declare it as 'List<Id> DelAccLst= new List<Id>();' and pass the list of id's to database.delete as in the below code.

DelAccLst.addAll(parSet );
Database.delete(DelAccLst,false);

Thanks.
Raj VakatiRaj Vakati
Try this
 
Set<Id> parSet = new Set<Id>();
List<Account> aList= [select id from account ];
List<Account> DelAccLst= new List<Account>();
For(Account a : aList){

If(a.IsDelete__c){
DelAccLst.add(a);
}
}


Database.DeleteResult[] drList = Database.delete(DelAccLst, false);

// Iterate through each returned result
for(Database.DeleteResult dr : drList) {
    if (dr.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully deleted account with ID: ' + dr.getId());
    }
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : dr.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Account fields that affected this error: ' + err.getFields());
        }
    }
}