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
Etienne_spEtienne_sp 

System.abortJob() from trigger not working

Hi,

I've a trigger which add new jobs successfully (with system.schedule), but when I want to delete a job, the method System.abortJob(id) isn't working (job still scheduled).
Do you know where this problem may come from?

Thank you,

Saravanan @CreationSaravanan @Creation

when you are adding a schedule job through the trigger that time you need to store the schedule job id in some where.e.g fields

 

Id SchJobId=System.schedule(SchClassName, '0 30 * * * ?', new Monitor_Field_Scheduler());

 

SchJobId -is new schedule job id.

 

by using this SchJobId again you need to query the running schedule job id from the CronTrigger like below query

 

CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE Id = :SchJobId];

 

now use the ct.id to abort the created schedule job like below

 

System.abortJob(ct.id);

 

now it will abort the job,are you followed the same job.

 

 

 

Etienne_spEtienne_sp

Thank you for your reply.

But it's what I do, and it doesn't work.

When I add the schedule job, I store its id, and when I want to delete it (in the trigger), I use System.abortJob(the_correct_job_id) but there is still the job!

If I execute the System.abortJob() from an apex class it works (the job is deleted), but not from my trigger. Do you know why?

Saravanan @CreationSaravanan @Creation

Hi,

I dont have the reson,for you question 'not working on trigger?'.

 

But as per the best practice you will call the class method from the trigger.

 

over the class method you need to perform your operation rigth.then it will work fine.

 

i need some more time to look at your problem,I will get you back once i got answer for your question.

 

Etienne_spEtienne_sp

I tried to create an apex class to delete the scheduled job, which is call by the trigger, but it still doesn't work. I don't get any error but my job is still scheduled.

if the deleteJob() method from the apex class is called by a visual force page it works (job is deleted) but when called by the trigger it doesn't work! I don't understand.

 

-Class:

public class DeleteScheduledJob {

   public void deleteJob() {
       System.abortJob('correct_job_id');

   }

}

 

-Code in the trigger:

new DeleteScheduledJob().deleteJob();

 

-Visual force page:

<apex:page controller="DeleteScheduledJob">
  <apex:form >
      <apex:commandButton action="{!deleteJob}"/>
  </apex:form>
</apex:page>

 

force_knightforce_knight

When you call System.abort inside DML transaction, it won't get aborted. Abort will silently fail without any error message.