Community forum

Please note that VisualCron support is not actively monitoring this community forum. Please use our contact page for contacting the VisualCron support directly.


burnsmeister
2015-04-22T15:20:25Z
Hi Folks.
I have inherited some code which programmatically creates jobs and adds notifications. The code was written using VC API v6.x.x but having upgraded to VC API v7.x.x it will no longer compile. I realise that the way VC handles notifications has changed but I cannot find anywhere explaining how to do this programmatically.

The original code is shown below -
NotificationClass nc = job.NewNotification(NotificationClass.NType.SQL);
nc.Description = functionDisplay + descriptionPostfix;
nc.SQL.ConnectionId = sqlCon.Id;
nc.SQL.CommandType = System.Data.CommandType.Text;

string userId = new User(DB, Configuration.system).id;

if (type.Equals("ERROR"))
	nc.SQL.Command = "spSysErrorInsert " + userId + "," + functionId.ToString() + "," + DB.WrapValue(Environment.MachineName) + "," + DB.WrapValue("VisualCron") + "," + DB.WrapValue("Execution Error") + "," + DB.WrapValue("Check VisualCron Logs") + ",null,null,null,null,null";
else
	nc.SQL.Command = "spSysActivityInsert " + userId + "," + functionId.ToString() + ",null," + DB.WrapValue(type) + "," + DB.WrapValue(type.Substring(0, 1) + type.Substring(1).ToLower()) + ",null,null,null,null,null";

//Add to notification server
server.Notifications.Add(nc);

//Attach notification to event based on passed type
if (type.Equals("START"))
	job.StartNotifications.Add(nc.Id);
else if (type.Equals("END"))
	job.CompleteNotifications.Add(nc.Id);
else if (type.Equals("ERROR"))
	job.ErrorNotifications.Add(nc.Id);

This line now fails - server.Notifications.Add(nc);

I cannot work out how to create a TaskNotificationClass which seems to have replaced the NotificationClass.

All help greatly appreciated!!

Regards.

John
Sponsor
Forum information
Support
2015-04-22T21:31:27Z
The StartNotifications etc are no longer used as TaskNotifications are connected to what we call Flows. Here is the code below that we use to convert old Notification to new:


            If j.StartNotifications.Count > 0 Then
                For Each startNotificationId As String In j.StartNotifications
                    Dim tf As New FlowClass
                    tf.ActionType = FlowClass.FlowActionT.RunNotification
                    tf.NotificationId = startNotificationId
                    tf.EventType = FlowClass.FlowEventT.OnStart
                    j.Flow.Add(tf)
                Next
                j.StartNotifications.Clear()
            End If
            ' complete notifications
            If j.CompleteNotifications.Count > 0 Then
                For Each completeNotificationId As String In j.CompleteNotifications
                    Dim tf As New FlowClass
                    tf.ActionType = FlowClass.FlowActionT.RunNotification
                    tf.NotificationId = completeNotificationId
                    tf.EventType = FlowClass.FlowEventT.OnComplete
                    j.Flow.Add(tf)
                Next
                j.CompleteNotifications.Clear()
            End If
            ' error notifications
            If j.ErrorNotifications.Count > 0 Then
                For Each errorNotificationId As String In j.ErrorNotifications
                    Dim tf As New FlowClass
                    tf.ActionType = FlowClass.FlowActionT.RunNotification
                    tf.NotificationId = errorNotificationId
                    tf.EventType = FlowClass.FlowEventT.OnError
                    j.Flow.Add(tf)
                Next
                j.ErrorNotifications.Clear()
            End If

Henrik
Support
http://www.visualcron.com 
Please like  VisualCron on facebook!
burnsmeister
2015-04-23T08:35:30Z
Hi Henrik.

Thanks for getting back to me. I'm not sure that what you propose is exactly what I'm trying to achive so I'll give a bit more background. The jobs and tasks are being created on the fly when we sync with VC. So effectively on each sync the jobs/tasks are removed and recreated. I'm sure there was a good reason why it was done that way but I wasn't involved in the development of the code so can't really comment too much.

