You need to sign in to do that
Don't have an account?
Aidan Keenan
Apex trigger that will update another field when another field is auto incremented.
I am trying to develop a field that will close all jobs order statuses when another field which is set to auto increment each day goes over 30.
Here is my trigger but this is only working when an edit is done to the job order and I press save which is not what I want. I want all job orders with days idle greater than 30 to be closed. This is my trigger so far. Any ideas how I can set this.
trigger closeJobOrder on ts2__Job__c (before update) {
for(ts2__Job__c jobOrderStatus : trigger.new){
if (jobOrderStatus.Days_Idle__c > 30)
{
jobOrderStatus.ts2__Status__c ='Closed';
}
}
}
Here is my trigger but this is only working when an edit is done to the job order and I press save which is not what I want. I want all job orders with days idle greater than 30 to be closed. This is my trigger so far. Any ideas how I can set this.
trigger closeJobOrder on ts2__Job__c (before update) {
for(ts2__Job__c jobOrderStatus : trigger.new){
if (jobOrderStatus.Days_Idle__c > 30)
{
jobOrderStatus.ts2__Status__c ='Closed';
}
}
}
Can you tell how the auto increment happens? If it is not through an edit, like a formula, it will not fire the trigger. You have to choose scheduled APEX for this.
Thanks,
Shashank
You can schedule this Apex to run daily.
Go through below links to learn more :-
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm
http://www.infallibletechie.com/2012/05/apex-scheduler.html
If this helps,please mark it as best answer to help others :)
Thanks for the reply . I wrote this class. I then scheduled the class to run. I can see under the monitoring apex jobs that the job is queued but it does not seem to ever execute. Do you know why this would be?
global class jobOrderStatusUpdate implements Schedulable{
global void execute(SchedulableContext SC) {
List <ts2__Job__c> jobOrder = [Select id,ts2__Status__c,Days_Idle__c From ts2__Job__c Where ts2__Status__c='Open'];
for(ts2__Job__c status: jobOrder){
if(status.Days_Idle__c > 30)
status.ts2__Status__c ='Closed';
}
update(jobOrder);
}
}
Setup -> Monitoring -> Scheduled Jobs
Then,search your scheduled job and look for Next Scheduled Run column ,that would give you the time when the Job would run next.
If this helps,please mark it as best answer to help others :)
Thanks for your help on this.I can see that the job has started in the scheduled jobs but in apex jobs it is saying queued in apex jobs. I get that this may be because it has a next run time. My question is how do I know that the class has completed and worked?
Alternatively,you can write an Apex code in your class which will send you an email notification confirming the same.
Hope this helps !!
If this helps,please mark it as best answer to help others :)
Thanks for the reply again.
I ran the job but it does not seem to be doing what I expected as jobs that have been idle for more than 30 days still have the open status. My code
Am I right in saying that I don t need any trigger or could I write the trigger and call it in the class?
global class jobOrderStatusUpdate implements Schedulable{
global void execute(SchedulableContext SC) {
List <ts2__Job__c> jobOrder = [Select id,ts2__Status__c,Days_Idle__c From ts2__Job__c Where ts2__Status__c='Open'];
for(ts2__Job__c status: jobOrder){
if(status.Days_Idle__c > 30)
status.ts2__Status__c ='Closed';
}
update(jobOrder);
}
}
So,go ahead and verify the same.
14/05/2014 15:42 Scheduled Apex Queued 0 0 0 Timmons, Jonathan jobOrderStatusUpdate3 707Z000000Dj75c
Hopefully it will run overnight , I will let you thanks for the hepl
Unfortunately the job does not seem to execute, still shows queued in the apex jobs. Do you have any ideas how I can get this to run?
scheduledMerge m = new scheduledMerge();
String sch = '20 30 8 10 2 ?';
String jobID = system.schedule('Merge Job', sch, m);
where scheduledMerge = Apex class to be scheduled.
sch = The time when it needs to be scheduled
Go through the below link to learn more :-
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm
I was just wondering if you had any other ideas why this job is not executing?
Go to Setup -> Monitioring -> Scheduled Jobs -> Look for Next Run and Last Run column and see what time is coming under it.
Last Run : The time job last ran
Next Run : The time it will run again.
The code which I gave you to schedule earlier needs to be executed in Dev console.
When I look at scheduled job, the job is saying that it has started at 12 and the next scheduled run time is 12 the next day but I can see that it is queud in the Apex jobs and is not performing what I want it to do. This code that you gave me where di I need to put it ?
scheduledMerge m = new scheduledMerge();
String sch = '20 30 8 10 2 ?';
String jobID = system.schedule('Merge Job', sch, m);
Go to your name on the top and cikc on that .In the the dropdown you will see Developer console ,click on that and Open execute anonymous window.
I am thinking you are not entering the if condition which I told you earlier too.
status.ts2__Status__c ='Closed';
tsList.add(status);
Where do I need to put another IF ? Really appreciate the help with this.
system.debug('*****Entering IF*****');
after if(status.Days_Idle__c > 30) and then run the class and see the debug logs whether this debug statement which we jsut added is there or not.
In this way we can conclude whether we are ebetring the if condition or not.
global class jobOrderStatusUpdate5 implements Schedulable{
global void execute(SchedulableContext SC) {
List <ts2__Job__c> tsList = new List<ts2__Job__c>();
List <ts2__Job__c> jobOrder = [Select id,ts2__Status__c,Days_Idle__c From ts2__Job__c Where ts2__Status__c='Open'];
for(ts2__Job__c status: jobOrder){
if(status.Days_Idle__c > 30)
status.ts2__Status__c ='Closed';
tsList.add(status);
system.debug('*****Entering IF*****');
}
if(tsList.size() > 0)
{
update tsList;
}
}
}
I go to the deveopler console ? How do I execute this class I have never done this before.
jobOrderStatusUpdate5 jobOrderUpdate = new jobOrderStatusUpdate5();
String sch = '0 0 12 * * ?';
String jobID = system.schedule('Update Job ', sch, jobOrderUpdate);
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm
Try below String and change the job name so your code should be :--
jobOrderStatusUpdate5 m = new jobOrderStatusUpdate5();
String sch = '0 0 0 * * * ';
String jobID = system.schedule('Update Job1', sch, m);
This job would run daily at midnight..
String sch = '0 0 0 * * * ';
String jobID = system.schedule('Update Job1', sch, m);
Treid this code getting execute anonymous error. One of the errors is 11:33:17:043 FATAL_ERROR System.StringException: Support for specifying both a day-of-week AND a day-of-month parameter is not implemented.
jobOrderStatusUpdate5 m = new jobOrderStatusUpdate5();
String sch = '0 0 13 * * ? ';
String jobID = system.schedule('Update Job2', sch, m);
This job would run daily at 1 PM..
Unfortunately my job has not run at all. Maybe I need to do a batch apex job for this. I can t understand how my job is not running even tho it has said it has started.