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
hogehogehogehoge 

Help me! Apex Class test code

global without sharing class ResultFulfillingAggregateScheduler implements Schedulable{
    
    /**
     *  *** AcceptanceExpectedDate is divided into 1st half and 2nd half (1st half: April to September, 2nd half: October to next March)
     *  *** Split timing is based on execution date
     */
    public void execute( SchedulableContext sc ){
        // Get record type
        String conditonRecordTypeA = 'OrderEstimated';
        String conditonRecordTypeB = 'OrderRegistration';
        Map<String,Schema.RecordTypeInfo> recordTypeMap = new Map<String,Schema.RecordTypeInfo>();
        for( Schema.RecordTypeInfo info : Result__c.SObjectType.getDescribe().getRecordTypeInfos() ){
            if( info.getDeveloperName() == conditonRecordTypeA || info.getDeveloperName() == conditonRecordTypeB ){
                recordTypeMap.put( info.getDeveloperName() , info );
            }
        }
        /**

         */
        // Set String value
        List<String> conditionOrderProbabilityRank = new List<String>{ 'A' , 'B' , 'C' };
        // Set conditions for first half and second half
		Date day = Date.today();
        Date startDate = Date.newInstance( day.year() - ( day.month() < 4 ? 1 : 0 ) , ( day.month() >= 4 || day.month() <= 9 ) ? 4 : 10 , 1 );
        Date endDate = startDate.addMonths( 6 ).addDays( - 1 );
        // execute SOQL
        List<Result__c> resultList = [
            SELECT OrderProbabilityRank__c , RecordTypeId , DXSolutionDeptFlg__c , PlatformSolutionDeptFlg__c , DSPSSolutionDeptFlg__c , ManagedServiceDeptFlg__c , EastJapanSolutionDeptFlg__c , ChubuSolutionDeptFlg__c , WestJapanSolutionDeptFlg__c ,  LimitProfit__c , LimitIncomePartTarget__c
            FROM Result__c
            WHERE ( DXSolutionDeptFlg__c = TRUE OR PlatformSolutionDeptFlg__c = TRUE OR DSPSSolutionDeptFlg__c = TRUE OR ManagedServiceDeptFlg__c = TRUE OR EastJapanSolutionDeptFlg__c = TRUE OR ChubuSolutionDeptFlg__c = TRUE OR WestJapanSolutionDeptFlg__c = TRUE )
              AND ( FulfillingAggregateFlg__c = FALSE )
              AND ( AcceptanceExpectedDate__c >= :startDate AND AcceptanceExpectedDate__c <= :endDate )
              AND ( ( RecordType.DeveloperName = :conditonRecordTypeA AND OrderProbabilityRank__c IN :conditionOrderProbabilityRank ) OR RecordType.DeveloperName = :conditonRecordTypeB )
        ];       
        // Save records to aggregate
        Map<String,Result__c> resultMap = new Map<String,Result__c>();
        resultMap.put( '01' , new Result__c( RecordTypeId = recordTypeMap.get( conditonRecordTypeA ).getRecordTypeId() , OrderProbabilityRank__c = 'A' , FulfillingAggregateFlg__c = true , SE3__c = 'AAA' , LimitIncomeAchievementSum__c = 0 , LimitIncomeTargetSum__c = 0 ) );
        resultMap.put( '02' , new Result__c( RecordTypeId = recordTypeMap.get( conditonRecordTypeA ).getRecordTypeId() , OrderProbabilityRank__c = 'B' , FulfillingAggregateFlg__c = true , SE3__c = 'AAA' , LimitIncomeAchievementSum__c = 0 , LimitIncomeTargetSum__c = 0 ) );
        resultMap.put( '03' , new Result__c( RecordTypeId = recordTypeMap.get( conditonRecordTypeA ).getRecordTypeId() , OrderProbabilityRank__c = 'C' , FulfillingAggregateFlg__c = true , SE3__c = 'AAA' , LimitIncomeAchievementSum__c = 0 , LimitIncomeTargetSum__c = 0 ) );  
        resultMap.put( '22' , new Result__c( RecordTypeId = recordTypeMap.get( conditonRecordTypeB ).getRecordTypeId() , FulfillingAggregateFlg__c = true , SE3__c = 'AAA', LimitIncomeAchievementSum__c = 0 , LimitIncomeTargetSum__c = 0 ) );
   
        // do the aggregation
        for( Result__c result : resultList ){

            if( result.RecordTypeId == recordTypeMap.get( conditonRecordTypeA ).getRecordTypeId() && result.OrderProbabilityRank__c == 'A' && result.DXSolutionDeptFlg__c ){
                resultMap.get( '01' ).LimitIncomeAchievementSum__c += result.LimitProfit__c;
                resultMap.get( '01' ).LimitIncomeTargetSum__c += result.LimitIncomePartTarget__c;
		resultMap.get( '01' ).FulfillingAggregateDate__c = day;
            }

            if( result.RecordTypeId == recordTypeMap.get( conditonRecordTypeA ).getRecordTypeId() && result.OrderProbabilityRank__c == 'B' && result.DXSolutionDeptFlg__c ){
                resultMap.get( '02' ).LimitIncomeAchievementSum__c += result.LimitProfit__c;
                resultMap.get( '02' ).LimitIncomeTargetSum__c += result.LimitIncomePartTarget__c;
		resultMap.get( '02' ).FulfillingAggregateDate__c = day;
            }

            if( result.RecordTypeId == recordTypeMap.get( conditonRecordTypeA ).getRecordTypeId() && result.OrderProbabilityRank__c == 'C' && result.DXSolutionDeptFlg__c ){
                resultMap.get( '03' ).LimitIncomeAchievementSum__c += result.LimitProfit__c;
                resultMap.get( '03' ).LimitIncomeTargetSum__c += result.LimitIncomePartTarget__c;
		resultMap.get( '03' ).FulfillingAggregateDate__c = day;
            }
       

            if( result.RecordTypeId == recordTypeMap.get( conditonRecordTypeB ).getRecordTypeId() && result.DXSolutionDeptFlg__c ){
                resultMap.get( '22' ).LimitIncomeAchievementSum__c += result.LimitProfit__c;
                resultMap.get( '22' ).LimitIncomeTargetSum__c += result.LimitIncomePartTarget__c;
		resultMap.get( '22' ).FulfillingAggregateDate__c = day;
            }


        }
        // insert record
        insert resultMap.values();
    }

}


Please tell me how to write the test class.
SwethaSwetha (Salesforce Developers) 
HI hogehoge ,
The below articles give a good insight into how to get started with writing test classes
 https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines 
 
Examples of test classes for schedulable interface:
https://salesforce.stackexchange.com/questions/17428/test-class-for-schedulable-interface
https://salesforce.stackexchange.com/questions/95827/test-class-for-scheduler-class
https://salesforce.stackexchange.com/questions/161702/inserting-test-record-with-record-type
 
Hope this helps! Thanks
TobyKutlerTobyKutler
Might be wrong but I do not think anyone is going to write this Test Class for you... It should be easy. You are just querying one object so make sure to create that object. You then map that object's record and loop through them where you have multiple conditions. Try to create different test data so that it will hit each those unique conditions that way you get max coverage.