You need to sign in to do that
Don't have an account?
Apex.Scheduler.Not.Working...I think it's my code.
Hello everyone and Happy FRIDAY!!
I have the task of creating a child record for a custom object automatically on a the first of the Month. This is probably something extremely simple - but I am only a pretend developer (I'm teaching myself as I go...) Any help will be greatly appreciated!!
Here's the layout:
Parent Object: Locations_Spaces__c
Child Object: Partnership_Validation__c
I need one validation record created on the first of every month for each Location/Space record.
Here is my class - along with this, I have the Apex Schedule for everyday (resetting the time every time I make what I think is a good tweak to my code in order to test).
global class pvalschedule implements Schedulable { global void execute(SchedulableContext ctx) { Locations_Spaces__c location =[Select ID,Employee__c from Locations_Spaces__c]; if(location <> Null){ Partnership_Validation__c pval = new Partnership_Validation__c (Name = ('SRA Validation'+location.Month__c+location.Year__c), Employee__c = location.Employee__c, Locations_Spaces__c = location.ID, RecordTypeID = '012Q0000000D6nc', OwnerID= location.OwnerID); insert pval; } } }
Nevermind, I figured it out. In my pvalbatch class, I needed to SELECT the Month__c, Year__c, OwnerID, etc.
Here is the final code in case anyone else needs help with a similar issue:
Batch Class:
Scheduler:
and the system.schedule method:
THANK YOU soooo much to Vinit_Kumar for all of your help!!!!
All Answers
bro you need a batch class and then schdule that batch class using a schedular.
plz refer to the e.g http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm
Create a batchable class as follows:
global class pvalbatch implements Database.Batchable<sObject>, Schedulable
{
global pvalbatch()
{
//constuctor
}
global Database.QueryLocator start(Database.BatchableContext bc)
{
//query to return all expired Case Share records
return Database.getQueryLocator([Select ID,Employee__c from Locations_Spaces__c]);
}
global void execute(SchedulableContext sc)
{
}
global void execute(Database.BatchableContext BC, list<sObject> scope)
{
if(scope.size() <> Null){
Partnership_Validation__c pval = new Partnership_Validation__c
(Name = ('SRA Validation'+location.Month__c+location.Year__c),
Employee__c = location.Employee__c,
Locations_Spaces__c = location.ID,
RecordTypeID = '012Q0000000D6nc',
OwnerID= location.OwnerID);
insert pval;
}
global void finish(Database.BatchableContext BC)
{
}
}
Then,create a schedulable class :
global class pvalscheulde implements Schedulable
{
global void execute(SchedulableContext sc)
{
//execute the batch
pvalbatch deleteCS = new pvalbatch();
ID batchprocessid = Database.executeBatch(deleteCS);
}
}
and,now you can schedule this class using system.schedule method
pvalscheulde m = new pvalscheulde();
String sch = '20 30 8 10 2 ?'; // change the time as per your requirement
system.schedule('insert records', sch, m);
I am not at all familiar with this - and its definitely my first shot at batchable. Is the code you posted exactly what I should use, or should I be adding anything to it? And do I put the schedule method within the scheduable class? I'm sorry to be such a newbie! I'm really trying to learn this.
Amanda,
The code above should ork,you can test it out.
I tried the code, but it is not working.
Here is what I have - I had to change the batch class because the field Employee__c(lookup) was throwing an error. That could be the reason it isn't working - I'm just not sure how to tweak it correctly so that it will work.
And the scheduler:
Amanda,
Change the scheduler class to below :-
global class pvalschedule implements Schedulable
{
global void execute(SchedulableContext sc)
{
//execute the batch
pvalbatch createpval = new pvalbatch();
ID batchprocessid = Database.executeBatch(createpval);
}
}
Run below code in developer console to schedule the job:-
pvalschedule m = new pvalschedule();
String sch = '0 45 * * * ?';
system.schedule('insert pval', sch, m);
Oh wow - that was my first time using the developer console - yes extreme newbie. It scheduled fine, but still no records were created. =(
Amanda,
You need to check the logs as where it is failing ,check the logs and see where exactly it is failing...
In the execute method of batch where yiu are inserting the record i.e. insert pval;
put this code after that ;
system.debug('************' + pval.id);
and search for this ******** in the logs and see if you get any id there.
I looked through the log - as best as I know how and didn't see anything where it would have failed - am I looking in the right spot?
Nevermind, I figured it out. In my pvalbatch class, I needed to SELECT the Month__c, Year__c, OwnerID, etc.
Here is the final code in case anyone else needs help with a similar issue:
Batch Class:
Scheduler:
and the system.schedule method:
THANK YOU soooo much to Vinit_Kumar for all of your help!!!!