Help so that function can validate the existence of a record through the NIT field

Hi. At this moment I am trying to make a functionality in SuiteCRM for the Account module, so when in a text field called NIT and save it, look in the database for that NIT code and show a message on the screen to the user saying that it already exists and redirects it to the creation page of an account, that is to say that it does not let it create the duplicate. However, in doing this I get the error that I show below. please if anyone knows how to fix this problem for me to function properly this function.

My code is the following:

/// ******************************************archivo ValidateDuplicateRecord.php **************************************************************//

class ValidateDuplicateRecord {

//Checks if the account name already exists
function validateDuplicateRecord( $bean, $event, $arguments ) {

$query_buscar_nombre = "SELECT COUNT(id) AS num FROM accounts WHERE name = '$bean->name'";
$v_sql_bean = $bean->db->query($query_buscar_nombre,true);
$row_beanrad = $bean->db->fetchByAssoc($v_sql_bean);
$nombre_encontrado = $row_beanrad['num'];

//Validar Si nombre Existe
if($nombre_encontrado > 0)
{

	SugarApplication :: appendErrorMessage ("<strong><font size='6'>el registro no se puede guardar !!!.</font></strong>"); 
            $ params = array ( 
              'module' => 'Accounts', 
              'action' => 'EditView', 
               'record' => $bean-> id 
            ); 

SugarApplication :: redirect (‘index.php?’. Http_build_query ($ params). ‘& Return_module = Accounts & return_action = EditView & offset = 1’);

}

}

}

////////////////////////////////////// fin del código ///////////////////////////////////////////////////////////

Pero al momento de crear un registro de cuenta nueva ingresando un nombre que no existe me sale este una vista de texto con los siguiente:

class ValidateDuplicateRecord { //Checks if the account name already exists function validateDuplicateRecord( $bean, $event, $arguments ) { $query_buscar_nombre = “SELECT COUNT(id) AS num FROM accounts WHERE name = ‘$bean->name’ AND deleted = 0”; $v_sql_bean = $bean->db->query($query_buscar_nombre,true); $row_beanrad = $bean->db->fetchByAssoc($v_sql_bean); $nombre_encontrado = $row_beanrad[‘num’]; //Validar Si nombre Existe if($nombre_encontrado > 0) { SugarApplication::appendErrorMessage(’
Record saved successfully!
‘); exit; //SugarApplication :: appendErrorMessage (“el registro no se puede guardar !!!.”);� ����������������//$params = array (� �����������������//�’module’ => ‘Accounts’,� ����������������//��’action’ => ‘EditView’,� �����������������//��’record’ => $bean-> id� ���������������//�);� ��������//SugarApplication :: redirect (‘index.php?’. Http_build_query ($params). ‘& Return_module = Accounts & return_action = EditView & offset = 1’); } } function methodMayusBSHook($bean, $event, $arguments) { $bean->name=strtoupper($bean->name); } }

/////// por favor si alguien que pueda ayudarme a corregir el error y lograr que funcione lo que pretendo hacer. Gracias. /////////////////////////

Not sure if this is the real issue but I see some errors on your query:

  1. You need to concatenate query properly:
$query_buscar_nombre = "SELECT COUNT(id) AS num FROM accounts WHERE name = '".$bean->name."'";
  1. The result should be called with the alias you are using on the query:
$nombre_encontrado = $row_beanrad['num'];

Try that and test it.

Hope it helps

OK, but I need to know if the source code I show below is well written and would work to redirect to the editing view when the name of the account already exists so that it does not allow you to create it again. Only in case there is no left to save, I do not know if it is clear what I would like to do.

The source code is the following:

if($nombre_encontrado > 0)
{

	SugarApplication :: appendErrorMessage ('el registro no se puede guardar !!!.</font></strong>'); 
            $ params = array ( 
              'module' => 'Accounts', 
              'action' => 'EditView', 
               'record' => $bean-> id 
            ); 

SugarApplication :: redirect (‘index.php?’. Http_build_query ($ params). ‘& Return_module = Accounts & return_action = EditView & offset = 1’);
}