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
sfdc007sfdc007 

Batch class help needed

Hi,

I have a requirement where i need to write a batch class for which i need help n it

I have a custom object called "PS_TF_Status_Track__c"

I have 3 number fields in it called Hours__c, Minutes__c and days__c

i want to write a batch class which should calculate the milli seconds for all the 3 fields mentioned above and sum it up to a single custom field

Hours -> milli seconds
Minutes -> milli seconds
days -> milli seconds

the single custom field should sum up all the 3 milli seconds together

let me know hw to write it

Pls help me

Thanks in Advance
Best Answer chosen by sfdc007
Arunkumar RArunkumar R
Hi SFDC,

Could you try the below code. Change the field names according to your object if needed.
 
global class MillisecondBatchConvertController implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
		// Here mention where condition if you need.
        String query = 'SELECT Hours__c, Minutes__c, days__c FROM PS_TF_Status_Track__c';
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, List<PS_TF_Status_Track__c> scope)
    {
         for(PS_TF_Status_Track__c currPS: scope)
         {
			Integer hoursToMilliSeconds  = currPS.Hours__c != null ? currPS.Hours__c * 3600000 : 0;
			Integer minToMilliSeconds = currPS.Minutes__c != null ? currPS.Minutes__c * 60000 : 0;
            Integer daysToMilliSeconds =  currPS.days__c != null ? currPS.days__c * 86400000 : 0;
		
			 // Enter your custom field to sum all three fields values.
			 currPS.Total_Milliseconds__c =  hoursToMilliSeconds + minToMilliSeconds + daysToMilliSeconds;

         }
         update scope;
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}

Execute Batch From Developer Console:
 
Database.executeBatch(new MillisecondBatchConvertController(), 200);