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
Cloud AtlasCloud Atlas 

Looping to find minimum

Hello,
Below is my batch class to find the minimum pageview/session rate if affiliated record  matches certain criteria.

Concept is simple, get all Monthly Activities in a LIST and compare their Pageview_Rate against Minimum_Pageview_Rate.
If it is higher, then replace the Minimum with Pageview_Rate.
Minimum_Pageview_Rate is a common value for all records for that ONE MONTH and YEAR.

The batch is executing , but isn't updating anything.
Can someone point out what I amy be doing wrong here.

I apologize in advance for any basic error.
I am not very adept in writing batch jobs.

Any help is appreciated.
Thanks!
global class BatchToFindFormerAuthorRate implements Database.Batchable<sObject>, Schedulable{
    
    global final string query;
	private Date currentDate = date.today();
    
    global BatchToFindFormerAuthorRate(){
       query = 'SELECT Id,Paid_On__c,FA_Pageview_Rate__c,FA_Session_Rate__c, Minimum_Pageview_Rate__c,Minimum_Session_Rate__c, Expert_Contract__r.Status__c, Month__c, Year__c ,Pageview_Rate__c, Session_Rate__c FROM Monthly_Activity__c';  
    	
    }
               
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Monthly_Activity__c> scope){			
        List<Monthly_Activity__c> monActList = new List<Monthly_Activity__c>();
       
        for(Monthly_Activity__c c : scope){
            if(c.Expert_Contract__r.Status__c == 'Live' && c.Month__c == currentDate.month() && c.Year__c == currentDate.year()){
                if(c.Paid_On__c == 'Pageviews' && c.Minimum_Pageview_Rate__c > c.Pageview_Rate__c ){
                    c.Minimum_Pageview_Rate__c = c.Pageview_Rate__c;
                    c.FA_Pageview_Rate__c = c.Minimum_Pageview_Rate__c/2;
                    monActList.add(c);       
            	}else if(c.Paid_On__c == 'Sessions' && c.Minimum_Session_Rate__c > c.Session_Rate__c){
                    c.Minimum_Session_Rate__c = c.Session_Rate__c;
                    c.FA_Session_Rate__c = c.Session_Rate__c/2;
                    monActList.add(c);
    	}
       }         
     }
     update monActList;        
 }      
    
    global void execute(SchedulableContext SC){
        Database.executeBatch(new BatchToFindFormerAuthorRate());                  
    }
    
    global void finish(Database.BatchableContext BC){
        List<String> EMAIL_RESULTS = new List<String>{System.Label.Email_List};
             
        AsyncApexJob apexBatchResult = [
            SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email
            FROM AsyncApexJob
            WHERE Id =: BC.getJobId()
        ];
    
        // Generate email body
      
    

    }
 }

 
Best Answer chosen by Cloud Atlas
Paul S.Paul S.
Your code is currently only updating the pageview rate fields if the pageviews are LESS than the minimum.  Try this:
c.Paid_On__c == 'Pageviews' && c.Pageview_Rate__c > c.Minimum_Pageview_Rate__c