(Sorry to post this here ‘and’ in the Developer channel…I’m not really quite sure where it belongs?)
I have built a custom schedule job to calculate how many day’s it’s been since an ‘activity’ has been made on this record.
I’ve created a custom field that gets updated whenever an activity is logged, which sets a datetime field in the Account record to the current datetime.
Then, I want this scheduled job to run overnight, to update another custom field (days_since_last_activity) to calculate the difference between ‘today’ and that date, and enter that number into the field.
My Code seem to run correctly, it does the calculation correctly, and it updates the field to the new value…
However…for some reason, the Account record seems to ‘lose’ it’s email address. (ie. the email field will be blank after the update)
After some more research, it’s really just losing the ‘relationship’ between the Account and the email field.
I’m guessing there is some way I’d need to save the related Bean as well???
Any guidance ???
(I’m kinda stuck)
Thanks!
My Code Below:
function accountActivityAgingUpdater()
{
//LoggerManager::getLogger();
//Get the cutoff date for which meetings will be considered
date_default_timezone_set("America/New_York");
$todaysDate = date('Y-m-d h:i:s');
$todaysDate = strtotime($todaysDate);
//LoggerManager::getLogger()->error('Account -- Todays date ' . $todaysDate);
//Get an instance of the Accounts bean for querying
//see the Working With Beans chapter.
$bean = BeanFactory::getBean('Accounts');
//Get the list of Accounts -
//this can likely be optimized to only find accounts with
//some value in the $account->last_activity_date_c field
$query = "accounts_cstm.ams_sid_c != ''";
$accounts = $bean->get_full_list('', $query);
foreach($accounts as $account)
{
//Get the date the case was created
$lastActivityDate = $account->last_activity_date_c;
//LoggerManager::getLogger()->error('Last Activity Date ' . $lastActivityDate);
$lastActivityDate = strtotime($lastActivityDate);
//Determine the date difference in days
//$timeDiff = abs($todaysDate - $lastActivityDate);
$timeDiff = abs($todaysDate - $lastActivityDate);
//LoggerManager::getLogger()->error('Time Diff ' . $timeDiff);
// 86400 seconds in one day
$numberDays = $timeDiff/86400;
$numberDays = intval($numberDays);
//LoggerManager::getLogger()->error('The Diff ' . $numberDays);
//this handles if the last activity date is empty
if (!empty($lastActivityDate)) {
$account->days_since_last_activity_c = $numberDays;
} else {
$account->days_since_last_activity_c = 999;
}
//Save the cases changes
$account->save();
}
//Signify we have successfully ran
return true;
}