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
Aidan KeenanAidan 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';
            }
    }
}
ShashForceShashForce
Hi,

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 
Aidan KeenanAidan Keenan
Hi thanks for the reply. Yes the auto increment field updates through the use of a formula.  Can you give me some information how I would do this using scheduled Apex? 
Vinit_KumarVinit_Kumar
You need to use Scheduled Apex to achieve this.The logic for the same should be if current day is greater than 30 days,then change the status to closed.

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 :)

Aidan KeenanAidan Keenan
Hi Vinit

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);
}
}
Vinit_KumarVinit_Kumar
If you have scheduled your job,then it should show under scheduled jobs.Follow below steps to look there :-

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 :)
Aidan KeenanAidan Keenan
Thanks Vinit 

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?  
Vinit_KumarVinit_Kumar
Once the job is finished,it would perform what it is supposed to do.Also,In the Apex jobs section the Job Id would show as completed.However,it won't be available for a long time.So,you should verify the time it is supposed to run.

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 :)
Aidan KeenanAidan Keenan
Hi Vinit
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);
}
}
Vinit_KumarVinit_Kumar
Yes,you don't need any Trigger to call this class.Your code does not seem to be correct to me.Try the below code :-

global class jobOrderStatusUpdate 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);
    }
    if(tsList.size() > 0)
    {
            update tsList;
    }
}
}


Vinit_KumarVinit_Kumar
Another thing which I just saw,what is the datatype of ypur field Days_Idle__c.I have a feeling that you are not entering inside your 'if' condition.

So,go ahead and verify the same.
Aidan KeenanAidan Keenan
Days_Idle__c. is a number field  determined by this formula field  Today() - DATEVALUE(Last_Job_Activity__c )    Do I need to alter this  in the if statement??
Vinit_KumarVinit_Kumar
Looks fine then,try with my code and let me know what happens ??
Aidan KeenanAidan Keenan
I have set this to run over night  says it has started in the scheduled jobs but still in the apex jobs shows 

  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
Aidan KeenanAidan Keenan
Hi Vinit 

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?
Vinit_KumarVinit_Kumar
How are you scheduling it.You can use system.schedule method to schedule at anytime.Try running the below code in Dev console :-

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
Aidan KeenanAidan Keenan
I am going into  apex classes   using the button schedule apex  I am scheduling my class to run that way.  It is showing in the scheduled job as started but in the apex jobs just as queued.  Where would i put the above code in my code example to get this to work. 
Aidan KeenanAidan Keenan
Hi Vinit 

I was just wondering if you had any other ideas why this job is not executing?


Vinit_KumarVinit_Kumar
I would suggest to check below :-

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.


Aidan KeenanAidan Keenan
Hi Vinit

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);
Vinit_KumarVinit_Kumar
You need to execute the code in Dev console :--

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.
Aidan KeenanAidan Keenan
if(status.Days_Idle__c > 30)
           status.ts2__Status__c ='Closed';
         tsList.add(status);

Where do I need to put another IF ?  Really appreciate the help with this.
Vinit_KumarVinit_Kumar
Can you put a debug statement inside your if condition to verify whether you are enterig the 'if'  condition.Put something like :-

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.
Aidan KeenanAidan Keenan
Ok so I have this class now 

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.
Aidan KeenanAidan Keenan
I used this code to schedule the job every day at 12 but got this execute anonymnous error 


jobOrderStatusUpdate5 jobOrderUpdate = new jobOrderStatusUpdate5();
String sch = '0 0 12 * * ?';
String jobID = system.schedule('Update Job ', sch, jobOrderUpdate);
Vinit_KumarVinit_Kumar
Go to Dev console,open execute anonymous window and run the below code :-
jobOrderStatusUpdate5 m = new jobOrderStatusUpdate5(); 
String sch = '20 30 8 10 2 ?';
String jobID = system.schedule('Update Job', sch, m);

// where replace Strin sch value with current time
20 - hours
30 - min
8 - seconds
10 - day
2 - month
?- year
You can visit below link to learn more about paremeters :-

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm
Vinit_KumarVinit_Kumar
Whats the error you are getting in execute anonymous window ??
Aidan KeenanAidan Keenan
Changed  sch to String sch = '20 30 8 10 2 ?';  and got no error.  It says that 11:04:30:061 EXECUTION_STARTED    I take it this has executed now and I need to change the code to get this to run today?
Vinit_KumarVinit_Kumar
Good to know it worked :)
Aidan KeenanAidan Keenan
It seems to be scheduled to run in 2015 ?  Everytime I enter the code to get it to run at 1 pm every day I get the execution anonymous error 
Vinit_KumarVinit_Kumar
Whats the error you are getting ?

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..
Aidan KeenanAidan Keenan
Next scheduled start time is 10/02/2015 08:30  using that code and the job has not started and in the Apex jobs shows as queued ? 
Aidan KeenanAidan Keenan
jobOrderStatusUpdate5 m = new jobOrderStatusUpdate5();
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.
Aidan KeenanAidan Keenan
When I replace sch with  21 30 8 10 2 ?  the code executes 
Vinit_KumarVinit_Kumar
Ohk,try the below code,this should work.

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..
Aidan KeenanAidan Keenan
I got it to start running at 12 pm   but again it is saying that it has started in the scheduled job section but in the apex jobs section it is saying that it is status = queued which is the same as when I used the UI to schedule the job.  Surely this should change to processing?
Vinit_KumarVinit_Kumar
Yes,it would change to processing the time it needs to run.
Aidan KeenanAidan Keenan
It is just staying at queued which leads me to believe the job is still not working?  It seems to be never actually running even tho in the scheduled jobs section it says it has started.  
Aidan KeenanAidan Keenan
Hi Vinit 

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.