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
tn01 rtn01 r 

Help me! Please tell me how to make Apex.

Goal: Create an Apex (content below) that fires every Monday at 7:00 am

↓Apex class

1. With SOQL, get the record of "Result__c object" that satisfies all of the following conditions①to③
①AcceptanceExpectedDate__c is from April 1st to September 1st in the first half and from October 1st to March 31st in the second half
②FulfillingAggregateFlg__c is false
③OrderProbabilityRank__c is 'A' or 'B' or 'C' or cOrderNo__c != null

2. Create a new record (I want to combine multiple records into one record)
・The value of the SE3__c item is 'Cloud Solution Department' , 'Industrial solution' , 'COLOPL'
Assign the sum of the LimitProfit__c items to the LimitIncomeSum__c item
→→I want to create a new record only with a value in the LimitIncomeSum__c field

・Set FillRage__c to the new record
・Set the Apex execution date to the new record in AggregateDate

I tried to write it, but I don't understand it at all.

-----------------------------------
global with sharing class FulfillingAggregate implements Database.Batchable<sObject> ,Schedulable, Database.Stateful {
    
    //Batchschedule
    global void execute(SchedulableContext sc) {
        FulfillingAggregate b = new FulfillingAggregate();
        database.executebatch(b,200);
    }

    
    //data acquisition
    global Database.QueryLocator start(Database.BatchableContext bc) {
        Date myToday = Date.Today();
        Date firstDate;
        Date endDate ;
        
        if (myToday.month() >= 4 && myToday.month() <= 9){
               
               firstDate = Date.newInstance(myToday.year(), 4, 1);
               endDate = Date.newInstance(myToday.year(), 9, 30);

        } else {
               
               firstDate = Date.newInstance(myToday.year(), 10, 1);
               endDate = Date.newInstance(myToday.year() + 1, 3, 31);
               
        }        
        //return Database.getQueryLocator('');  
        return Database.getQueryLocator('SELECT Id, SellingPrice__c, ExternalSales__c, InCompanySales__c, InCompanyTransfer__c, IndividualTransfer__c, AbilitySales__c, ProductCost__c, ExternalProductionCost__c, InternallyProductionCost__c, SalesTotalProfit__c, AcceptanceExpectedDate__c, FillRage__c, FulfillingAggregateFlg__c, SE1__c, SE2__c, SE3__c, SE4__c, FulfillingAggregateDate__c FROM Result__c WHERE FulfillingAggregateFlg__c = false AND (cOrderNo__c != null  OR OrderProbabilityRank__c = \'A\' OR OrderProbabilityRank__c = \'B\' OR OrderProbabilityRank__c = \'C\')' );   
        
        //return Database.getQueryLocator('SELECT Id, SellingPrice__c, ExternalSales__c, InCompanySales__c, InCompanyTransfer__c, IndividualTransfer__c, AbilitySales__c, ProductCost__c, ExternalProductionCost__c, InternallyProductionCost__c, SalesTotalProfit__c, AcceptanceExpectedDate__c, FillRage__c, FulfillingAggregateFlg__c, SE1__c, SE2__c, SE3__c, SE4__c, FulfillingAggregateDate__c FROM Result__c WHERE AcceptanceExpectedDate__c >= firstDate AND AcceptanceExpectedDate__c <= endDate AND FulfillingAggregateFlg__c = false AND (cOrderNo__c != null  OR OrderProbabilityRank__c = \'A\' OR OrderProbabilityRank__c = \'B\' OR OrderProbabilityRank__c = \'C\')' );   
    }
    
    global void execute(Database.BatchableContext bc, List<Result__c> resultList) {  
        Date myDate = Date.Today();
        List<Result__c> results = new List<Result__c>();
        for(Result__c result: resultList){
            if(result.IsClosed__c == FALSE){
                result.FulfillingAggregateFlg__c = true; /
                result.FulfillingAggregateDate__c = myDate; 
                
                results.add(result);
            }
        }    
        if(results.size()>0){
            insert results;
            }
    }
        

    global void finish(Database.BatchableContext bc){
        System.debug('FulfillingAggregateBatchProcessCompleted');  
    }
}
 
Salesforce TechieSalesforce Techie
Have you tried using schduled triggered flow also?