Last active
January 28, 2016 13:20
-
-
Save bickart/6da27cc1b9d9da539eac to your computer and use it in GitHub Desktop.
Place this file and your CSV file into the root of your SugarCRM install and you can update data in SugarCRM from a CSV on the command line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Use an unlimited amount of memory, we probably shouldn't do this but that is how I roll | |
ini_set('memory_limit', '-1'); | |
//Our program allows us to skip over rows in the data file and only load a sub-set of rows | |
if (count($argv) < 3 || ! is_numeric($argv[1]) || ! is_numeric($argv[2])) { | |
echo "Usage php -f $argv[0] skip max" . PHP_EOL; | |
echo " skip - number of rows to skip when processing" . PHP_EOL; | |
echo " max - number of rows to import when processing" . PHP_EOL; | |
die(); | |
} | |
$skip = $argv[1]; | |
$max = $argv[2]; | |
//BEGIN STANDARD ITEMS NEEDED TO RUN SUGARCRM FROM THE COMMAND LINE | |
if (!defined('sugarEntry')) { | |
define('sugarEntry', true); | |
} | |
chdir(dirname(__FILE__)); | |
define('ENTRY_POINT_TYPE', 'api'); | |
require_once('include/entryPoint.php'); | |
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); | |
//set the $current_user to be the System Administrator, if you want to run as another person use the $current_user->retrieve | |
//function to load the appropriate user | |
global $current_user; | |
$current_user = BeanFactory::getBean('Users'); | |
$current_user->getSystemUser(); | |
//END STANDARD ITEMS NEEDED TO RUN SUGARCRM FROM THE COMMAND LINE | |
echo PHP_EOL; | |
$column = null; | |
//open a file handle to a file called dbupdate.csv, you can change this to the appropiate choice | |
if (($handle = fopen("dbupdate.csv", "r")) !== false) { | |
$row = 0; | |
$processed = 0; | |
while (($data = fgetcsv($handle, 0, ",")) !== false) { | |
//row[0] should be the column headers, you'll need to know what is in each column to find data appropriately | |
if ($processed >= $max) | |
die(); | |
else { | |
if ($row >= $skip && $processed < $max) { | |
//$data contains a row of CSV data, use this information to update or find manipulate some data | |
$contact = BeanFactory::newBean('Contacts'); | |
$contactQuery = new SugarQuery(); | |
$contactQuery->from($contact); | |
$contactQuery->where()->equals('last_name', $data[2]); | |
foreach ($contact->fetchFromQuery($contactQuery) as $c) { | |
echo "$row | $c->name" . PHP_EOL; | |
//do something | |
$c->save(false); | |
unset($c); | |
} | |
unset($contactQuery); | |
unset($contact); | |
$processed++; | |
} | |
} | |
$row++; | |
} | |
fclose($handle); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment