Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save esimonetti/4d30f4b12fc64319f4047a21761e8e0e to your computer and use it in GitHub Desktop.
Save esimonetti/4d30f4b12fc64319f4047a21761e8e0e to your computer and use it in GitHub Desktop.
Save all Sugar Contacts, preserving the date modified and modified by. Enforce advanced workflows to trigger for each of the contacts
<?php
// Enrico Simonetti
// enricosimonetti.com
function usage($error = '') {
if(!empty($error)) print(PHP_EOL . 'Error: ' . $error . PHP_EOL);
print(' php ' . __FILE__ . ' --instance /full/path' . PHP_EOL);
exit(1);
}
// only allow CLI
$sapi_type = php_sapi_name();
if (substr($sapi_type, 0, 3) != 'cli') {
die(__FILE__ . ' is CLI only.');
}
// get command line params
$o = getopt('', array('instance:'));
if (!$o) usage();
// find folder
if(!empty($o['instance'])) {
print('Debug: Entering folder ' . $o['instance'] . PHP_EOL);
chdir($o['instance']);
} else {
chdir(dirname(__FILE__));
}
if(!file_exists('config.php') || !file_exists('sugar_version.php')) {
usage('The provided folder is not a Sugar system');
}
// sugar basic setup
define('sugarEntry', true);
require_once('include/entryPoint.php');
// added for awf logic
use Sugarcrm\Sugarcrm\ProcessManager\Registry;
if(empty($current_language)) {
$current_language = $sugar_config['default_language'];
}
$app_list_strings = return_app_list_strings_language($current_language);
$app_strings = return_application_language($current_language);
global $current_user;
$current_user = BeanFactory::getBean('Users');
$current_user->getSystemUser();
$start_time = microtime(true);
// get matching results with sugar query
$module = 'Contacts';
$mybean = BeanFactory::getBean($module);
$query = new SugarQuery();
$query->select(array('id'));
$query->from($mybean);
$query->orderBy('date_entered', 'ASC');
$preparedstatement = $query->compile();
print('Debug: Executing query: ' . $preparedstatement->getSQL() . print_r($preparedstatement->getParameters(), true). PHP_EOL);
$results = $query->execute();
if(empty($results)) {
print('Debug: No ' . $module . ' records found for the above query' . PHP_EOL);
} else {
// save every record
foreach($results as $result => $fields) {
// reset awf trigger so that it can trigger workflows for every record
Registry\Registry::getInstance()->drop('triggered_starts');
$record = BeanFactory::getBean($module, $fields['id']);
$record->update_date_modified = false;
$record->update_modified_by = false;
$record->save();
print('Debug: Saved ' . $module . ' record with id ' . $fields['id'] . PHP_EOL);
}
}
print('Processed in ' . (int)(microtime(true) - $start_time) . PHP_EOL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment