LoginSignup
0
0

More than 1 year has passed since last update.

Sandoboxのテストデータを消す

Last updated at Posted at 2022-01-28

Sandoboxのストレージがすぐにいっぱいになるらしい。今はデータローダで消しているが複数のオブジェクトがあるので面倒だとのこと。

私はApexバッチを提案。

ContentDocumentLinkが関連する場合には消す順番が大事みたい。
まずはContentDocumentLinkを消す。そのあとは添付ファイルと、関連先のオブジェクト。
間違えるとエラーになる。注意。

public class BAT_Delete_Account implements Database.Batchable<sObject> {

    public Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute

        String query = 'Select Id,Name From Account limit 1';
        return Database.getQueryLocator(query);
    }

    public void execute(Database.BatchableContext BC, List<Account> accList) {

        System.debug('******** accList ' + accList);
        List<Account> deleteAccountList = new List<Account>();
        SET<Id> AccountIdSet = new SET<Id>();

        for (Account a : accList ){
            AccountIdSet.add(a.Id);
            Account acc = new Account();
            acc.Id = a.Id;
            deleteAccountList.add(acc);
        }

        List<ContentDocumentLink> ContentDocumentLinkList = [SELECT Id,ContentDocumentId,LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId =: AccountIdSet];
        List<ContentDocumentLink> deleteContentDocumentLinkList = new List<ContentDocumentLink>();
        SET<Id> ContentDocumentLinkIdSet = new SET<Id>();

        for (ContentDocumentLink cdl : ContentDocumentLinkList){
            ContentDocumentLinkIdSet.add(cdl.ContentDocumentId);
            ContentDocumentLink c = new ContentDocumentLink();
            c.Id = cdl.Id;
            deleteContentDocumentLinkList.add(c);
        }

        List <ContentNote> deleteContentNoteList = [SELECT Id FROM ContentNote WHERE Id =: ContentDocumentLinkIdSet];


        try {        
            System.debug('******** deleteContentDocumentLinkList.size() ' + deleteContentDocumentLinkList.size());
            System.debug('******** deleteAccountList.size() ' + deleteAccountList.size());
            System.debug('******** deleteContentNoteList.size() ' + deleteContentNoteList.size());            

            if(deleteContentDocumentLinkList.size() > 0) delete deleteContentDocumentLinkList;  
            if(deleteAccountList.size() > 0) delete deleteAccountList;
            if(deleteContentNoteList.size() > 0) delete deleteContentNoteList; 



        } catch(Exception e) {
            System.debug(e);
        }

    }   

    public void finish(Database.BatchableContext BC) {
        // execute any post-processing operations like sending email}
    }

}

テストクラス

@isTest
public class BAT_Delete_Account_test {

    private static testMethod void test01() {

        Test.startTest();

        Account acc = new Account();
        acc.Name = 'Test 001';
        insert acc;

        ContentNote cn = new ContentNote();
        cn.Title = 'test Note';
        cn.Content = Blob.valueof('test Note');
        insert cn;

        ContentDocumentLink cdl = new ContentDocumentLink();
        cdl.ContentDocumentId = cn.Id;
        cdl.LinkedEntityId = acc.Id;
        insert cdl;

        System.debug('-------- BAT start ' );

        BAT_Delete_Account bat = new BAT_Delete_Account();
        ID jobId = Database.executeBatch(bat); 

        Test.stopTest();

        List<Account> ResultList = [SELECT Id FROM Account ];
        System.debug('-------- ResultList ' + ResultList);
        System.assertEquals(ResultList.size(), 0);     

        List<ContentNote> ResultList2 = [SELECT Id FROM ContentNote WHERE Id=: cn.Id ];
        System.debug('-------- ResultList2 ' + ResultList2);
        System.assertEquals(ResultList2.size(), 0);     

    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0