Hi to all. I’m doing a scheduled task for the import of accounts. Actually i have a problem with the import. My idea is:
if date_modified of the csv is > of date_modified of the db i update the data, if not i skip the update. My problem is “find the account”. In the csv i have an ID and i want to use that id to find the account. But i can’t force the instert of the id in the table. There is a work around to force this insert? Thank you.
you could create an entryPoint that you can access in the scheduler.
e.g. index.php?entryPoint=account_import
Making it point to a file where you handle the import.
For example:
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
global $log;
$data = file('upload/import.csv'); //parse the rows
foreach ($data as $row) {
$account = new Account();
$row = str_getcsv($row, ";"); //parse the items in rows
//replace index with the index of the id in csv
//if an account is found it'll update the account
$id = $account->retrieve($row[0]) ? $account->id : '';
//replace index with the index of the date modified in csv
if (!empty($id) && $account->date_modified > $row[1]) {
//do something with account fields
$values = array(
'id' => $id,
'name' => $row[2]
//and so on
);
$account->populateFromRow($values);
$account->save();
} else {
$log->error('account not modified');
}
}
Thankyou for the answer, but actually i have to use the API to interact between the csv and the db, because there are a lot of custom field ad i don’t want to query 3 or 4 table for a insert. I’ve tried to create a custom field called id_csv_c and use that for index. But when i try to use get_entry_list on Accounts_cstm an error occured: {“name”:“Module Does Not Exist”,“number”:20,“description”:“This module is not available on this server”} and i don’t know why.
You don’t need to use get_entry_list on Accounts_cstm since get_entry_list returns the Account object which contains custom fields as well. I’m not using the API that often and can’t confirm if it works but this is how I’d do:
$get_entry_list_parameters = array(
//session id
'session' => $session_id,
//The name of the module from which to retrieve records
'module_name' => 'Accounts',
//The SQL WHERE clause without the word "where".
'query' => "id_csv_c = '{$id_from_csv}' AND date_modified < '{$date_modfied_csv}'",
//The SQL ORDER BY clause without the phrase "order by".
'order_by' => "",
//The record offset from which to start.
'offset' => 0,
//A list of fields to include in the results.
'select_fields' => array(
'id',
'name',
'title',
),
//A list of link names and the fields to be returned for each link name.
'link_name_to_fields_array' => array(
array(
'name' => 'email_addresses',
'value' => array(
'email_address',
'opt_out',
'primary_address'
),
),
),
//The maximum number of results to return.
'max_results' => 2,
//If deleted records should be included in results.
'deleted' => 0,
//If only records marked as favorites should be returned.
'favorites' => false,
);
thanks, i will try it