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
Marty DorrenMarty Dorren 

Code in Anonymous works but....

Hi,
I'll apologize ahead of time...

I have some class code that inserts records to a custom object and works fine in anonymouse as long as I comment out the class and method lines at the top.  I have to put this class in a schedule and the schedule runs but the class does NOT execute.  I fear I'm missing something very basic, but at this point, I'm in quicksand.  Here's the code... very simple:

public class Daily_SD_Insert_SVIRA {
    public static void DoSDInsert() {

        date tdy = system.date.today();
        date SvcDate = system.date.today();
        date FirstofLast = date.valueOf(system.date.newInstance(system.today().year(),system.today().month()-1,01));
        date LastofLast = FirstofLast.addMonths(1).toStartofMonth().addDays(-1);
        string IPRecType = '012Z00000004nSCIAY';
        string SDRecType = '012Z00000004nS7IAI';
        string MSRecType = '012F0000000muqTIAQ';
//SD Inserts
        //Create SD Lists
        List<Individual_Programs__c> IPRGSD1 = [select Id, service__c from individual_programs__c where (
        (Status__c = 'Enrolled' and enroll_date__c < today )
        and RecordTypeId = :IPRecType and
            Id NOT IN (Select Individual_Program__c from service_documentation__c where
                       service_date__c = :SvcDate and RecordTypeID = :SDRecType))];
          
        system.debug('IPRGSD1 = '+IPRGSD1);
        List<service_documentation__c> sda = new List<service_documentation__c>();
        //Insert SD
        INTEGER ipcnt = 0;
        for (Individual_Programs__c IPRGSD : IPRGSD1) {
                                    sda.add (new service_documentation__c(
                                    service_Date__c = SvcDate,       
                                    Billable_hours__c = 0,
                                    Billable_minutes__c = 0,
                                    RecordTypeId = SDRecType,
                                    service__c = IPRGSD.service__c,
                                    Individual_Program__c = IPRGSD.Id));
                                system.debug('IPRGSD-Individual_Program__c = '+IPRGSD.Id+ ' |count:'+ipcnt);
                                ipcnt = ipcnt+1;
                            }
                    Database.insert(sda);
                    }
                    }

Thanks ahead of time...

Marty
Best Answer chosen by Marty Dorren
Vinit_KumarVinit_Kumar
Marty,

I don't see any schedulable interface in your Class.So,you won't be able to schedule it.That's why I was asking how you are doing that.I thought you are calling this class from some other schedulable interface class.

So,your class structure should be something like below :-

//The following example implements the Schedulable interface for a class called mergeNumbers:

global class scheduledMerge implements Schedulable {
   global void execute(SchedulableContext SC) {
      mergeNumbers M = new mergeNumbers(); 
   }
}

If this helps,please mark it as best answer to help others :)

All Answers

Vinit_KumarVinit_Kumar
How are you scheduling this class ??
Marty DorrenMarty Dorren
Just through the UI.  I've also used Job Book (app)... Same results.  Thanks for the reply btw.
Vinit_KumarVinit_Kumar
Marty,

I don't see any schedulable interface in your Class.So,you won't be able to schedule it.That's why I was asking how you are doing that.I thought you are calling this class from some other schedulable interface class.

So,your class structure should be something like below :-

//The following example implements the Schedulable interface for a class called mergeNumbers:

global class scheduledMerge implements Schedulable {
   global void execute(SchedulableContext SC) {
      mergeNumbers M = new mergeNumbers(); 
   }
}

If this helps,please mark it as best answer to help others :)
This was selected as the best answer
Marty DorrenMarty Dorren
Hi Thanks again for the reply.  Yes indeed I created a class exactly as you laid out but still, it did not work.  as it turned out, I had to include the method in order for it to work, which it did:

global class Daily_SD_Insert_SVIRA_Sched implements Schedulable {
   global void execute(SchedulableContext SC) {
      Daily_SD_Insert_SVIRA myPageCon = new Daily_SD_Insert_SVIRA();
      myPageCon.DoSDInsert();
   }
}

I'm still not sure why I had to do that but of course, I'm happy that it did.

Many thanks.
Marty
Vinit_KumarVinit_Kumar
Happy to help Marty !!

The class which I was calling has everything(all operations) in constructor,so I have to just intialize it and I am good to go.For you,you have to call the method to as you were doing operation inside the method.

Hope this clears your doubt..