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
TEJA M 19TEJA M 19 

optimized DML operation from database.saveresult

Hi All,

I m trying to delete from a temperory object from database.saveresult. after successful process i am getting the ids and querying records and from quired records I am taking conact ids and entitlement ids and then querying based on the ids and deleting those records. 
Is there any optimized way that i can delete records without qureying from database because i have the ids for both temporary object and entilements here.

if(contEntToInsert != null && contEntToInsert.size()>0) {
                
                Database.SaveResult[] conEntList = Database.insert(contEntToInsert,false); 
                System.debug('conEntList------------>'+conEntList);
                integer index = 0;
                set<id> CONENTID = new set<id>();
                for (Database.SaveResult sr : conEntList){
                    if(sr.isSuccess()){
                        //set<id> CONENTID = new set<id>();
                        CONENTID.add(sr.getId());
                        system.debug('sr.getId()=======>' +sr.getId());
                        
                    }
                }
                
                List<Core_Contact_Entitlement__c> successConEntList = [SELECT id,Core_Contact_Id__c, Core_Entitlement_Id__c 
                                                                       from Core_Contact_Entitlement__c where id IN: CONENTID];
                system.debug('successConEntList'+ successConEntList);
                List<id> contactids = new List<id>();
                List<id> contEntitids = new List<id>();
                for(Core_Contact_Entitlement__c conEnt: successConEntList){
                    contactids.add(conEnt.Core_Contact_Id__c);
                    contEntitids.add(conEnt.Core_Entitlement_Id__c);    
                }
                system.debug('contactids'+ contactids);
                system.debug('contEntitids' + contEntitids);
                List<PRM_Batch_Entitlement__c> batchEntitlementtoDeleteList =[select id,PRM_Contact_Id__c,PRM_Entitlement_Id__c 
                                                                              from PRM_Batch_Entitlement__c where PRM_Contact_Id__c 
                                                                             IN: contactids AND PRM_Entitlement_Id__c IN:contEntitids];
                
                system.debug('batchEntitlementtoDeleteList' + batchEntitlementtoDeleteList);
                delete batchEntitlementtoDeleteList;
Karthikeyan Rajendran 14Karthikeyan Rajendran 14
Hi Teja

     From your question and code I understand that after the successful insertion you want to carry out the delete operation. I guess the following will be an optimized one.
if(contEntToInsert != null && contEntToInsert.size()>0) {

      List<Database.SaveResult> result = Database.insert(contEntToInsert,false);
      for(Integer i = 0;i<result.size(); i++){

            if(result.get(i).isSuccess()){
                 contactids.add(contEntToInsert.get(i).Core_Contact_Id__c);
                 contEntitids.add(contEntToInsert.get(i).Core_Entitlement_Id__c);
            }
      }
}

List<PRM_Batch_Entitlement__c> batchEntitlementtoDeleteList =[select id,PRM_Contact_Id__c,PRM_Entitlement_Id__c from PRM_Batch_Entitlement__c where PRM_Contact_Id__c IN: contactids AND PRM_Entitlement_Id__c IN:contEntitids];
                
delete batchEntitlementtoDeleteList;
These two (Contact_Id__c, Core_Entitlement_Id__c) are lookup fields and I guess these will have values before in contEntToInsert list doing performing insert.

I hope the answer is helpful for you.

Regards
Karthik