HOWTO: Set date fields in a logic hook

sugarcrmdevelopers —  February 22, 2012 — 5 Comments

With the new TimeDate changes that came in Sugar 6.2, there’s been a bit of confusion on how to best set date values when using logic hooks. I saw this question recently on our internal mailing list…

What’s the best way to handle setting the due date on a task before it gets saved in a logic hook?

This is what i’m going to do so in the logic hook

$timedate = TimeDate::getInstance();  $end_date = new SugarDateTime('+4 days');  $task->date_due = $timedate->asUser($end_date, $GLOBALS['current_user']); 

Our Application Architect Stas Malyshev jumped right on this, providing this code solution:

You should use DB time format, as before_save is called after conversion hooks. I would advise doing this:

$task->date_due = TimeDate::getInstance()->getNow(true)->modify("+4 days")->asDb();

5 responses to HOWTO: Set date fields in a logic hook


    This does not work for me:  Any Idea what could be wrong here. The value in the DB shows as NULL after the execution of the code.

    Thanks & regards,
    if ($db->getRowCount($Result) == 0) { //INSERT $timedate = TimeDate::getInstance();  $SugarDt = new SugarDateTime(‘+4 days’);   $NewInventory = new TRA_Inventory(); $NewInventory->tra_inv_resort = $i; $NewInventory->tra_inv_date = TimeDate::getInstance()->getNow(true)->modify(“+4 days”)->asDb(); $NewInventory->tra_inv_tour_type = 2; $NewInventory->tra_inv_tour_time = $j; $NewInventory->tra_inv_available_slot = $vAvailSlot; $NewInventory->tra_inv_bookable_slot = $vBookSlot; $NewInventory->tra_inv_booked_slots = 0;

Trackbacks and Pingbacks:

  1. Invalid data somehow being retrieved.. not sure what's happening - May 21, 2012

    […] […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s