how to build custom triggers

  • KM00960959R
  • 28-May-2014
  • 29-May-2014

This document is under revision.

Summary

trigger type, sample custom triggers

Question

Service manager provides triggers and it could be used effectively if well-tailored.

This dosoment shows various sample custom triggers and SM developer could use it as reference when building custom triggers.

 

Answer

1. trigger type
 . 1 - Before Add - When adding a new record to this file, launch the application prior to committing the addition.
 . 2 - After Add - When adding a new record to this file, launch the application after committing the addition.
 . 3 - Before Update - When modifying a record in this file, launch the application prior to committing the change.
 . 4 - After Update - When modifying a record in this file, launch the application after committing the change.
 . 5 - Before Delete - When deleting a record in this file, launch the application prior to performing the deletion.
 . 6 - After Delete - When deleting a record in this file, launch the application after performing the deletion

2. naming custom triggers
 - trigger runs by name order.
   It is better to have name starting by 'z_xxxx' if wish to run lastly.
   It is better to have name starting by 'A_xxxx' if wish to run firstly.
   This is because 'A' is first character and 'z' is last character in ASCII code.

3. file variables for each trigger type in Java Script
 - add, delete trigger : record
 - update trigger    : record, oldrecord
   . record     // updated information
   . oldrecord  // previous information before update

4. Basic Java Script
 - all columns' value of current record
  record
 - one of columns' value in current record
  record[brief_description]     // recommend, always working
  record.brief_description       // record.class makes issue in testing because class is real column but also reserved word in Java script
 - other variables
  vars.$lo_user_name
 
 
5. Sample triggers

A. monitor file's activity
 1) ex: monitor UPDATE in probsummary file
 2) add custom trigger in triggers file
  - Trigger Name: probsummary_before_update   // anyname
  - Table Name: probsummary
  - Trigger Type: 3 - Before Update
  - Application :
  - Script :
  print("### trigger execution: probusmmary_before_update (messagebox)");
  vars.$L_void=system.functions.rtecall("log",vars.$L_rc,
         "### trigger execution: probusmmary_before_update (sm.log)", "NULL");
  > click 'save' then click 'compile'
 3) search any IM ticket and then update it
  - in message box,
    ### trigger execution: probusmmary_before_update (messagebox)    
  - in sm.log
    3904(  3724) 10/01/2013 10:58:42  RAD I ### trigger execution: probusmmary_before_update (sm.log)
  --> this message is shown even though no debugging options in sm.ini
 
B. mointor file's value by trigger
  - add logics into trigger, probsummary_before_update
  print("### updated record:" + record );
  vars.$L_void=system.functions.rtecall("log",vars.$L_rc,
          "### updated record:" + record , "NULL");
  - in message box,
   ### updated record:[C++ object Datum] - probsummary={["IM10002", "incident", '09/02/07 01:51:00', "Jurr.Fleijs", "2", "3", '09/30/13 20:41:00', "Network", , , , "updat
   ed", , , , , , , , , , , , , , , , , , "adv-nam-server-mail", , , , , , , , {"Webmail can't be reached, browser can’t find it"}, {}, {}, {}, {}, , , , , , , , 1
   , true, , '09/02/07 01:44:00', "Incident.Analyst", , , , , , {"08/04/08 12:54:14 US/Mountain (falcon):", "test", "07/23/08 17:44:03 US/Mountain (prietke):", "te
   st", "07/23/08 16:24:22 US/Mountain (prietke):", "test", "07/23/08 14:45:54 US/Mountain (prietke):", "test", "07/23/08 13:04:15 US/Mountain (prietke):", "test",
    "04/23/08 23:24:17 US/Mountain (falcon):", "yo yo"}, "falcon", "IM.update.incident", 0, , , , , , , , , , , , , , , {}, {}, , , , , "advantage", , , "Webmail l
   ogin failure", , , , , , , , , "Jurr.Fleijs", "SD10002", , , '09/30/13 20:41:00', "falcon", "Work In Progress", {}, , "Investigation", , "failure", , , false, 1
   68, , , , , , , 23, "falcon", , , , , , , , , , , false, , , , false, "function or feature not working", "incident", , false, , , , , false, , , {}, {}, {}, {},
    , , , , , , , , , , , {}, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , "advantage/North America", '09/30/13 20:35:28', , , , ,
    "dept", , , {}, , , , , , , , , , {{[, , , ]}}, {{[, , , , ]}}, true, , {}, , 10, , , , , , , , , true, , false, false, "2", , , {}, {168}, , , , "E-mail / Web
   mail (North America)", , , {}, , , , , , , , {}, ]}    
  - in sm.log
    3904(  3724) 10/01/2013 11:41:00  RAD I ### updated record:[C++ object Datum] - probsummary={["IM10002", "incident", '09/02/07 01:51:00', "Jurr.Fleijs", "2", "3", '09/30/13 20:41:00', "Network", , , , "updated", , , , , , , , , , , , , , , , , , "adv-nam-server-mail", , , , , , , , {"Webmail can't be reached, browser can?셳 find it"}, {}, {}, {}, {}, , , , , , , , 1, true, , '09/02/07 01:44:00', "Incident.Analyst", , , , , , {"08/04/08 12:54:14 US/Mountain (falcon):", "test", "07/23/08 17:44:03 US/Mountain (prietke):", "test", "07/23/08 16:24:22 US/Mountain (prietke):", "test", "07/23/08 14:45:54 US/Mountain (prietke):", "test", "07/23/08 13:04:15 US/Mountain (prietke):", "test", "04/23/08 23:24:17 US/Mountain (falcon):", "yo yo"}, "falcon", "IM.update.incident", 0, , , , , , , , , , , , , , , {}, {}, , , , , "advantage", , , "Webmail login failure", , , , , , , , , "Jurr.Fleijs", "SD10002", , , '09/30/13 20:41:00', "falcon", "Work In Progress", {}, , "Investigation", , "failure", , , false, 168, , , , , , , 23, "falcon", , , , , , , , , , , false, , , , false, "function or feature not working", "incident", , false, , , , , false, , , {}, {}, {}, {}, , , , , , , , , , , , {}, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , "advantage/North America", '09/30/13 20:35:28', , , , , "dept", , , {}, , , , , , , , , , {{[, , , ]}}, {{[, , , , ]}}, true, , {}, , 10, , , , , , , , , true, , false, false, "2", , , {}, {168}, , , , "E-mail / Webmail (North America)", , , {}, , , , , , , , {}, ]}

