How to generate unique Sugar id in the database?

Suppose if you have created/uploaded a custom module in your SugarCRM instance and want that custom module to work as the other modules in the system. You want the custom module to have the ACL/Roles actions accordingly as the other modules are having. At the same time you want to update the database with the unique SugarCRM id in the database. Now the big question is “How to do that man?”

Let’s create a php file which will do the same and name it createID.php. This file will create random SugarCRM unique id of 36 characters.

/*In this portion of code we give the login credentials of SugarCRM database which is to be updated.*/

global $host, $username, $password, $database;

$host = "localhost";
$username = "sugar";
$password = "sugar";
$database = "sugarcrm";

/*This portion of the code will create a MySQL connection that will connect to the database.*/

$server = mysql_connect($host, $username, $password) or
die(mysql_error());

mysql_select_db($database);

/*This portion of the code will crteate the 36 character unique SugarCRM id.*/

function create_guid()
{
$microTime = microtime();
list($a_dec, $a_sec) = explode(" ", $microTime);

$dec_hex = sprintf("%x", $a_dec* 1000000);
$sec_hex = sprintf("%x", $a_sec);

ensure_length($dec_hex, 5);
ensure_length($sec_hex, 6);

$guid = "";
$guid .= $dec_hex;
$guid .= create_guid_section(3);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= $sec_hex;
$guid .= create_guid_section(6);

return $guid;

}

function create_guid_section($characters)
{
$return = "";
for($i=0; $i<$characters; $i++)
{
$return .= sprintf("%x", mt_rand(0,15));
}
return $return;
}
function ensure_length(&$string, $length)
{
$strlen = strlen($string);
if($strlen < $length)
{
$string = str_pad($string,$length,"0");
}
else if($strlen > $length)
{
$string = substr($string, 0, $length);
}
}
/*This portion of code will select the all the records of the table mentioned here.*/
$query = "select * from custom_module";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
if($num_rows > 0)
{
while(($row=mysql_fetch_assoc($result)) != null)

/*This portion of the code will check if the condition is true(meaning data is there in the table mentioned above) then it will create the unique id and update the existing records.*/

{
$opp_proid = create_guid();
$id = $row['id'];
$query2 = “update opportunities_products set opp_proid=’$opp_proid’ where id =’$id’”;
echo $query2;
mysql_query($query2);
}
echo “completed”;
}

Wow!! The table is updated with the new unique id.

Leave a Reply


OpenApp Media Network OpenApp Media Network

Various trademarks held by their respective owners.

Privacy Statement | Terms Of Service