0

As per the code I don't want to delete 0th record and delete rest of the record. But it is deleting all the records!

Kindly assist where I am making the mistake.

Here is the code:

list<account> scope = [Select Id,(Select id,CreatedDate,ebMobile__FileType__c,ebMobile__Account__c from Files__r order by CreatedDate DESC) from account where id in ('0016D00000444','0016D000000ugO')]; 
        Set<Id>OldIds = new Set<Id>();
        Set<Id>newIds = new Set<Id>();
        Set<Id> rIds = new Set<Id>();
        Set<Id> rrIds = new Set<Id>();
        list<File__c> listmemb = new list<File__c>();
        List<File__c> listmemb3 = new List<File__c>();
        List<File__c> listmemb4 = new List<File__c>();
        for(Account Acc : scope)
        {
            for(File__c fi : Acc.ebMobile__Files__r)
            {
                listmemb = [select id,CreatedDate,ebMobile__FileType__c from File__c where id =: fi.id];
                if(fi.ebMobile__Account__c != Null)
                {
                    for(Integer i=0; i<listmemb.size(); i++)
                    {
                        if(fi.ebMobile__FileType__c == 'Signature' && i==0)
                        {
                            rIds.add(listmemb[0].id); // Exclude 0th record 
                        }
                        if(i>0 && listmemb[i].ebMobile__FileType__c == 'Signature' || i > 0 && listmemb[i].ebMobile__FileType__c != 'signature')
                        {
                            rrIds.add(listmemb[i].id); // Delete all record excluding 0th record
                        }
                        if(fi.ebMobile__FileType__c != 'Signature')
                        {
                            OldIds.add(fi.id);  
                        }
                    }
                }
            }
        }
        listmemb3 = [Select id,CreatedDate,ebMobile__FileType__c from File__c where id in : OldIds];
        listmemb4 = [Select id,CreatedDate,ebMobile__FileType__c from ebMobile__File__c where id in : rrIds];
        if(listmemb3.size() > 0 && listmemb4.size()>0)
        {
            delete listmemb3;
            delete listmemb4;
        }
    }
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

2 Answers2

0

There are some many unnecessary checks and lists in the code

Let us simplify the stuff:

list scope = [Select Id,(Select id,CreatedDate,ebMobile__FileType__c,ebMobile__Account__c from Files__r order by CreatedDate DESC) from account where id in ('XXXXXXXXXXXXXXXX','XXXXXXXXXXXXXXXX')];//Always keep Ids encrypted while posting on public platform
SetOldIds = new Set();
SetnewIds = new Set();
Set rIds = new Set();
Set rrIds = new Set();
list listmemb = new list();
List listmemb3 = new List();
List listmemb4 = new List();
for(Account Acc : scope)
{
    Integer i= 0; //This will be used to exclue the first element
    for(File__c fi : Acc.ebMobile__Files__r)
    {
        //listmemb = [select id,CreatedDate,ebMobile__FileType__c from File__c where id =: fi.id];//You don't need this as you already have fi
        //if(fi.ebMobile__Account__c != Null)We are getting fi from Acc so it won't be having ebMobile__Account__c as null, assuming it as lookup
        //{
            //for(Integer i=0; i {This is syntactically wrong
                /* This whole section is not needed as rIds is not being used anywhere
                if(fi.ebMobile__FileType__c == 'Signature' && i==0)
                {
                    rIds.add(listmemb[0].id); // Exclude 0th record
                }
                */
                //if(i>0 && listmemb[i].ebMobile__FileType__c == 'Signature' || i > 0 && listmemb[i].ebMobile__FileType__c != 'signature')
                if( i > 0 ) //Just put the check you need
                {
                    rrIds.add(listmemb[i].id); // Delete all record excluding 0th record
                }
                if(fi.ebMobile__FileType__c != 'Signature')
                {
                    OldIds.add(fi.id);
                }
                i++;
            //}
        //}
    }
}
listmemb3 = [Select id,CreatedDate,ebMobile__FileType__c from File__c where id in : OldIds];
listmemb4 = [Select id,CreatedDate,ebMobile__FileType__c from ebMobile__File__c where id in : rrIds];
if(listmemb3.size() > 0 && listmemb4.size()>0)
{
    delete listmemb3;
    delete listmemb4;
}
}

Hope this helps.

वरुण
  • 1,237
  • 3
  • 18
  • 50
  • @varun Thank you for your answer, with some modifications in your code i resolved the issue your input was very valuable. – Vishal Reddy Apr 22 '20 at 18:45
0

@Vishal there are a couple things I'm not really sure of based on your code and context:

  1. What defines 'the first file'? In your second (duplicate) query you don't seem to apply any ordering, so Salesforce might return you the same records in different order based on their database structuring. In your first sub-select query there is.
  2. Is there a specific reason you use if, if, if, instead of if, else if, else if? With this approach you prevent the second and third item to run, even when the first one applied. This will simplify your code as you don't need all duplicate check (i == 0, i > 0 and such)
  3. How is it possible that a different Salesforce record (File vs. ebMobile__File__c) can have the same Salesforce ID? (in your query for listMembers)

Couple suggestions:

  1. Only query the records you really want to delete using Offset (shift starting record)
  2. Please try to avoid doing queries and DML actions in loops, since this is bad for performance, but also might cause running into governor limits
  3. Apply usage of variableBinding, this will ensure no SOQL Injection can be applied (when the account IDs are e.g. fetched from the front-end)
  4. Simplify your logic to do what you really want; if you query only those to delete (see 1.) then you can simply loop ones to determine the Signature condition and then just delete the list of Files per Account. In my perspective, there is no need to query the files again, since you already have their IDs and records, so you can simply specify to delete the retrieved records, right?
Set<Id> accountIds = new Set<Id>{ 'xxxx', 'xxxx' };
List<Account> scope = [SELECT Id,
                            ( SELECT Id, ... 
                              FROM Files__r 
                              ORDER BY CreatedDate DESC
                              OFFSET 1 )
                         FROM Account 
                        WHERE Id IN :accountIds];
List<File> filesToDelete = new List<File>();
List<ebMobile__File__c> ebMobileFileToDelete = new List<File>();
for( Integer i = 0, j = scope.size(); i < j; i++ ){
   Account acc = scope[ i ];
   if( acc.Files__r != null & !acc.Files__r.isEmpty() ){
      for( Integer k = 0, l = acc.Files__r.size(); k < l; k++ ){
         File f = acc.Files__r[ k ];
         if( f.ebMobile__FileType__c != 'Signature' ){
            // When not signature, delete the original file
            filesToDelete.add( f );
         } else{
             // Don't delete the File, but delete the EB MobileFile
             ebMobileFileToDelete.add( new ebMobile__File__c( Id = f.Id ) );
         }
      }
   }
}
if( !filesToDelete.isEmpty() ){ delete filesToDelete; }
if( !ebMobileFileToDelete.isEmpty() ){ delete ebMobileFileToDelete; }

Please note, I haven't run this code, so it might require some tweaking, but I hope you'll be able to get it all working.

Good luck and enjoy! Reinier

Reinier
  • 1
  • 1