C. monitor file's value by trigger ( before/after update )
   - add logics into trigger, probsummary_before_update
     print("### old record:" + oldrecord);
     vars.$L_void=system.functions.rtecall("log",vars.$L_rc,
           "### old record:" + oldrecord, "NULL");

     print("### new record:" + record);
     vars.$L_void=system.functions.rtecall("log",vars.$L_rc,
           "### new record:" + record, "NULL");

  - in message box,
     ### new record:[C++ object Datum] - probsummary={["IM10002", "incident", '09/02/07 01:51:00', "Jurr.Fleijs", "2", "3", '10/01/13 01:30:51', "Network", , , , "up
     dated", , , , , , , , , , , , , , , , , , "adv-nam-server-mail", , , , , , , , {"Webmail can't be reached, browser can’t find it"}, {}, {}, {}, {}, , , , , , ,
     , 1, true, , '09/02/07 01:44:00', "Incident.Analyst", , , , , , {"08/04/08 12:54:14 US/Mountain (falcon):", "test", "07/23/08 17:44:03 US/Mountain (prietke):",
     "test", "07/23/08 16:24:22 US/Mountain (prietke):", "test", "07/23/08 14:45:54 US/Mountain (prietke):", "test", "07/23/08 13:04:15 US/Mountain (prietke):", "tes
     t", "04/23/08 23:24:17 US/Mountain (falcon):", "yo yo"}, "falcon", "IM.update.incident", 0, , , , , , , , , , , , , , , {}, {}, , , , , "advantage", , , "new",
     , , , , , , , , "Jurr.Fleijs", "SD10002", , , '10/01/13 01:30:51', "falcon", "Work In Progress", {}, , "Investigation", , "failure", , , false, 168, , , , , , ,
      29, "falcon", , , , , , , , , , , false, , , , false, "function or feature not working", "incident", , false, , , , , false, , , {}, {}, {}, {}, , , , , , , ,
     , , , , {}, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , "advantage/North America", '10/01/13 01:30:35', , , , , "dept", , , {}
     , , , , , , , , , , {{[, , , ]}}, {{[, , , , ]}}, true, , {}, , 16, , , , , , , , , true, , false, false, "2", , , {}, {168}, , , , "E-mail / Webmail (North Ame
     rica)", , , {}, , , , , , , , {}, ]}
     ### old record:[C++ object Datum] - probsummary={["IM10002", "incident", '09/02/07 01:51:00', "Jurr.Fleijs", "2", "3", '10/01/13 01:30:35', "Network", , , , "up
     dated", , , , , , , , , , , , , , , , , , "adv-nam-server-mail", , , , , , , , {"Webmail can't be reached, browser can’t find it"}, {}, {}, {}, {}, , , , , , ,
     , 1, true, , '09/02/07 01:44:00', "Incident.Analyst", , , , , , {"08/04/08 12:54:14 US/Mountain (falcon):", "test", "07/23/08 17:44:03 US/Mountain (prietke):",
     "test", "07/23/08 16:24:22 US/Mountain (prietke):", "test", "07/23/08 14:45:54 US/Mountain (prietke):", "test", "07/23/08 13:04:15 US/Mountain (prietke):", "tes
     t", "04/23/08 23:24:17 US/Mountain (falcon):", "yo yo"}, "falcon", "IM.update.incident", 0, , , , , , , , , , , , , , , {}, {}, , , , , "advantage", , , "old",
     , , , , , , , , "Jurr.Fleijs", "SD10002", , , '10/01/13 01:30:35', "falcon", "Work In Progress", {}, , "Investigation", , "failure", , , false, 168, , , , , , ,
      28, "falcon", , , , , , , , , , , false, , , , false, "function or feature not working", "incident", , false, , , , , false, , , {}, {}, {}, {}, , , , , , , ,
     , , , , {}, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , "advantage/North America", '09/30/13 21:04:37', , , , , "dept", , , {}
     , , , , , , , , , , {{[, , , ]}}, {{[, , , , ]}}, true, , {}, , 15, , , , , , , , , true, , false, false, "2", , , {}, {168}, , , , "E-mail / Webmail (North Ame
     rica)", , , {}, , , , , , , , {}, ]}   
   - in sm.log
     3904(   496) 10/01/2013 16:30:51  RAD I ### old record:[C++ object Datum] - probsummary={["IM10002", "incident", '09/02/07 01:51:00', "Jurr.Fleijs", "2", "3", '10/01/13 01:30:35', "Network", , , , "updated", , , , , , , , , , , , , , , , , , "adv-nam-server-mail", , , , , , , , {"Webmail can't be reached, browser can?셳 find it"}, {}, {}, {}, {}, , , , , , , , 1, true, , '09/02/07 01:44:00', "Incident.Analyst", , , , , , {"08/04/08 12:54:14 US/Mountain (falcon):", "test", "07/23/08 17:44:03 US/Mountain (prietke):", "test", "07/23/08 16:24:22 US/Mountain (prietke):", "test", "07/23/08 14:45:54 US/Mountain (prietke):", "test", "07/23/08 13:04:15 US/Mountain (prietke):", "test", "04/23/08 23:24:17 US/Mountain (falcon):", "yo yo"}, "falcon", "IM.update.incident", 0, , , , , , , , , , , , , , , {}, {}, , , , , "advantage", , , "old", , , , , , , , , "Jurr.Fleijs", "SD10002", , , '10/01/13 01:30:35', "falcon", "Work In Progress", {}, , "Investigation", , "failure", , , false, 168, , , , , , , 28, "falcon", , , , , , , , , , , false, , , , false, "function or feature not working", "incident", , false, , , , , false, , , {}, {}, {}, {}, , , , , , , , , , , , {}, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , "advantage/North America", '09/30/13 21:04:37', , , , , "dept", , , {}, , , , , , , , , , {{[, , , ]}}, {{[, , , , ]}}, true, , {}, , 15, , , , , , , , , true, , false, false, "2", , , {}, {168}, , , , "E-mail / Webmail (North America)", , , {}, , , , , , , , {}, ]}
     3904(   496) 10/01/2013 16:30:52  RAD I ### new record:[C++ object Datum] - probsummary={["IM10002", "incident", '09/02/07 01:51:00', "Jurr.Fleijs", "2", "3", '10/01/13 01:30:35', "Network", , , , "updated", , , , , , , , , , , , , , , , , , "adv-nam-server-mail", , , , , , , , {"Webmail can't be reached, browser can?셳 find it"}, {}, {}, {}, {}, , , , , , , , 1, true, , '09/02/07 01:44:00', "Incident.Analyst", , , , , , {"08/04/08 12:54:14 US/Mountain (falcon):", "test", "07/23/08 17:44:03 US/Mountain (prietke):", "test", "07/23/08 16:24:22 US/Mountain (prietke):", "test", "07/23/08 14:45:54 US/Mountain (prietke):", "test", "07/23/08 13:04:15 US/Mountain (prietke):", "test", "04/23/08 23:24:17 US/Mountain (falcon):", "yo yo"}, "falcon", "IM.update.incident", 0, , , , , , , , , , , , , , , {}, {}, , , , , "advantage", , , "old", , , , , , , , , "Jurr.Fleijs", "SD10002", , , '10/01/13 01:30:35', "falcon", "Work In Progress", {}, , "Investigation", , "failure", , , false, 168, , , , , , , 28, "falcon", , , , , , , , , , , false, , , , false, "function or feature not working", "incident", , false, , , , , false, , , {}, {}, {}, {}, , , , , , , , , , , , {}, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , "advantage/North America", '09/30/13 21:04:37', , , , , "dept", , , {}, , , , , , , , , , {{[, , , ]}}, {{[, , , , ]}}, true, , {}, , 15, , , , , , , , , true, , false, false, "2", , , {}, {168}, , , , "E-mail / Webmail (North America)", , , {}, , , , , , , , {}, ]}

D. replace value when any condition
   - add logics into trigger, probsummary_before_update
     print("### brief.description(old):" +  oldrecord.brief_description );
     print("### brief.description(new):" +  record.brief_description );

     // below code is also valid in "4- After Update" trigger type
     if( record.brief_description=="new" ) record.brief_description="new -> detect it";

   - when saving as brief.description="test" in the form then, it is saved as brief.description="test" in the sm system(db)
   - when saving as brief.description="new" in the form then,
      it is saved as brief_description="new -> detect it" in the sm system(db) because of trigger, probsummary_before_update

E. monitor deleting
 - tracking deletion of Todo file
 - custom trigger
  . Trigger Name : Todo_After_Delete
  . Table Name   : Todo
  . Trigger Type : 6 - After Delete
  . Applicatin   :
  . Script       :
    vars.$L_void=system.functions.rtecall("log",vars.$L_rc,
    "### HP Support Todo delete: ID(" + record.record_id + ") username("+ vars.$lo_user_name + ")", "NULL");
    vars.$L_void=system.functions.rtecall("log",vars.$L_rc,
    "### HP Support Todo delete(record):" + record, "NULL");
 - sm.log when deleting a record of Todo file
  3904(  3296) 10/04/2013 11:29:18  RAD I ### HP Support Todo delete: ID(C10001) username(falcon)
  3904(  3296) 10/04/2013 11:29:18  RAD I ### HP Support Todo delete(record):[C++ object Datum] - Todo={["C10001", "cm3r", "closed", "Printer memory upgrade", "Office Supplies (North America)", , , "3", "3", '10/08/13 00:00:00', , ]}

F. monitor updating
 - tracking updates of TodoMap file
 - custom trigger
  . Trigger Name : TodoMap_After_Update
  . Table Name   : TodoMap
  . Trigger Type : 4 - After Update
  . Applicatin   :
  . Script       :
     vars.$L_void=system.functions.rtecall("log",vars.$L_rc,
          "### HP Support TodoMap update       : Table Name(" + record.name + ") username("+ vars.$lo_user_name + ")", "NULL");
     vars.$L_void=system.functions.rtecall("log",vars.$L_rc,
           "### HP Support TodoMap update (old):" + oldrecord, "NULL");
     vars.$L_void=system.functions.rtecall("log",vars.$L_rc,
           "### HP Support TodoMap update (new):" + record, "NULL");
 - sm.log when deleting a record of Todo file
  3904(  4144) 10/04/2013 11:23:48  RAD I ### HP Support TodoMap update       : Table Name(incidents) username(falcon)
  3904(  4144) 10/04/2013 11:23:48  RAD I ### HP Support TodoMap update (old):[C++ object Datum] - TodoMap={["incidents", '10/03/13 20:14:02', 2, "falcon", open in $L.file="Closed", {"incident.id", "open", "title", "needed.by.time", "folder", "severity", "priority.code", "assignment,1", "owner.name"}, {"record.id", "status", "description", "target.date", "folder", "urgency", "priority", "group", "assignee"}]}
  3904(  4144) 10/04/2013 11:23:48  RAD I ### HP Support TodoMap update (new):[C++ object Datum] - TodoMap={["incidents", '10/03/13 20:23:48', 3, "falcon", open in $L.file="Closed" and true, {"incident.id", "open", "title", "needed.by.time", "folder", "severity", "priority.code", "assignment,1", "owner.name"}, {"record.id", "status", "description", "target.date", "folder", "urgency", "priority", "group", "assignee"}]}

G. launching multiple linker processes by trigger
 -
KM00911374 , how to launch multiple background process