Skip to content

Instantly share code, notes, and snippets.

@arcanadev
Created February 18, 2021 22:50
Show Gist options
  • Save arcanadev/4fc517587ac4ad13d515b99abbfa2f3a to your computer and use it in GitHub Desktop.
Save arcanadev/4fc517587ac4ad13d515b99abbfa2f3a to your computer and use it in GitHub Desktop.
Link jobs using a Job Trigger #adtempus #api #version4

This sample shows how to link two jobs using a Job Trigger.

In this example, the Job 3 waits for successful completion of both Job 1 and Job 2.

using (var session = Scheduler.Connect(".", LoginAuthenticationType.Windows, "", ""))
{
using (var context = session.NewDataContext())
{
//retrieve the jobs
var job1=context.GetJob("Job 1");
var job2 = context.GetJob("Job 2");
var job3 = context.GetJob("Job 3");
//create a Job Trigger
var jobTrigger = context.CreateObject(ClassID.JobTrigger) as JobTrigger;
jobTrigger.TriggerOptions = JobTriggerOptions.WaitForAll;
//create and add a JobTriggerTarget for each of the jobs that you need to wait on
var target=context.CreateObject(ClassID.JobTriggerTarget) as JobTriggerTarget;
target.TargetJobOID=job1.OID;
target.TargetInstance= JobConditionInstance.MostRecentSinceLastExecution;
target.TriggerType = JobTriggerType.JobSucceeded;
jobTrigger.Targets.Add(target);
target = context.CreateObject(ClassID.JobTriggerTarget) as JobTriggerTarget;
target.TargetJobOID = job2.OID;
target.TargetInstance = JobConditionInstance.MostRecentSinceLastExecution;
target.TriggerType = JobTriggerType.JobSucceeded;
jobTrigger.Targets.Add(target);
job3.Triggers.Add(jobTrigger);
job3.Save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment