Set Job Variables from values in a trigger file

Language: C#

View on GitHub to download or comment.

See the User script help topic for additional information.

Samples index

This script can be used if you want to trigger a job using a file and set Job Variables from values in the file.

To use the script, create a new Shared Script in adTempus using C# as the language and this script as the script body. In your job, configure the File Trigger as normal and add a Script Execution Task as the first step of the job to run the Shared Script.

The script gets the name of the trigger file, reads it, and sets the variables.

sample.cs
using System;
using System.IO;
using System.Collections.Generic;
using ArcanaDevelopment.adTempus.ApplicationIntegration;
using ArcanaDevelopment.adTempus.Shared;

/*
This script reads the file that triggered the job and sets Job Variables based on the information in the file.
Each line of the file should be a variable setting in the form
    variable name=variable value
    
See www.arcanadev.com/support/CodeSamples/932d01813535fb4c5e86d80f8897847d for more information.    
*/

namespace UserScript
{
    public class UserScript : ArcanaDevelopment.adTempus.ApplicationIntegration.UserScriptBase
    {
        public override Object Run()
        {
            //get the name of the file that triggered the job (set by the FileTrigger)
            var fileName = adTempus.JobVariables["FileTrigger.FileName"];
            if (!File.Exists(fileName))
            {
                adTempus.LogMessage(MessageTypeEnum.Error, 0, $"Trigger file {fileName} not found");

                //return 8 to signal failure. This will cause the job to end at this point. If you want
                //the job to ignore this and keep running, change this to return 0.
                return 8;
            }

            using (var file = File.OpenText(fileName))
            {
                //loop to read each line of the file
                while (true)
                {
                    var line = file.ReadLine();
                    if (line == null)
                        break;

                    //find the first "=" on the line and split the line at that point to get the variable name and value.
                    var index = line.IndexOf("=");
                    if (index > 0)
                    {
                        var name = line.Substring(0, index).Trim();
                        var value = line.Substring(index + 1).Trim();

                        //set the variable. This setting is effective only for the currently-executing instance of the job
                        adTempus.JobVariables[name] = value;
                    }
                }
            }
            return 0;
        }
    }
}

Comments

View on GitHub to comment.