根据代码,我不想删除第0条记录并删除其余记录。但是它正在删除所有记录!
请在我犯错的地方提供帮助。
代码如下:
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;
}
}
答案 0 :(得分:0)
代码中有许多不必要的检查和列表
让我们简化内容:
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;
}
}
希望这会有所帮助。
答案 1 :(得分:0)
@Vishal根据您的代码和上下文,我不确定有几件事:
夫妇建议:
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; }
请注意,我尚未运行此代码,因此可能需要进行一些调整,但我希望您能够使它们全部正常工作。
祝你好运,享受!雷尼尔(Reinier)