PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV 2024 → HDescribeTrigger
HDescribeTrigger
Débuté par Joris, 24 sep. 2008 01:53 - 4 réponses
Posté le 24 septembre 2008 - 01:53
Hi,
I'm having this situation :
Our customer wants to know for every file who has created a record an when, and who has made the last modification to a record and when. So I added these fields to every file in the project : USRCREATION, DATECREATION,USRMOD,DATEMOD
I have decided to create a trigger to apply these values to the file. Therefore i have created 2 triggers like this :
HDescribeTrigger("*","HMODIFY","TRIG_Modify",hTriggerBefore)
HDescribeTrigger("*","HADD","TRIG_Add",hTriggerBefore)
So i don't have to worry about these fields anymore, they must be automatically filled by these triggers in the procedures TRIG_Modify and TRIG_Add
This is what i tried in the TRIG_modify procedure :
PROCEDURE TRIG_Modify()
usrcreation = gpwGetUserInfo(gpwInfoLogin)
datecreation = Today
Now here is my problem :
How do i know the name of the current file? I can not apply a value to a field without knowing the file it belongs to. And i don't want to create a seperate trigger and procedure for each of my files.
Any ideas? Maybe with indirection? Thanks for helping.
Joris
Posté le 24 septembre 2008 - 01:53
just look in the help for triggers.
you have several predefined variables:
H.FileName
Character string: Logical name of the file for which the trigger is activated.

H.Action
Character initialized to "A" for a trigger Before, and "P" for a trigger After.

H.TriggerFunction
Character string: Name of the Hyper File function that started the trigger.

H.ToDo
During the execution of a before trigger, you can cancel the execution of the current Hyper File function by assigning "A" to the Hyper File state variable: h.todo = "A"
In your case use the H.FileName
PS: If you use sql INSERT/UPDATE the triggers don't work, they only work with HADD/HMODIFY. If you use sql (SQLSERVER, ORACLE) the triggers must be managed in the DB and not in the windev code.
Posté le 24 septembre 2008 - 01:53
Hi Joris
Have you considered using the build in "LogMethod". (Various Tab in the File Describtion window - Analysis) You don't have to write code and what you need is probably there.
Regards
Marius
Posté le 24 septembre 2008 - 01:55
OK, thanks a lot for the info. :cheers:
I should have known about the H.Filename command, but i've never used it before.
(The logging system is not an option, the user name and date should be visible in my application.)
Joris.
Posté le 24 septembre 2008 - 01:55
Easy... Here's an example:
Procedure TRG_SetAuditTrail()
//Start trigger logic
SWITCH Upper(H.Action)
CASE "A" // Trigger before
//Check the called function
SWITCH Upper(H.TriggerFunction)
CASE "HADD", "HAJOUTE"
{H.FileName+".CreatedBy ",indItem}=objSession::str_SessionUserName
{H.FileName+".CreatedOn",indItem}=DateSys()+TimeSys()
{H.FileName+".LastUpdatedBy",indItem}=objSession::str_SessionUserName
{H.FileName+".LastUpdatedOn",indItem}=DateSys()+TimeSys()
CASE "HMODIFY", "HMODIFIE"
{H.FileName+".LastUpdatedBy",indItem}=objSession::str_SessionUserName
{H.FileName+".LastUpdatedOn",indItem}=DateSys()+TimeSys()
CASE "HCROSS", "HRAYE"
{H.FileName+".LastUpdatedBy",indItem}=objSession::str_SessionUserName
{H.FileName+".LastUpdatedOn",indItem}=DateSys()+TimeSys()
END

CASE "P" // Trigger After
END