sript with sugarEntry

Hi to all, i have a strange problem. i’ve created a script to export contacts from suite crm:


if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
$destination_dir = "C:\\FTP\\DelBarba\\";

$oContact = BeanFactory::newBean('Contacts');

$oContacts = $oContact->get_full_list( );
$csv = '';
$first_row="Nome;Cognome;ID;Titolo;Funzione;Divisione;Nome azienda;Email;Non Primary E-mails;Cellulare;Telefono Ufficio;Telefono Abitazione;Altro Telefono;Fax;Indirizzo Primario, Via;Indirizzo Primario, Comune;Indirizzo Primario, Provincia;Indirizzo Primario, CAP;Indirizzo Primario, Nazione;Indirizzo Alternativo, Via;Indirizzo Alternativo, Comune;Indirizzo Alternativo, Provincia;Indirizzo Alternativo, CAP;Indirizzo Alternativo, Nazione;Descrizione;Data di nascita;Fonte del lead;ID Campagna;Non chiamare;Riferito a ID;Assistente;Telefono Assistente;Nome Utente Assegnato;Assegnato a Utente ID;Date Created;Data Modifica;Modificato da ID;Creato da ID;Deleted;Photo;ID Account Joomla;Account Disattivato;Tipo Utente Portale;Indirizzi;Geocode Stato;Latitudine;Longitudine;CardCode;".chr(10);
    $csv .=$first_row;

foreach ($oContacts as $oCon) {
    $csv .= $oCon->first_name .';';
    $csv .= $oCon->last_name .';';
    $csv .= $oCon->id_csv_c .';';
    $csv .= $oCon->salutation .';';
    $csv .= $oCon->funzione_c .';';
    $csv .= $oCon->department .';';
    $csv .= $oCon->nomeazienda_c .';';
    $csv .= $oCon->email1 .';';
    $csv .= $oCon->non_primary_email_c .';';
    $csv .= $oCon->phone_mobile .';';
    $csv .= $oCon->phone_work .';';
    $csv .= $oCon->phone_home .';';
    $csv .= $oCon->phone_other .';';
    $csv .= $oCon->phone_fax .';';
    $csv .= $oCon->primary_address_street .';';
    $csv .= $oCon->primary_address_city .';';
    $csv .= $oCon->primary_address_state .';';
    $csv .= $oCon->primary_address_postalcode .';';
    $csv .= $oCon->primary_address_country .';';
    $csv .= $oCon->alt_address_street .';';
    $csv .= $oCon->alt_address_city .';';
    $csv .= $oCon->alt_address_state .';';
    $csv .= $oCon->alt_address_postalcode .';';
    $csv .= $oCon->alt_address_country .';';
    $csv .= $oCon->description .';';
    $csv .= $oCon->birthdate .';';
    $csv .= $oCon->lead_source .';';
    $csv .= $oCon->campaign_id .';';
    $csv .= $oCon->do_not_call .';';
    $csv .= $oCon->riferito_a_id_c .';';
    $csv .= $oCon->assistant .';';
    $csv .= $oCon->assistant_phone .';';
    $csv .= $oCon->nome_utente_assegnato_c .';';
    $csv .= $oCon->assigned_user_id .';';
    $csv .= $oCon->date_entered .';';
    $csv .= $oCon->date_modified .';';
    $csv .= $oCon->modified_user_id .';';
    $csv .= $oCon->created_by .';';
    $csv .= $oCon->deleted .';';
    $csv .= $oCon->photo .';';
    $csv .= $oCon->joomla_account_id .';';
    $csv .= $oCon->portal_account_disabled .';';
    $csv .= $oCon->portal_user_type .';';
    $csv .= $oCon->jjwg_maps_address_c .';';
    $csv .= $oCon->jjwg_maps_geocode_status_c .';';
    $csv .= $oCon->jjwg_maps_lat_c .';';
    $csv .= $oCon->jjwg_maps_lng_c .';';
    $csv .= $oCon->car_code_c .';'.chr(10);


$a = str_replace("&#039;","'",($csv));

$csv =html_entity_decode($a);

file_put_contents($destination_dir."dataOut\\Contatti.csv", $csv);

echo "fatto";

I created my entrypoint and when i run from web the scrit, all works fine and smooty. But now i want to schedule this export, the server is Widows so i’ve the task skedule manager, and when i configured the script and tryed to launch, it give me a error. SO i’ve tryed to run from CMD and here the error:

So, why from browser it’s a valid entry point, but if i use the script doesn’t work at all?

I’m stuck in this like 3 hours, please help!!

Its because of this line of code.

if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

When you access the entryPoint via the browser it doesn’t just go straight to that script, it initiates the application and thus has access to the sugarEntry and passes that condition.

However if you set up a windows scheduled task to call that script directly then it will return stating that sugarEntry doesn’t exist (like what you are experiencing).

There are couple options here:

  1. Set up a SuiteCRM Scheduler job in the Admin Panel and point the scheduler record to the URL that you use for the entryPoint
    NOte if you do this way you will need to make sure you set up a suitable current_user (i.e. Admin) that has the correct access to whatever you are doing like exporting, from within your script.

  2. Set up a scheduler task job (similar to an entrypoint) using the extension framework. It provides you an additional ability to not use the entryPoint and just run the script using the Schedulers Panel in SuiteCRM.

For both options you will need to point your Windows task schedule to the global scheduler script in SuiteCRM rather than an individual file. If you navigate to Admin Panel > Schedulers and there at the bottom of the list of default Scheduler jobs it provides the full path of where you should point your windows task script and it will execute all the jobs within the CRM.

^^ Obviously mine is on apache but it would be tailored to yours.

In Summary, if you want to point your Windows scheduled task to an actual script then remove that line of code. However if you want to be more thorough then chose the two options above.

Hope that helps.

1 Like