AnsweredAssumed Answered

How check log in sugar ondemand?

Question asked by Rodolfo Jesus on Mar 29, 2016
Latest reply on Mar 29, 2016 by Matt Marum

How check log in sugar ondemand?

 

I'm install two custom hooks in contacts e leads, but when try convert lead, receive this message.

 

erro01.png

 

My code hook contacts.

<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
 *
 class logic_hooks_class {
  //Função chamada antes da ação salvar
  function before_save_method($bean, $event, $arguments) {


  global $db, $current_user;


  if($this->checkContaExistente($db, $bean) && !$this->checkExistente($db, $bean)){
  require_once 'include/api/SugarApiException.php';  
  throw new SugarApiExceptionInvalidParameter("Numero da conta corretora já existe gravado para outro cliente!");
  }
  else if($this->checkEdition($db, $bean) && !$current_user->is_admin){
        require_once 'include/api/SugarApiException.php';  
  throw new SugarApiExceptionInvalidParameter("Apenas o administrador pode editar os campos nome, cpf, inst. financeira, conta corretora e dígito!"); 
  } 
        


  }


  public function checkExistente($db, $obj){
  $query =  "SELECT c.id_c FROM contacts_cstm c "; 
  $query.= "WHERE c.id_c = '$obj->id' LIMIT 1";
  $results = $db->query($query, true);
  $row = $db->fetchByAssoc($results);
  $num_rows = $results->num_rows;




  if($num_rows > 0)
  return true;
  else
  return false;


  }


  public function checkContaExistente($db, $obj){
  $query =  "SELECT c.lftm_numero_conta_c FROM contacts_cstm c "; 
  $query.= "WHERE c.lftm_numero_conta_c = '$obj->lftm_numero_conta_c' LIMIT 1";
  $results = $db->query($query, true);
  $row = $db->fetchByAssoc($results);
  $num_rows = $results->num_rows;




  if($num_rows > 0)
  return true;
  else
  return false;


  }


  public function checkEdition($db, $obj){
  $query =  "SELECT c.last_name, c.first_name, cstm.lftm_numero_conta_c, cstm.lftm_conta_corretora_digito_c, cstm.lftm_cpf_c, cstm.lftm_instituicao_financeira_c FROM contacts c "; 
  $query.= "INNER JOIN contacts_cstm cstm ON cstm.id_c = c.id ";
  $query.= "WHERE c.id = '$obj->id' LIMIT 1";
  $results = $db->query($query, true);
  $row = $db->fetchByAssoc($results);
  $num_rows = $results->num_rows;
  $first_name = $row['first_name'];
  $last_name = $row['last_name'];
  $num_conta = $row['lftm_numero_conta_c'];
  $dig_conta = $row['lftm_conta_corretora_digito_c'];
  $db_cpf = $row['lftm_cpf_c'];
  $if = $row['lftm_instituicao_financeira_c']; 




  if(($obj->first_name != $first_name || $obj->last_name != $last_name || $obj->lftm_numero_conta_c != $num_conta || 
  $obj->lftm_conta_corretora_digito_c != $dig_conta || $db_cpf != $obj->lftm_cpf_c || $obj->lftm_instituicao_financeira_c != $if) && $num_rows != 0)
  return true;
  else
  return false;


  }
 }
 ?>



 

And my code hook lead.

