By Jeff Pagley - 2/7/2011
Hi SF,
My client has asked me to send an email alert when a value has changed in a field for a record. Is there an event the BO fires that I can handle which tells me what fields has changed and the before and after values of those fields?
Or does anyone have any suggestions on how I can do this?
Thanks,
Jeff
Using C#/Visual Studio 2008
|
By Keith Chisarik - 2/7/2011
If you want to do it via BO you can use the DataRowVersion on the underlying datarows
If NOT BO.CurrentRow.Item(colCount, Data.DataRowVersion.Original) = BO.CurrentRow.Item(colCount, Data.DataRowVersion.Current) Then
or if you want it all on the SQL Server you could do a trigger that looks at the values in the internal INSERTED AND DELETED tables
|
By Jeff Pagley - 2/7/2011
Hi Keith,
How can I tell what field has changed on the BO? Is there a way to know what are the before and after values of the field of the BO? And what BO event would I handle to access this info? Any code examples would be appreciated.
I don't what to audit the information, but I want to send an email when certain fields' values changed in a record. Therefore, I don't need to setup SQL triggers.
Thanks,
Jeff
Using C#/VS 2008
|
By Keith Chisarik - 2/7/2011
I put a line of code on my reply that should get you pointed in the right direction:
Call the below in a loop for each column in each row you want to check.
If NOT BO.CurrentRow.Item(colCount, Data.DataRowVersion.Original) = BO.CurrentRow.Item(colCount, Data.DataRowVersion.Current) Then
Additional Info:
http://msdn.microsoft.com/en-us/library/system.data.datarowversion.aspx
|
By Jeff Pagley - 2/7/2011
I will start from there as you suggested.
Thank you for your help.
Jeff
|
By Sam Tenney - 3/3/2011
I am a beginner and I must be missing something simple.
In the BeforeSave method of my BO, I want to compare the current value of a field to the original value. I am using C# and Visual Studio 2008 and when I try to enter:
If (!BO.CurrentRow.Item(colCount, Data.DataRowVersion.Original) == BO.CurrentRow.Item(colCount, Data.DataRowVersion.Current))
it appears that BO.CurrentRow does not have a method named Item. What am I doing wrong?
Sam Tenney
|
By Keith Chisarik - 3/3/2011
if BO is a business object, its currentrow has an item property, in the code snippet above BO is just a placeholder for an actual business object
Quick and dirty code you can use on any SF form with a Primary BO is below. Not efficient if the BO has many rows, but you can always skinny it down if you need to.
For colCount = 0 To Me.PrimaryBusinessObject.CurrentDataTable.Columns.Count - 1
If Not Me.PrimaryBusinessObject.CurrentRow.Item(colCount, Data.DataRowVersion.Original) = Me.PrimaryBusinessObject.CurrentRow.Item(colCount, Data.DataRowVersion.Current) Then
MsgBox("Changed row old value: " & Me.PrimaryBusinessObject.CurrentRow.Item(colCount, Data.DataRowVersion.Original) & vbCrLf & "Changed row new value: " & Me.PrimaryBusinessObject.CurrentRow.Item(colCount, Data.DataRowVersion.Current))
End If
Next
|
By Jeff Pagley - 3/3/2011
I have been testing this code in the BO_BeforeSave event and it seems to work:
// Established a local for the BO's current row
DataRow dataRow = this.CurrentRow;
// Iterate through the columns of the data table
foreach (DataColumn item in this.CurrentDataTable.Columns)
{
// Determine what data row column to retrieve it's value
if (item.ColumnName.Equals("cc_Description") || item.ColumnName.Equals("coco_Description"))
{
// Compare the original version with the current to determine if it changed
if (dataRow[item.ColumnName, DataRowVersion.Original] != dataRow[item.ColumnName, DataRowVersion.Current])
{
Console.WriteLine(String.Format("Table: {0}", this.TableName));
Console.WriteLine(String.Format("Col ({0}) Original = {1} Current = {2}", item.ColumnName, dataRow[item.ColumnName, DataRowVersion.Original].ToString(), dataRow[item.ColumnName, DataRowVersion.Current].ToString()));
}
}
}
I hope this is helpful.
Jeff
|
By Sam Tenney - 3/4/2011
Thanks Keith and Jeff,
I think I finally figured out that in C#, the CurrentRow does not have a property named Item. So the C# code I am using is:
if (! BO.CurrentRow[colCount, Data.DataRowVersion.Original] == BO.CurrentRow[colCount, Data.DataRowVersion.Current])
which I think is logically equivalent to Keith's VB code as follows:
If NOT BO.CurrentRow.Item(colCount, Data.DataRowVersion.Original) = BO.CurrentRow.Item(colCount, Data.DataRowVersion.Current)
Notice the C# code does not use an Item property. If I am off base, please let me know.
Sam Tenney
|
|