AnsweredAssumed Answered

How to Create a Unique ID Number in a Logic Hook?

Question asked by Erica Barnetts on Nov 11, 2014
Latest reply on Dec 3, 2015 by Anand Chitikela
I searched high and low for an answer to this and managed to figure it out and wanted to share. This is a way to make a Logic Hook that will  stamp a unique ID number onto any record. The number is based upon the date and time, down to the second. So, it assure the records will always be unique (unless two records are submitted in the exact same second, which is unlikely).
In this example, I am creating a unique ID for a custom module called Entities. The first step is to make sure you have a text field in Entities called "id_number_c" with a default setting of "3". This can be any number. The reason I set it to a value is so the ID will only be written once. Without that sort of logic, the ID will be rewritten everytime the record is edited.

Like all Logic Hooks, you need two files. One goes here:
\custom\modules\[MODULE NAME]\logic_hooks.php

That file looks like this:


<?php
// Do not store anything in this file that is not part of the array or the hook version.  This file will
// be automatically rebuilt in the future. 
$hook_version = 1; 
$hook_array = Array(); 
// position, file, function 
$hook_array['before_save'] = Array(); 
$hook_array['before_save'][] = Array(1, 'workflow', 'include/workflow/WorkFlowHandler.php','WorkFlowHandler', 'WorkFlowHandler'); 


// This custom Logic Hook creates a unique ID number for the record based upon the date and time and seconds.
$hook_array['before_save'][] = Array(
//Processing index. For sorting the array.
1, 

//Label. A string value to identify the hook. This can be any name as a variable and it isn't referenced anywhere else.
'create_unique_id', 

//The PHP file where your CLASS is located. This is just pointing to where the logic is going to happen.
'custom/modules/entit_Entities/logichooks/unique_id.php',

//The CLASS the FUNCTION is in. This name will be used in the PHP file referenced above as CLASS.
'assignUniqueIDNumber', 

//The FUNCTION to call. This name will be used in the PHP file referenced above as FUNCTION.
'createTheNumber'

);

?>


The other file goes here:
\custom\modules\[MODULE NAME]\logichooks\unique_id.php

That file looks like this:

<?php

// This CLASS name must match the variable set up in the logic hooks file at C:\...\custom\modules\entit_Entities\logic_hooks.php
 class assignUniqueIDNumber {
 
// This FUNCTION name must match the variable set up in the logic hooks file at C:\...\custom\modules\entit_Entities\logic_hooks.php
function createTheNumber($bean, $event, $arguments) 

{
// START THE ACTUAL CODE...


// This "if" statement makes sure that the record only runs when a new record is first created. Without the "if" statement, tbe ID will be regenerated everytime the record is edited.
  if ($bean->id_number_c == '3')
        {
  
   // Do not edit these three lines of code. These lines actually create the ID number based upon year-month-date-militaryhour-minute-second. At the end, the number is placed in a variable called "$uniqueID"
   $date = new DateTime(); 
   $date->setTimezone(new DateTimeZone('America/Los_Angeles')); 
   $uniqueID = $date->format('Ymd-Hi-s');
  
   //This next line actually copies the ID generated above into the ID field of the module. Make sure this field name matches whatever is in the database.
   $bean->id_number_c = $uniqueID;
  }
 
 


// END THE ACTUAL CODE...
}
 }
 ?>

This will produce an ID number in the following format:
20141012-2202-15

That would be 2014 in October on the 12th at 22 hours PST and 02 minutes and 15 seconds after the hour.

Formatting the number in this fashion, from largest measurement of time to the smallest, ensures:
a.) The number will always get larger with time.
b.) The number will never get repeated (unless multiple people create records at the exact same second, which is obviously unlikely).
c.) The number is generated off the server time, not the client browser. That means even if users are in different timezones, the sequencing of the number will always remain consistent. There will never be issues with users in different timezones accidentally getting lower or higher numbers.
d.) It will make it easy to sort the date and time records were created, since the identification number is the date and time.

Hope that helps people trying to create a unique ID for records in SugarCRM using a Logic Hook.

Outcomes