<?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');    class logic_hooks_class {       //Função chamada antes da ação salvar       function before_save_method($bean, $event, $arguments) {            global $db, $current_user;             $bean->lftm_cpf_c = preg_replace("/\D+/", "", $bean->lftm_cpf_c);            $phones = false;            $email = $this->checkEmail($db, $bean);            if(!empty($bean->phone_mobile) || !empty($bean->phone_work) || !empty($bean->phone_fax) ||                !empty($bean->phone_other) || !empty($bean->assistant_phone) || !empty($bean->phone_home)){                 $phones = $this->checkPhone($db, $bean);            }             if(!empty($email)){                 if(!$this->checkExistente($db, $bean)){                         $res = $db->query("UPDATE email_addr_bean_rel SET deleted = '1' WHERE bean_id = '$bean->id' AND bean_module = 'Leads'");                   }                else{                     $array_email = explode(",", $email['id']);                     foreach ($array_email as $key => $value) {                          if(!empty($value))                               $res = $db->query("UPDATE email_addr_bean_rel SET deleted = '1' WHERE email_address_id = '$value' AND bean_id = '$bean->id' AND bean_module = 'Leads'");                                                       }                }                 require_once 'include/api/SugarApiException.php';                  throw new SugarApiExceptionInvalidParameter("O email ".$email['mail']." já existe gravado para outro Lead!");                }           if($phones){                require_once 'include/api/SugarApiException.php';                  throw new SugarApiExceptionInvalidParameter($phones);                }           if(!empty($bean->lftm_cpf_c) && $this->checkCpf($db, $bean)){                require_once 'include/api/SugarApiException.php';                  throw new SugarApiExceptionInvalidParameter("CPF já existe gravado para outro Lead!");                }                 }        public function checkExistente($db, $obj){            $query =  "SELECT c.id FROM leads c ";            $query.= "WHERE c.id = '$obj->id' LIMIT 1";           $results = $db->query($query, true);           $row = $db->fetchByAssoc($results);           $num_rows = $results->num_rows;            if($num_rows > 0)                return true;           else                return false;        }        public function checkPhone($db, $obj){            //monta where            $where = "";            $where_old_record = "";            if(!empty($obj->phone_mobile))                 $where.= "c.phone_mobile = '$obj->phone_mobile' OR ";            if(!empty($obj->phone_work))                 $where.= "c.phone_work = '$obj->phone_work'  OR ";            if(!empty($obj->phone_fax))                 $where.= "c.phone_fax = '$obj->phone_fax'  OR ";            if(!empty($obj->phone_other))                 $where.= "c.phone_other = '$obj->phone_other'  OR ";            if(!empty($obj->assistant_phone))                 $where.= "c.assistant_phone = '$obj->assistant_phone'  OR ";            if(!empty($obj->phone_home))                 $where.= "c.phone_home = '$obj->phone_home'  OR ";             if (is_array($obj->fetched_row)) {              //adiciona condição para registro existente                 $where_old_record = ") AND (c.id <> '$obj->id' ";           }             $where = substr($where, 0, -3); //remove OR do final da expressão            $where.= $where_old_record."AND"; //inclui AND no final da expressão            //executa query            $query =  "SELECT c.id, c.phone_mobile, c.phone_work, c.phone_fax, c.phone_other, c.assistant_phone, c.phone_home FROM leads c ";             $query.= "WHERE (".$where." deleted = '0') LIMIT 1";                     $results = $db->query($query, true);           $row = $db->fetchByAssoc($results);           $num_rows = $results->num_rows;            //trata retorno           if($num_rows > 0){                $phone_mobile = $row['phone_mobile'];                $phone_work = $row['phone_work'];                $phone_fax = $row['phone_fax'];                $phone_other = $row['phone_other'];                $assistant_phone = $row['assistant_phone'];                $phone_home = $row['phone_home'];                 $return_phones = "";                $cont = 0;                if(!empty($obj->phone_mobile) && $obj->phone_mobile == $phone_mobile){                     $cont++;                     $return_phones.= "celular, ";                }                if(!empty($obj->phone_work) && $obj->phone_work == $phone_work){                     $cont++;                     $return_phones.= "escritório, ";                }                if(!empty($obj->phone_fax) && $obj->phone_fax == $phone_fax){                     $cont++;                     $return_phones.= "fax, ";                }                if(!empty($obj->phone_other) && $obj->phone_other == $phone_other){                     $cont++;                     $return_phones.= "outro, ";                }                if(!empty($obj->assistant_phone) && $obj->assistant_phone == $assistant_phone){                     $cont++;                     $return_phones.= "assistente, ";                }                if(!empty($obj->phone_home) && $obj->phone_home == $phone_home){                     $cont++;                     $return_phones.= "residencial, ";                }                 $return_phones = substr($return_phones, 0, -2); //remove , do final da expressão                if($cont > 1)                     $return_phones = "Os numeros de telefones ".$return_phones." já existem gravado para outro Lead!";                else                     $return_phones = "O numero de telefone ".$return_phones.", já existe gravado para outro Lead!";                 return $return_phones;            }else{                return false;           }        }                public function checkCpf($db, $obj){            $cpf = preg_replace("/\D+/", "", $obj->lftm_cpf_c);            $where = "";            $where_old_record = "";            if(!empty($obj->lftm_cpf_c))                 $where.= "c.lftm_cpf_c = '$cpf'  ";             if (is_array($obj->fetched_row)) {              //adiciona condição para registro existente                 $where_old_record = " AND c.id_c <> '$obj->id' ";           }            $where.= $where_old_record;             $query =  "SELECT c.id_c FROM leads_cstm c ";             $query.= "WHERE ".$where." LIMIT 1";                //$query.= "WHERE c.lftm_cpf_c = '$cpf' AND deleted = '0' LIMIT 1";           $results = $db->query($query, true);           $row = $db->fetchByAssoc($results);           $num_rows = $results->num_rows;            if($num_rows > 0)                return true;           else                return false;        }        //calcula horas de acessorias em reuniões       public function checkEmail($db, $obj){                 $retorno = "";            $id_email = "";            $res = array();            //pega emails desse lead            $query = "SELECT ea.id, ea.email_address FROM email_addresses ea ";             $query.= "INNER JOIN email_addr_bean_rel eabr ON eabr.email_address_id = ea.id AND eabr.bean_module = 'Leads' AND eabr.deleted = 0 ";            $query.= "WHERE ea.deleted = 0 AND eabr.bean_id = '$obj->id' ";           $results = $db->query($query, true);           //$row = $db->fetchByAssoc($results);                          $array_mail = array();           do {               //$email.= $row['email_address']." - ";               $array_mail[$row['id']] = $row['email_address'];           }while($row = $db->fetchByAssoc($results));           //verifica se email existe para outro lead           foreach ($array_mail as $key => $email) {                $query = "SELECT ea.id, ea.email_address FROM email_addresses ea ";                  $query.= "INNER JOIN email_addr_bean_rel eabr ON eabr.email_address_id = ea.id AND eabr.bean_module = 'Leads' AND eabr.deleted = 0 ";                 $query.= "WHERE ea.deleted = 0 AND eabr.email_address_id = '$key' AND eabr.bean_id <> '$obj->id' AND eabr.deleted = 0 ";                $results = $db->query($query, true);                $row = $db->fetchByAssoc($results);                $num_rows = $results->num_rows;                if($num_rows > 0){                     $retorno.= $row['email_address'].", ";                     $id_email.= $row['id'].",";                     $num_rows = 0;                }           }            $res['id'] = $id_email;                $res['mail'] = $retorno;                 if(empty($retorno)){                return "";           }           else{                return $res;           }                  }      }   ?>   

