Skip to content

Instantly share code, notes, and snippets.

@matthewpoer
Last active August 29, 2015 14:16
Show Gist options
  • Save matthewpoer/5663f5b4b8e9c44cb53d to your computer and use it in GitHub Desktop.
Save matthewpoer/5663f5b4b8e9c44cb53d to your computer and use it in GitHub Desktop.
SugarCRM Scheduler example shows a DO and DON'T way of updating records. The DO includes proper code annotation, using SugarQuery to pull records, and updating the found records without changing the record's modification metadata. These files should reside in custom/Extension/modules/Schedulers/Ext/ScheduledTasks/
<?php
array_push($job_strings, 'UpdateProductTemplatesExpiration');
function UpdateProductTemplatesExpiration(){
$product_bean = new ProductTemplate();
if(isset($product_bean->field_defs['end_date_c']) && isset($product_bean->field_defs['is_expired_c'])
&& $product_bean->field_defs['end_date_c']['source'] == 'custom_fields'
&& $product_bean->field_defs['is_expired_c']['source'] == 'custom_fields'){
$query = "update product_templates_cstm set is_expired_c = if(datediff(utc_timestamp(), end_date_c)>0, 'yes' , 'no') ";
$db = DBManagerFactory::getInstance();
$GLOBALS['log']->info("----->Scheduler is about to update expiration status on product catalog records by running the query $query");
$db->query($query);
}else{
$GLOBALS['log']->fatal("----->Scheduler can't update expiration status on product catalog records, possible reason: missing custom field end_date_c or is_expired_c");
}
return true;
}
<?php
$job_strings[] = 'tcx_ExpireProductTemplatesAtEndDate';
/**
* A nightly process to set an Expired boolean on Product Catalog entries
* that meet their specified end dates.
*
* @return bool
* @throws SugarQueryException
*/
function tcx_ExpireProductTemplatesAtEndDate()
{
/**
* We'll use an established TimeDate object in our query below
*/
global $timedate;
/**
* Use SugarQuery to find a list of ProductTemplate ("Product Catalog")
* records that are:
* 1. not currently expired, and
* 2. the end date is in the past
*
* Establish the SugarQuery object, tell it to select the Product Template
* 'id' field (we only need 'id' because we'll be establishing and working
* with the SugarBean object once we find our records), and then tell it to
* pull our data from the ProductTemplate class
*/
$query = new SugarQuery();
$query->select(array('id'));
$query->from(BeanFactory::getBean('ProductTemplates'));
/**
* Build the "where" piece of the query using the equals() method and
* lte() - note "lte" is "Less Than or Equal to"
* @see SugarQuery_Builder_Where::lte()
*/
$query->where()->lte('end_date', $timedate->nowDb()); // note "lte" is Less Than or Equal to
$query->where()->equals('is_expired', '0');
/**
* With our Query built, execute it, storing all results in a new array
*/
$results = $query->execute();
/**
* Begin processing results if there are any. If no results were found,
* $result would be empty, so no processing will occur.
*/
foreach ($results as $result) {
/**
* Use the ProductTemplate class to work with our record. Instantiate
* using the 'id' field we pulled in from our SugarQuery
*/
$ProductTemplate = BeanFactory::getBean('ProductTemplates', $result['id']);
$ProductTemplate->is_expired = '1';
/**
* Some optional settings here. It may be a requirement that we do not
* update the record's metadata, i.e. the Modified By
* (modified_user_id) or Date Modified (date_modified). This is often
* desirable in this type of background process. If they are not set,
* then the date_modified would take a "now" timestamp and
* modified_user_id would take a value of "1" the admin/system user)
*/
$ProductTemplate->update_modified_by = false;
$ProductTemplate->update_date_modified = false;
/**
* Save our Product Template entry. Using the SugarBean object in this
* way would trigger any additional workflows, logic or notifications
*/
$ProductTemplate->save();
}
/**
* All Scheduled Jobs must return TRUE to indicate successful execution
*/
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment