AnsweredAssumed Answered

How can I ensure leads inserted via the API are not duplicates of existing data?

Question asked by Michael Michael on Feb 26, 2014
Latest reply on Jul 15, 2014 by zymba2k zymba2k
Hi all,
I ́m trying to import data from mysql database and convert them into LEADS via SOAP and here is my problem.

How can I write a function, which before creating LEAD will check, if that row (from which is created new LEAD) isn ́t already imported to Sugar CRM.

Example:
I have mysql database where I have important data like a first_name, last_name, id etc.
and this database is everyday filled with new data and keeping existind data. I need to fill SugarCRM with this data, not updating already existing, but compare, if id from source database is already in sugarcrm database ... if yes, jump to next row and compare other id ....
when it will find id, which is in source database, but not in sugarcrm database, create lead and import there that data and jump to next row and make new lead etc.

Here is my code (this is not final version):

<?php

function connectDatabase($host, $user, $password, $databaseName) {
    $returnValue = NULL;
    
    $returnValue = mysqli_connect($host, $user, $password, $databaseName);
    
    if (mysqli_connect_errno()) {
        echo "Can't connect to db $databaseName: " . mysqli_error();
        }
    
    return $returnValue;
}

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

set_time_limit(0);

$portal_name ='example';
$portal_password = 'example';
$user_name ='lead';
$user_password = 'lead';
foreach($_POST as $name=>$value){
  $$name = $value;
}

//EOQ;

if(!empty($portal_name)){
$portal_password = md5($portal_password);
require_once('../include/nusoap/nusoap.php');

# $soapclient = new nusoapclient($GLOBALS['sugar_config']['site_url'].'/soap.php');  //define the SOAP Client an 
$soapclient = new nusoapclient('http://example bla bla bla');  //define the SOAP Client an

echo '<b>LOGIN:</b><BR>';
$result = $soapclient->call('portal_login',array('portal_auth'=>array('user_name'=>$portal_name,'password'=>$portal_password, 'version'=>'.01'),'user_name'=>$user_name, 'application_name'=>'SoapTestPortal'));
echo '<b>HERE IS ERRORS:</b><BR>';
echo $soapclient->error_str;

echo '<BR><BR><b>HERE IS RESPONSE:</b><BR>';
echo $soapclient->response;

echo '<BR><BR><b>HERE IS RESULT:</b><BR>';
echo print_r($result);
$session = $result['id'];

$dbConnection1 = connectDatabase("host", "user", "password", "dbName");

$resource = "SELECT * FROM !tableName!";
$query = mysqli_query($resource);

$resultIds = array();

while ($row = mysqli_fetch_assoc($query)) {
// MUST BE THAT FUNCTION HERE OR ??

//lead
$result = $soapclient->call(
    'portal_set_entry',
    array(
     'session'=>$session , 
     'module_name'=>'Leads', 
     'name_value_list'=>array(
      array(
       'name'=>'idpoptavky',
       'value'=>$row['idpoptavky']
      ),
      array(
       'name'=>'first_name', 
       'value'=>$row['kontakt_osoba']
      ),
      array(
       'name'=>'account_name', 
       'value'=>$row['odberatel']
      ), 
      array(
       'name'=>'status_description', 
       'value'=>$row['realizace']
      ), 
      array(
       'name'=>'opportunity_amount', 
       'value'=>$row['cena']
      ),
      array(
       'name'=>'department',
       'value'=>$row['stat']
      ),
      array(
       'name'=>'description', 
       'value'=>$row['poptavka']
      ),
      array(
       'name'=>'mena',
       'value'=>$row['mena']
      ),
      array(
       'name'=>'lead_source_description',
       'value'=>$row['poznamka']
      )
     )
    )
   );

if ($soapclient–>error_str) {
  echo $soapclient->error_str;
  echo "<br />";
}

echo print_r($result);
echo "<br />";

$resultIds[] = $result['id'];
}

echo '<br><br><b>LOGOUT:</b><BR>';
$result = $soapclient->call('portal_logout',array('session'=>$session));
echo '<b>HERE IS ERRORS:</b><BR>';
echo $soapclient->error_str;

echo '<BR><BR><b>HERE IS RESPONSE:</b><BR>';
echo $soapclient->response;

echo '<BR><BR><b>HERE IS RESULT:</b><BR>';
echo print_r($result);
}

?>

I will appreciate any advice ... I ́m really frustrated with this :(((

Sorry for my english

Thanks

Michael

Outcomes