You need to sign in to do that
Don't have an account?
Lena Christy
Schedule an Apex Class
Hi everyone,
I tried to create a class to get executed once a year:
My questions are:
How to schedule this to get executed in december each year for example? And what I want also is, depending on the result of cal__c, another field in Account will get updated, where I should put this treatment part? in the finish() ?
Thanks in advance
I tried to create a class to get executed once a year:
global class CalculOnAccount implements Database.Batchable<sObject>{ global Database.QueryLocator start(Database.BatchableContext BC) { String query = 'SELECT field1__c,field2__c, cal__c FROM Account'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<Account> scope) { for(Account a : scope) { a.cal__c= ((a.field1__c- a.field2__c)*100 )/(a.field2__c); } insert scope; } global void finish(Database.BatchableContext BC) { } }
My questions are:
How to schedule this to get executed in december each year for example? And what I want also is, depending on the result of cal__c, another field in Account will get updated, where I should put this treatment part? in the finish() ?
Thanks in advance
1) http://amitsalesforce.blogspot.com/2016/02/batch-apex-in-salesforce-test-class-for.html
How to Schedule scheduler class
There are two option we have schedule the scheduler classes.
1) By System Scheduler :-
Step 1) Click on Setup->Apex class. Then search Schedule Apex button.
Step 2) Select the scheduler class and set Time
Scheduler class for you 2) By Developer console
Execute below code from developer console :-
Please let us know if this will help you
Thanks,
Amit Chaudhary
All Answers
In order for a class to be able to be scheduled, you must implement a Schedulable interface. Like:
At that point, you can schedule it via Setup by going to Develop > Apex Classes > Schedule Apex Class (button)
For the the result of cal__c, it would be best to store the result outside of the field and process in the same transaction. For example:
The finish method is only called 1 time after all the batch "executes" are completed so you would not be able to perform record-level logic there.
Hope that helps
Thank you so much for your reply John ! I tried what you said, here is my code updated:
I sheduled it but nothing happend, I didn't find a value in cal__c and I find this error
First error: Attempt to de-reference a null object
Any more help please I would be thankful..
1) http://amitsalesforce.blogspot.com/2016/02/batch-apex-in-salesforce-test-class-for.html
How to Schedule scheduler class
There are two option we have schedule the scheduler classes.
1) By System Scheduler :-
Step 1) Click on Setup->Apex class. Then search Schedule Apex button.
Step 2) Select the scheduler class and set Time
Scheduler class for you 2) By Developer console
Execute below code from developer console :-
Please let us know if this will help you
Thanks,
Amit Chaudhary
- Field1__c and/or Field2__c being returned from the query are null
- Field2__c being returned from the query equals 0
Use this code and turn on the debug log:
Look at the debug logs and check those debug print statements for any nulls or zero demoninators.
1) add null check.
2) remove insert and use update.
3) Add zero check on field2__c field as well other divide by zero you will get Let us know if this will help you
Thanks
Amit Chaudhary
A lot of thanks for your replies, II tried both of your solutions with different fields
For John I tried yours and I put "update" and the Batch Apex is completed with no failures, but I did'nt find the result in the fied.
For your solution Amit Chaudhary, I got this Error: First error: Update failed. First exception on row 0 with id 001580000049cHmAAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ConversionAcc: execution of BeforeUpdate
caused by: System.NullPointerException: Attempt to de-reference a null ...
the ConversionAcc is a trigger already set for field1__c . what do you think about this?
What are the values for field1__c and field2__c?
Run a test case for this to confirm - field1__c equal to 5 and field2__c equal to 10 for instance. Then let us know what you get
Issue not coming in your batch job now issue is coming in ConversionAcc trigger. Please post the code of ConversionAcc trigger so that we can help you or just deactive the ConversionAcc trigger and try above Batch job. Let us know if this will help you
Thanks
Amit Chaudhary
Hi Amit, sorry I couldn't answer before,
When I tried to desactivate the trigger, the batch job work perfectly, so the issue is from the trigger which is a trigger (before insert, before update) to the field1__c with some simple calculation from other fields.
Thanks a lot for you help Amit.