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
Hiral Patel 25Hiral Patel 25 

in scheduled apex write a program any custom object every 1st saturday of the month

in scheduled apex write a program any custom object every 1st saturday of the month
in Batch apex write program to update the name field in the contact object with the level as primary 
(Note : upload 500 records through dataloader)
AnudeepAnudeep (Salesforce Developers) 
Hi Hiral, 

You can use the apex scheduler from setup or schedule the apex from developer console. Here is a sample (You need adjust the values in string so that it executes every saturday)
scheduledMerge m = new scheduledMerge();
String sch = '20 30 8 10 2 ?';
String jobID = system.schedule('Merge Job', sch, m);

See below code for sample
 
global class ContactProcessingBatch implements Database.Batchable<sObject>, Schedulable, Database.Stateful {

    //Variable Section
    global FINAL String strQuery;
    global List<String> errorMessages = new List<String>();
    
    global ContactProcessingBatch() { 
        this.strQuery = getBatchQuery();
    }
    
    //Returns the Query String to Batch constructor to fetch right records.
    private String getBatchQuery() {
        String strQuery = 'SELECT Id, Name, OwnerId FROM Contact WHERE Days_DBS_submitted_c > 30 AND Volunteer_Status__c = \'New Sign up\''; 
        return strQuery;
    }
    
    //Batch Start method
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(strQuery);
    }

    //Batch Execute method
    global void execute(Database.BatchableContext BC, List<sObject> scopeList) {
        System.debug(LoggingLevel.INFO, '== scopeList size ==' + scopeList.size());
        
        List<Contact> conList = (List<Contact>) scopeList;
		List<Task> taskList = new List<Task>();
        if(!conList.isEmpty()) {             
            for(Contact con: conList) {
                Task DBStask = New Task();
				DBSTask.Subject = 'Overdue DBS';
				DBSTask.Priority = 'Normal';
				DBSTask.Status = 'In Pogress';
				DBStask.WhoId = con.id;
				DBStask.OwnerId=con.ownerId;
				taskList.add(DBSTask);
            }
            
            try {                    
                Database.SaveResult[] saveResults = Database.insert(taskList, false);
                for (Database.SaveResult sr : SaveResults) {
                    if(!sr.isSuccess()) {
                        for (Database.Error err : sr.getErrors()) {
                            errorMessages.add('Error: ' + err.getStatusCode() + ': ' + err.getMessage());
                        }
                        system.debug(sr.getErrors()[0].getMessage());
                    }
                }
                System.debug('errorMessages: '+errorMessages);
            } catch (System.Exception ex) {
                System.debug('An error occurred when inserting the Tasks: ' + ex.getMessage());
                return;
            }
        }
    }  

    //Batch Finish method for after execution of batch work
    global void finish(Database.BatchableContext BC) { 
        AsyncApexJob aaj = [Select Id, Status, NumberOfErrors, JobItemsProcessed, MethodName, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id =:BC.getJobId()];
        
        // Send an email to the Apex job's submitter notifying of job completion.
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {aaj.CreatedBy.Email};
        mail.setToAddresses(toAddresses);
        mail.setSubject('JOB Salesforce Contact Processing Batch: ' + aaj.Status);
        String bodyText='Total Job Items ' + aaj.TotalJobItems + ' Number of records processed ' + aaj.JobItemsProcessed + ' with '+ aaj.NumberOfErrors + ' failures.\n';
        bodyText += 'Number of Error Messages ' + errorMessages.size() + '\n';
        bodyText += 'Error Message' + String.join(errorMessages, '\n');
        mail.setPlainTextBody(bodyText);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
    
    //Method which schedules the ContactProcessingBatch
    global void execute(SchedulableContext sc) {        
        ContactProcessingBatch cpBatch = new ContactProcessingBatch();
        ID batchprocessid = Database.executeBatch(cpBatch);
    }
}


Also, refer to the documentation

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
 

NOTE: The code provided is an example. You'll need to review and make modifications for your organization.

Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you

Hiral Patel 25Hiral Patel 25
its not work for me can you explain in detail please for schedule class
hims mosehims mose
I have tried this scripts and its working. Now I want to use the Billing option in my service page of Salesforce Consultant (https://axisconsulting.io/). Can you help me in this?