So basically each job is created and in VC v6.x.x a new notification was programmatically created on the server (as per the code above) and assigned to the job. It's this part that now doesn't work - the creation of the notification on the server. Is it possible that you could show me an example of how to programmatically create a notification on the server?

Many thanks for your help!

John
ErikC
2015-04-23T09:19:20Z
Hi,

You need to create the notification first in VC. You can do this programmaticaly, but this code will use the allready created notifications.

To see all the notification id's you can need use this code:

List<TaskNotificationClass> notifications = s.Notifications.GetAll();
Console.WriteLine("Notification ID                      -- Name");
Console.WriteLine("---------------------------------------------");
foreach (TaskNotificationClass notification in notifications)
{
    Console.WriteLine(notification.Task.Id + " -- " + notification.Task.Name);
 }


On a job you create a flow and use the right notification id .

List<JobClass> jobs = s.Jobs.GetAll();
JobClass job = jobs[indexnumber]; //indexnumber to get one of your jobs
FlowClass fc = new FlowClass();
fc.ActionType = FlowClass.FlowActionT.RunNotification;
fc.EventType = FlowClass.FlowEventT.OnComplete; //choose from enum
fc.NotificationId = notificationID; //use one from the list 
job.Flow.Add(fc);
s.Jobs.Update(job);


On a task you do the same:

List<JobClass> jobs = s.Jobs.GetAll();
JobClass job = jobs[indexnumber];  //indexnumber to get one of your jobs
TaskClass task = job.Tasks[indexnumber];  //indexnumber to get one of your tasks inside the job
FlowClass fc = new FlowClass();
fc.ActionType = FlowClass.FlowActionT.RunNotification;
fc.EventType = FlowClass.FlowEventT.OnComplete;  //choose from enum
fc.NotificationId = notificationID;  //use one from the list
task.Flow.Add(fc);
s.Jobs.Tasks.Update(task);


That's it.
I use this also to put my notifications on jobs and tasks and it rocks!

Regards,
Erik
Uses Visualcron since 2006.
burnsmeister
2015-04-23T09:48:44Z
Hi Erik.

Thanks for your reply. I see exactly what you are doing there - really clearly.

My problem still is that I need to be able to create the notifications at the time I'm creating the job - programmatically.... I can't really create them on the server beforehand.

Many thanks for your help.

John

ErikC
2015-04-23T09:58:35Z
Ok,

Creating a notification is like this:

//1st create a task
TaskClass tc = new TaskClass();
//you used an SQL task, so create the type
TaskSQLClass tsqlc = new TaskSQLClass();
//fill in your properties
tsqlc.CommandType = System.Data.CommandType.Text;
//than join the both together
tc.SQL = tsqlc;

//now create a notification
TaskNotificationClass tnc = new TaskNotificationClass();
//join the task to the notification
tnc.Task = tc;

//add the notification to the server
s.Notifications.Add(tnc); 


Regards
Erik
Uses Visualcron since 2006.
burnsmeister
2015-04-23T10:07:41Z
Great thanks Erik - I'll give that a try today and see if I can make it work.

Really appreciate your help!

Cheers.

John
ErikC
2015-04-23T10:10:11Z
Hi John,

Glad i could help. 😁
Please let us know in the forum if you sorted it out, so I can close this issue.

Regards
Erik

Uses Visualcron since 2006.
burnsmeister
2015-04-23T16:14:54Z
Ok so partial success! I can now create the notifications but with some missing information. For some reason the CommandText is not being stored when the notifcation is created.

I've also managed to add an 'on error' flow event to the job but haven't been able to hook that up to the notification.

Making progress but not quite there yet!

Happy for you to close the issue and I'll come back to the forum if I need any more help.

Cheers.

John



Support
2015-04-23T16:40:53Z
Use:

Task.SQL.EncryptedCommand = clientObject.Encrypt("Your command")

Henrik
Support
http://www.visualcron.com 
Please like  VisualCron on facebook!
burnsmeister
2015-04-23T16:54:57Z
YES!

That's got the sql text storing nicely. I'm sure with the other code examples you showed me wearlier I can work out how to attach the notification to the flow event.

Help much appreciated Henrik - genius!
Scroll to Top