<?phpif (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');// THIS CONTENT IS CREATED BY www.geekvalley.com.br/* * Classe desenvolvimda para manipular os dados antes do salvamento * Informações: www.geekvalley.com.br * * MÓDULO LEADS * - Não permitir gravar se já existir * - E-mail * - CPF * - Telefones ( todos ) */
class logic_hooks_class { //Função chamada antes da ação salvar function before_save_method($bean, $event, $arguments) { global $db, $current_user;
$bean->lftm_cpf_c = preg_replace("/\D+/", "", $bean->lftm_cpf_c); $phones = false; $email = $this->checkEmail($db, $bean); if(!empty($bean->phone_mobile) || !empty($bean->phone_work) || !empty($bean->phone_fax) ||     !empty($bean->phone_other) || !empty($bean->assistant_phone) || !empty($bean->phone_home)){ $phones = $this->checkPhone($db, $bean); }
if(!empty($email)){ if(!$this->checkExistente($db, $bean)){              $res = $db->query("UPDATE email_addr_bean_rel SET deleted = '1' WHERE bean_id = '$bean->id' AND bean_module = 'Leads'");             }          else{          $array_email = explode(",", $email['id']);          foreach ($array_email as $key => $value) {          if(!empty($value))          $res = $db->query("UPDATE email_addr_bean_rel SET deleted = '1' WHERE email_address_id = '$value' AND bean_id = '$bean->id' AND bean_module = 'Leads'");                       }          }
require_once 'include/api/SugarApiException.php';  throw new SugarApiExceptionInvalidParameter("O email ".$email['mail']." já existe gravado para outro Lead!"); } if($phones){ require_once 'include/api/SugarApiException.php';  throw new SugarApiExceptionInvalidParameter($phones); } if(!empty($bean->lftm_cpf_c) && $this->checkCpf($db, $bean)){ require_once 'include/api/SugarApiException.php';  throw new SugarApiExceptionInvalidParameter("CPF já existe gravado para outro Lead!"); }       
}
public function checkExistente($db, $obj){ $query =  "SELECT c.id FROM leads c "; $query.= "WHERE c.id = '$obj->id' LIMIT 1"; $results = $db->query($query, true); $row = $db->fetchByAssoc($results); $num_rows = $results->num_rows;
if($num_rows > 0) return true; else return false;
}
public function checkPhone($db, $obj){ //monta where $where = ""; $where_old_record = ""; if(!empty($obj->phone_mobile)) $where.= "c.phone_mobile = '$obj->phone_mobile' OR "; if(!empty($obj->phone_work)) $where.= "c.phone_work = '$obj->phone_work'  OR "; if(!empty($obj->phone_fax)) $where.= "c.phone_fax = '$obj->phone_fax'  OR "; if(!empty($obj->phone_other)) $where.= "c.phone_other = '$obj->phone_other'  OR "; if(!empty($obj->assistant_phone)) $where.= "c.assistant_phone = '$obj->assistant_phone'  OR "; if(!empty($obj->phone_home)) $where.= "c.phone_home = '$obj->phone_home'  OR ";
if (is_array($obj->fetched_row)) {    //adiciona condição para registro existente $where_old_record = ") AND (c.id <> '$obj->id' "; }
$where = substr($where, 0, -3); //remove OR do final da expressão $where.= $where_old_record."AND"; //inclui AND no final da expressão //executa query $query =  "SELECT c.id, c.phone_mobile, c.phone_work, c.phone_fax, c.phone_other, c.assistant_phone, c.phone_home FROM leads c ";  $query.= "WHERE (".$where." deleted = '0') LIMIT 1"; $results = $db->query($query, true); $row = $db->fetchByAssoc($results); $num_rows = $results->num_rows;
//trata retorno if($num_rows > 0){ $phone_mobile = $row['phone_mobile']; $phone_work = $row['phone_work']; $phone_fax = $row['phone_fax']; $phone_other = $row['phone_other']; $assistant_phone = $row['assistant_phone']; $phone_home = $row['phone_home'];
$return_phones = ""; $cont = 0; if(!empty($obj->phone_mobile) && $obj->phone_mobile == $phone_mobile){ $cont++; $return_phones.= "celular, "; } if(!empty($obj->phone_work) && $obj->phone_work == $phone_work){ $cont++; $return_phones.= "escritório, "; } if(!empty($obj->phone_fax) && $obj->phone_fax == $phone_fax){ $cont++; $return_phones.= "fax, "; } if(!empty($obj->phone_other) && $obj->phone_other == $phone_other){ $cont++; $return_phones.= "outro, "; } if(!empty($obj->assistant_phone) && $obj->assistant_phone == $assistant_phone){ $cont++; $return_phones.= "assistente, "; } if(!empty($obj->phone_home) && $obj->phone_home == $phone_home){ $cont++; $return_phones.= "residencial, "; }
$return_phones = substr($return_phones, 0, -2); //remove , do final da expressão if($cont > 1) $return_phones = "Os numeros de telefones ".$return_phones." já existem gravado para outro Lead!"; else $return_phones = "O numero de telefone ".$return_phones.", já existe gravado para outro Lead!";
return $return_phones;
}else{ return false; }
}

public function checkCpf($db, $obj){ $cpf = preg_replace("/\D+/", "", $obj->lftm_cpf_c); $where = ""; $where_old_record = ""; if(!empty($obj->lftm_cpf_c)) $where.= "c.lftm_cpf_c = '$cpf'  ";
if (is_array($obj->fetched_row)) {    //adiciona condição para registro existente $where_old_record = " AND c.id_c <> '$obj->id' "; }
$where.= $where_old_record;
$query =  "SELECT c.id_c FROM leads_cstm c ";  $query.= "WHERE ".$where." LIMIT 1"; //$query.= "WHERE c.lftm_cpf_c = '$cpf' AND deleted = '0' LIMIT 1"; $results = $db->query($query, true); $row = $db->fetchByAssoc($results); $num_rows = $results->num_rows;
if($num_rows > 0) return true; else return false;
}
//calcula horas de acessorias em reuniões public function checkEmail($db, $obj){ $retorno = ""; $id_email = ""; $res = array(); //pega emails desse lead $query = "SELECT ea.id, ea.email_address FROM email_addresses ea ";  $query.= "INNER JOIN email_addr_bean_rel eabr ON eabr.email_address_id = ea.id AND eabr.bean_module = 'Leads' AND eabr.deleted = 0 "; $query.= "WHERE ea.deleted = 0 AND eabr.bean_id = '$obj->id' "; $results = $db->query($query, true); //$row = $db->fetchByAssoc($results); $array_mail = array(); do {     //$email.= $row['email_address']." - ";     $array_mail[$row['id']] = $row['email_address']; }while($row = $db->fetchByAssoc($results)); //verifica se email existe para outro lead foreach ($array_mail as $key => $email) { $query = "SELECT ea.id, ea.email_address FROM email_addresses ea ";  $query.= "INNER JOIN email_addr_bean_rel eabr ON eabr.email_address_id = ea.id AND eabr.bean_module = 'Leads' AND eabr.deleted = 0 "; $query.= "WHERE ea.deleted = 0 AND eabr.email_address_id = '$key' AND eabr.bean_id <> '$obj->id' AND eabr.deleted = 0 "; $results = $db->query($query, true); $row = $db->fetchByAssoc($results); $num_rows = $results->num_rows; if($num_rows > 0){ $retorno.= $row['email_address'].", "; $id_email.= $row['id'].","; $num_rows = 0; } }
$res['id'] = $id_email; $res['mail'] = $retorno;
if(empty($retorno)){ return ""; } else{ return $res; } }



}
?>

Outcomes