General Discussions on developing custom functions with FileMaker Advanced

326 Views    -    2 Replies


Username Post: Auditing Fields Modified By Script        (Topic#215763)
Your continued generosity and support of FMForums is greatly appreciated.
frinholp 
enthusiast
Posts: 44

Post Rank (AVG):
            


FMP: 11
OS: Windows 7
Skill: Entry Level



Tweet This! Tweet This Post!
07-21-10 09:40 AM - Post#361014     - Post Rank:             


Hi All

I have found a fantastic video tutorial on logging changes made to records freely available at the following url:

Filemaker Magazine Super Audit Logging Tutorial

Moderators: Please edit this post if this infringes copyright. I am assuming not on the basis Matt Petrowski mentions copyright issues in the video.

All credit goes to Matt Petrowski and the original function creator Ray Cologon,NightWing Enterprises, Melbourne, Australia
www.nightwing.com.au/FileMaker

Function usage: AuditLog (Logfield; Fields; Format; Seperator)

The code to the function is as follows:

Code:

  Let( [ // Setup _Trigger = Fields; _LogLength = Length(LogField); _Script = Get(ScriptName); _Repetition = Get(ActiveRepetitionNumber); _RepeatFlag = If(_Repetition > 1; "[" & _Repetition & "]"); _FieldName = Get(ActiveFieldName) & _RepeatFlag; // Locate Any Previous Field Value in Log _FieldInLog = Position(LogField; Separator & _FieldName & Separator; 1; 1); _Divider = Separator & "-»" & Separator; _Start = Position(LogField; _Divider; _FieldInLog; 1) + Length(_Divider); _End = Position(LogField & ¶; ¶; _FieldInLog; 1); _PreviousValue = If(_FieldInLog; Middle(LogField; _Start; _End - _Start); "[---]"); // New Field Value (fixes returns) _FieldValue = GetField(_FieldName); _FixedValue = Substitute(_FieldValue; ¶; "‡"); _NewValue = If(Length(_FieldValue); _FixedValue; "[null]"); // Format Values _%Date = GetAsDate(Get(CurrentHostTimeStamp)); _%Time = GetAsTime(Get(CurrentHostTimeStamp)); _%Account = Get(AccountName); _%Field = _FieldName; _%Change = _PreviousValue & Separator & "-»" & Separator & _NewValue; _%Table = Get (LayoutTableName); _%Layout = Get(LayoutName); _%Key = GetAsNumber (Get(CurrentHostTimeStamp)) & _PreviousValue & _NewValue; _LogFormat = Substitute( Format;                               ["%Date"; _%Date & Separator];                               ["%Time"; _%Time & Separator];                               ["%Account"; _%Account & Separator];                               ["%Field"; _%Field & Separator];                               ["%Change"; _%Change & Separator];                               ["%Table"; _%Table & Separator];                               ["%Layout"; _%Layout & Separator];                               ["%Key"; _%Key & Separator]                               ); _LogString = Left( _LogFormat; Length( _LogFormat ) - Length( Separator ) ) ]; If( Length(_FieldName) and Length(_Script) = 0;   _LogString & Left(¶; _LogLength) & LogField;   LogField   ) )  // Original Function by Ray Cologon // NightWing Enterprises, Melbourne, Australia // www.nightwing.com.au/FileMaker // Revised by Matt Petrowsky 





I have been playing around with this for hours now trying to get the function to audit changes made by a script. Obviously my coding skills aren't quite up to it.

I have created a test script attached to a button that simply uses Set Field to change the contents of a field which has been passed into the function as a parameter, but the change is not logged. I have tried both with and without using Commit record in the test script.

Can anyone give me any pointers?

Thanks in advance

Lee










frinholp 
enthusiast
Posts: 44

Post Rank (AVG):
            


FMP: 11
OS: Windows 7
Skill: Entry Level



Tweet This! Tweet This Post!
07-21-10 04:21 PM - Post#361047     - Post Rank:             
    In response to frinholp

Sorry all,

I should of listened to the tutorial more carefully. Oops!

I have tried creating an analogue of AuditLog which I have named ScriptLog.

ScriptLog(LogField; FieldChanged; Format; Separator)

Let(
[
// Setup

_LogLength = Length(LogField);
_Script = Get(ScriptName);
_Repetition = Get(ActiveRepetitionNumber);
_RepeatFlag = If(_Repetition > 1; "[" & _Repetition & "]");
_FieldName = FieldChanged;

// Locate Any Previous Field Value in Log

_FieldInLog = Position(LogField; Separator & _FieldName & Separator; 1; 1);
_Divider = Separator & "-»" & Separator;
_Start = Position(LogField; _Divider; _FieldInLog; 1) + Length(_Divider);
_End = Position(LogField & ¶; ¶; _FieldInLog; 1);
_PreviousValue = If(_FieldInLog; Middle(LogField; _Start; _End - _Start); "[---]");

// New Field Value (fixes returns)

_FieldValue = GetField(_FieldName);
_FixedValue = Substitute(_FieldValue; ¶; "‡");
_NewValue = If(Length(_FieldValue); _FixedValue; "[null]");

// Format Values

_%Date = GetAsDate(Get(CurrentHostTimeStamp));
_%Time = GetAsTime(Get(CurrentHostTimeStamp));
_%Account = Get(AccountName);
_%Field = _FieldName;
_%Script = _Script;
_%Change = _PreviousValue & Separator & "-»" & Separator & _NewValue;
_%Table = Get (LayoutTableName);
_%Layout = Get(LayoutName);
_%Key = GetAsNumber (Get(CurrentHostTimeStamp)) & _PreviousValue & _NewValue;

_LogFormat = Substitute( Format;
["%Date"; _%Date & Separator];
["%Time"; _%Time & Separator];
["%Account"; _%Account & Separator];
["%Field"; _%Field & Separator];
["%Change"; _%Change & Separator];
["%Table"; _%Table & Separator];
["%Layout"; _%Layout & Separator];
["%Key"; _%Key & Separator]
);
_LogString = Left( _LogFormat; Length( _LogFormat ) - Length( Separator ) )

];

If( Length(_FieldName) and Length(_Script) = 0;
_LogString & Left(¶; _LogLength) & LogField;
LogField
)

)

I am using a test script:

SetField[fieldbeingchanged; value)]

SetField[logfield; ScriptLog(logfield; fieldbeingchanged; "%Date%Time%Account%Field%Change" ; " " )

but the logfield is not being updated.

Can anyone see any error in my code?

Thanks in advance

Lee











Your continued generosity and support of FMForums is greatly appreciated.
frinholp 
enthusiast
Posts: 44

Post Rank (AVG):
            


FMP: 11
OS: Windows 7
Skill: Entry Level



Tweet This! Tweet This Post!
07-22-10 03:56 AM - Post#361067     - Post Rank:             
    In response to frinholp

Ok, problem solved

Took me a bit of time to sort this one as I'm not an experienced developer.

I'll give you my solution incase anyone else would like to re-use the code. Again all credit to Matt Petrowski and Ray Cologon.

ScriptLog(LogField; FieldChanged; Format; Separator) // Do not use full table and field reference for FieldChanged, just field name if using in conjunction with AuditLog

Let(
[
// Set field name to be searched for

_FieldName = FieldChanged;

// Locate Any Previous Field Value in Log

_FieldInLog = Position(LogField; Separator & _FieldName & Separator; 1; 1);
_Divider = Separator & "-»" & Separator;
_Start = Position(LogField; _Divider; _FieldInLog; 1) + Length(_Divider);
_End = Position(LogField & ¶; ¶; _FieldInLog; 1);
_PreviousValue = If(_FieldInLog; Middle(LogField; _Start; _End - _Start); "[---]");

// New Field Value (fixes returns)

_FieldValue = GetField(_FieldName);
_FixedValue = Substitute(_FieldValue; ¶; "‡");
_NewValue = If(Length(_FieldValue); _FixedValue; "[null]");

// Format Values

_%Date = GetAsDate(Get(CurrentHostTimeStamp));
_%Time = GetAsTime(Get(CurrentHostTimeStamp));
_%Account = Get(AccountName);
_%Field = _FieldName;
_%Change = _PreviousValue & Separator & "-»" & Separator & _NewValue;
_%Table = Get (LayoutTableName);
_%Layout = Get(LayoutName);
_%Key = GetAsNumber (Get(CurrentHostTimeStamp)) & _PreviousValue & _NewValue;

_LogFormat = Substitute( Format;
["%Date"; _%Date & Separator];
["%Time"; _%Time & Separator];
["%Account"; _%Account & Separator];
["%Field"; _%Field & Separator];
["%Change"; _%Change & Separator];
["%Table"; _%Table & Separator];
["%Layout"; _%Layout & Separator];
["%Key"; _%Key & Separator]
);
_LogString = Left( _LogFormat; Length( _LogFormat ) - Length( Separator ) )

];

_LogString & ¶ & LogField

)

// Original Function by Ray Cologon
// NightWing Enterprises, Melbourne, Australia
// www.nightwing.com.au/FileMaker

// Revised by Matt Petrowsky & Me


Lee










Icon Legend Permissions Topic Options
Print Topic

Email Topic

326 Views
Welcome Guest...
Enter your username and password to login. If you do not have a username you can register one here

Username

Password

Remember me. Help



Forgot Password...


Quick Links
Recent Posts
Active Topics
No Replies
Recent Files
Functions & Scripts
FileMaker Pro Help
FileMaker on Twitter
FileMaker Marketplace

Custom Search

Find FileMaker Developers

Don't have time to wait,
hire a developer now!

Recent Members
Welcome them to our community!
Find FileMaker Jobs

Want a new career?
Find your new job now!

Joy of Tech
Latest Joy of Tech!

Recent Topics
Recent Hot Topics
Contribute
With your generosity we can make some real magic happen!. Support your favorite online FileMaker community...
FM Forums.com


Click here...


Or a Donation of any amount.


Thanks for your support!

Active Blogs
0 Recent blogs:
People to Post Lately in this Topic
frinholp
FM Forums Advertisers


FusionBB™ Version 3.0 FINAL | ©2003-2010 InteractivePHP, Inc.
Execution time: 0.273 seconds.   Total Queries: 50   Zlib Compression is on.
All times are . Current time is 01:34 PM
Content ©1996-2008 Ocean West Consulting, Inc. All Rights Reserved
Ocean West Consulting, Inc. can not and will not be held responsible for any of the contents in this site.
FM Forums™ is a trademark of Ocean West Consulting, Inc an independent entity, not affiliated with FileMaker Inc.
FileMaker® is a registered trademark of FileMaker Inc.
Top