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

how would I set the system to automatically create a new record for case object on every month if case is not created by user

i have requirement to create a case if case was not being created manually for that month for particular accounts. i have stored all account in custom object (where i am going to store the account which i want to apply above proceed. i am not sure what query i should write
global class BatchCaseRecord implements Database.Batchable<sObject> {
    global final string query;
    DateTime dt =;
    String monthName = dt.format('MMMMM');
    Integer year =;
    global Database.QueryLocator start(Database.BatchableContext bc) {
// what will be query to get the record which need to processed 
        string query = 'SELECT Account__c FROM CaseRecord_Account__c Limit 2000';

        return Database.getQueryLocator(query);
    global void execute(Database.BatchableContext bc, List<CaseRecord_Account__c> CNbatch) {
        List<Case> caserecords = new List<Case>();
        String rtype = [select id from recordtype where sobjecttype = 'Case' and name = 'CaseRecord'].id;
        for (CaseRecord_Account__c CN : CNbatch) {
            for (integer i = 0; i < AccountList.size; i++) {
                //what will be if codition to check if case is not created this month 
                case c1 = new case();

                c1.Month__c = monthName.left(3);
                c1.Year__c = String.valueOf(year);
                c1.Reason__c = 'Phone';
                c1.RecordTypeId = rtype;
                c1.Client_Account__c = CN.Account__c;
                if (!caserecords.isEmpty()) {
                    database.insert(caserecords, false);
/// send the email to account user for those case is created

    global void finish(Database.BatchableContext bc) {
        AsyncApexJob a = [Select Id, Status,ExtendedStatus,NumberOfErrors, JobItemsProcessed,TotalJobItems, CreatedBy.Email from AsyncApexJob where Id = :BC.getJobId()];
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[]{
        mail.setSubject('Match Merge Batch ' + a.Status);
        mail.setPlainTextBody('records processed ' + a.TotalJobItems + 'with ' + a.NumberOfErrors + ' failures.');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{

Brandon NelsonBrandon Nelson
Have you tried using Scheduling Jobs and running APEX? What I would do is on the scheduled job, create APEX to loop through the object and collect all case IDs and created dates. Compare the dates with TODAY()-Created Date. If no case has been created within x amount of days, create new record. 

Sounds easy and I'm sure there is more to it, but just general idea of how I would go about it.