You need to sign in to do that
Don't have an account?
Sean Clark
Need to schedule a Apex Trigger to run Daily
Hi All,
I have the below Apex Trigger and need to schedule it to run daily.
I am new to coding and this is one of my first pieces of code.
Could someone please advice me on how to do this?
---------------------------------------------------------------------------------------------
trigger CloseVacancies on TR1__Job__c (after insert, after update) {
Set <String> jobID = New Set <String> ();
For (TR1__Job__c job: Trigger.new){
if (job.Id != Null){
jobID.add(job.Id);
}
}
if(jobID.size ()> 0) {
List <TR1__Job__c> upJobList = new List <TR1__Job__c> ();
for (TR1__Job__c vac: [SELECT Id, TR1__Status__c, TR1__Days_Open__c FROM TR1__Job__c WHERE id in: jobID AND TR1__Days_Open__c >= 42 AND TR1__Status__c != 'Closed']){
vac.TR1__Status__c = 'Closed';
vac.TR1__Closed_Date__c = system.Date.today();
upJobList.add(vac);
}
If(upJobList.size()> 0)
update upJobList;
}
}
---------------------------------------------------------------------------------------------
I have the below Apex Trigger and need to schedule it to run daily.
I am new to coding and this is one of my first pieces of code.
Could someone please advice me on how to do this?
---------------------------------------------------------------------------------------------
trigger CloseVacancies on TR1__Job__c (after insert, after update) {
Set <String> jobID = New Set <String> ();
For (TR1__Job__c job: Trigger.new){
if (job.Id != Null){
jobID.add(job.Id);
}
}
if(jobID.size ()> 0) {
List <TR1__Job__c> upJobList = new List <TR1__Job__c> ();
for (TR1__Job__c vac: [SELECT Id, TR1__Status__c, TR1__Days_Open__c FROM TR1__Job__c WHERE id in: jobID AND TR1__Days_Open__c >= 42 AND TR1__Status__c != 'Closed']){
vac.TR1__Status__c = 'Closed';
vac.TR1__Closed_Date__c = system.Date.today();
upJobList.add(vac);
}
If(upJobList.size()> 0)
update upJobList;
}
}
---------------------------------------------------------------------------------------------
No, you can schedule the trigger. Trigger always fires on event when some DML(Insert/Update/Delete) Operation is performed.
Put your logic in Apex Class and then that class can be scheduled.
To Know the Concept of Apex Schedular visit to the link:--
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_scheduled
Example Schedular Class is given.
The above code updates the account at a particular time.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
A trigger only runs on DML commands which are insert, update and delete.
You can not schedule a trigger to run according to time intervals.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com