A: Anatomy of a Script Event

OnInform and the Trestle Repository

The following e.Info strings that are available for an onInform(sender, e) event:

//
/*
namespace Trestle.Data
{
/// <summary>
/// Convenience enum to call the RaiseInform() method of IHandleInfo
/// implementers. NEVER change these names, because IHandleInfo expects
/// a string value, so event handlers will convert this value to a
/// string before deciding on a course of action.
/// </summary>

public enum RepositoryOperation
{
/// <summary>
/// The item was created by the repository using the standard
/// <see cref="IRepository.Create(StringKey)"/> method.
/// </summary>
CreatedNew,

/// <summary>
/// The item was created by the repository using either the
/// standard <see cref="IRepository.Create(StringKey)"/> method
/// or <see cref="IRepository.CreateFrom(string)"/> method.
/// </summary>
Created,

/// <summary>
/// The item was created by the repository using the non-
/// standard <see cref="IRepository.CreateFrom(string)"/>
// method.
/// </summary>
CreatedFrom,

/// <summary>
/// Raised when ownership keys are navigated to a child record.
/// </summary>
KeysNavigated,

/// <summary>
/// The item will be edited in some way.
/// </summary>
Editing,

/// <summary>
/// The item will possibly be saved to the data store.
/// </summary>
Saving,

/// <summary>
/// The record will possibly be saved, and has been validated.
/// This is the last operation before the record is enlisted in
/// a transaction. NOTE: No side-effects from the repository
/// are allowed after this. If the record was not going to be
/// saved, but a change here makes it so that it will be, it is
/// the responsibility of the implementer to also set things
/// like modification date, etc.
/// </summary>
Validated,

/// <summary>
/// The item was permanently saved to the data store for the
/// first time.
/// </summary>
SavedNew,

/// <summary>
/// The item was permanently saved to the data store. Note that
/// this is always raised, whether the item was new or not.
/// </summary>
Saved,

/// <summary>
/// The existing item was permanently saved to the data store.
/// </summary>
SavedExisting,

/// <summary>
/// The item will be deleted from the data store.
/// </summary>
Deleting,

/// <summary>
/// The item was permanently deleted from the data store.
/// </summary>
Deleted,

/// <summary>
/// An unknown or invalid operation.
/// </summary>
Invalid
}
}

*/

// This example is for: OpenSale to intercept and change the SaleNumber.
def onInform(sender, e) {
// this could almost be onRepositoryOperation(...)
if e.Info == 'Created' {
sender.SaleNumber = sender.SaleNumber.Replace('S-01-', 'S');
}
}

Avoid OnPropertyChanged

Properties are accessible on the sender but this method is not recommended. Use OnDataChanged instead.

// This function checks for a due date change and adds to the notes
// field if it has.


// IMPORTS

from System import Environment, DateTime;

// HELPER FUNCTION

//Truncate DateTime to Day
def TruncToDay(dt)
{
return DateTime(dt.Year, dt.Month, dt.Day);
}

// PRIMARY FUNCTIONS

def onInform(sender, e) {
if e.Info == 'Saving' {
var trk = sender.GetTracker();
if not trk.IsNew {
if trk.ChangedProperties.ContainsKey('DateDue') {
var oldValue = trk.ChangedProperties['DateDue'];
if oldValue {
var newValue = sender.DateDue;

// verify date (ie. month/day/year) has actually
// changed and then add a note to the note field
if TruncToDay(newValue) != TruncToDay(oldValue){
var notes = sender.Notes ?? '';
notes += string.Format("On {0:yyyy-MM-dd}, the due date
was changed from {1:yyyy-MM-dd} to {2:yyyy-MM-dd}
by {3}{4}", DateTime.Today, oldValue, newValue,
host.user().Identity.Name, Environment.NewLine);
sender.Notes = notes;
}
}
}
}
}
}

How to Use OnDataChanged

We'll be expanding the documentation in this section soon. Please contact us if you have questions.

Raising a Custom Inform Event on Another Object

We'll be expanding the documentation in this section soon. Please contact us if you have questions.


How did we do?


Powered by HelpDocs (opens in a new tab)