• Sanjat Samal 8
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 0
    Replies
Public class EmailMessageDelete{
    public static void preventDeletionEmailMessage(EmailMessage [] pEmailMessage, EmailMessage [] pOldEmailMessage) {

        //EmailMessage deletion is only allowed for administrator
        Set<String> UserNames = new Set<String>();
        for(Exception_Users_List__c ListUsers : [Select Name,    Emp_ID__c from Exception_Users_List__c])
{
                UserNames.add(ListUsers.Emp_ID__c);
}

        String Names=[Select id,EmployeeNumber from User].EmployeeNumber;
        for(EmailMessage currentEmailMessage : pOldEmailMessage) {
            
            //Check if current user is not a system administrator
            if(UserNames.contains(Names)){
                currentEmailMessage.addError(System.Label.Custom_Label_for_email_object);
            }
        }
    }
}
global class ClearHistoricalCaseScheduler implements Schedulable
{
  
    global void execute(SchedulableContext SC) 
    {    
        System.debug('ClearHistoricalCaseScheduler Start...');        
        
        ClearHistoricalCaseBatch batchDelete = new ClearHistoricalCaseBatch();
        //Get Clear Date string - 14 months ago
        
        Integer caseDuration =  0 - Integer.valueOf(GenericProjectConfig__c.getValues('MDM_CaseDuration').value__c);
        String clearDateString = DateTime.Now().addMonths(caseDuration).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        batchDelete.query = 'Select Id From Case Where IsTrueClosed__c = true  and OwnerId not in :queueIdSet and CreatedDate < '+ clearDateString   ;
        ID batchProcessId = Database.executeBatch(batchDelete, 100);
        System.debug('Returned Batch Process ID: ' + batchProcessId);           
        
        System.debug('ClearHistoricalCaseScheduler Stop...'); 
    } 
}
How to write a test class for schedule apex class? For Below Code.
global class ClearHistoricalAccountCaseScheduler implements Schedulable
{
    global void execute(SchedulableContext SC) 
    {    
        System.debug('ClearHistoricalAccountCaseScheduler Start...');        
        
        List<Account> accountList = [Select Case_Life__c From Account Where Case_Life__c != '3 Years' and Case_Life__c != '2 Years'];
        Integer accountIndex = 0;
        if(accountList.size() > accountIndex)
        {
            ClearHistoricalAccountCaseBatch batchDelete = new ClearHistoricalAccountCaseBatch();
            batchDelete.accountList = accountList;
            batchDelete.accountIndex = accountIndex;
            ID batchProcessId = Database.executeBatch(batchDelete, 200);
            System.debug('Returned Batch Process ID: ' + batchProcessId);           
        }
        
        System.debug('ClearHistoricalAccountCaseScheduler Stop...'); 
    } 
}

Thanks
I Need to compare in IF condition but the one is "String" and another is a "Set". Please suggest
Here is my Batch class. Getting the error in   return Database.getQueryLocator(query);.

Can Someone help ?



global class ClearHistoricalAccountCaseBatch implements Database.Batchable<SObject>
{
    public List<Account> accountList;
    public Integer accountIndex;
    public String limitTerms; 
    Public List<GDPR_Case_Owner_Exemption__mdt> GDPRList ;
    Public List<Group>GroupList;

    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        GDPRList = [Select MasterLabel from GDPR_Case_Owner_Exemption__mdt];
        GroupList = [SELECT id FROM Group WHERE DeveloperName = 'GDPRList'];
        Account a = accountList[accountIndex];
        String query = 'Select Id From Case Where Account.Id = \'' + a.id + '\' and IsTrueClosed__c = true and ('
          + 'Ownerid' + '!=' + 'GroupList' + ' )' ;
        if(a.Case_Life__c == '3 Months')
        {
            query += ' and CreatedDate < ' + DateTime.Now().addMonths(-3).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        }
        else if(a.Case_Life__c == '6 Months')
        {
            query += ' and CreatedDate < ' + DateTime.Now().addMonths(-6).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        }
        else if(a.Case_Life__c == '1 Year')
        {
            query += ' and CreatedDate < ' + DateTime.Now().addYears(-1).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        }
        else if(a.Case_Life__c == '2 Years')
        {
            query += ' and CreatedDate < ' + DateTime.Now().addYears(-2).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        }
        else
        {
            query += ' and AccountId = null';
        }
        
        if(String.isNotBlank(limitTerms))
        {
            query += limitTerms;
        }
        
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<sObject> cases)
    {
        try
        {  
            Database.delete(cases, false);
        }
        catch(Exception ex)
        {
            ExceptionHandler.e('Class', 'ClearHistoricalAccountCaseBatch', ex);
        } 
    }

    global void finish(Database.BatchableContext bc)
    {
        accountIndex++;
        if(accountList.size() > accountIndex)
        {
            ClearHistoricalAccountCaseBatch batchDelete = new ClearHistoricalAccountCaseBatch();
            batchDelete.accountList = accountList;
            batchDelete.accountIndex = accountIndex;
            batchDelete.limitTerms = limitTerms;
            batchDelete.GDPRList = GDPRList;
            batchDelete.GroupList = GroupList;
            ID batchProcessId = Database.executeBatch(batchDelete, 200);
            System.debug('Returned Batch Process ID: ' + batchProcessId);           
        }
    }
}