Clone Tasks using API - VisualCron - Forum

Community forum

PrimeViewDevelopers
2014-07-16T15:35:12Z
I am currently trying to clone job tasks programmatically to other Jobs. I have the following code:

            var mytask = s.Jobs.GetJobByName("Template Job").Tasks[0];
            var myclone = mytask.CloneType();
            myclone.Name = "Ping Data Sources Test";
            var myjob = s.Jobs.GetJobByName("Target Job");
            myjob.Tasks.Add(myclone);
            s.Jobs.Tasks.Update(myclone);
            s.Jobs.Update(myjob);


This code executes fine, but causes the VC Client to crash (the server is completely fine). The crash happens because of a duplicated ID number. Do I need to generate a new ID number for the cloned task?
Support
2014-07-17T08:02:55Z
Yes, please do the following:

var mytask = s.Jobs.GetJobByName("Template Job").Tasks[0];
var myclone = mytask.CloneType();

myclone.Id = Guid.NewGuid().ToString(); // create new Task Id
myclone.Stats.TaskId = myclone.Id; // copy new Id to this place as well
myclone.ResetStats(); // reset to not run yet etc.

myclone.Name = "Ping Data Sources Test";
var myjob = s.Jobs.GetJobByName("Target Job");

myclone.Order = myjob.Tasks.Count + 1; // update Order property
myclone.JobId = myjob.Id; // if cloning to another Job

myjob.Tasks.Add(myclone);
s.Jobs.Tasks.Update(myclone);
s.Jobs.Update(myjob);

Henrik
Support
http://www.visualcron.com 
Please like  VisualCron on facebook!
Similar Topics
Users browsing this topic
Scroll to Top