<?php 
function addContactForAccounts($entity) {
  global $current_user, $adb;
  //code
  try {
    $data = $entity->data;
    $recordId = explode("x",$data['id'])[1];
       $assigned_user_id = explode("x",$data['assigned_user_id'])[1];

    $modelprimary = Vtiger_Record_Model::getCleanInstance('Contacts'); //instancia el Insert del Modulo
    //echo'<pre>';print_r($_SESSION);
    //die();
    $modelprimary->set('mode',''); // lo preparas para el insert
    $modelprimary->set('firstname',$data['cr_nombre']); //selecciona el campo y asignale valores 
    $modelprimary->set('lastname',$data['cr_apepater'].' '.$data['cr_apemater']);
    $modelprimary->set('email',$data['cr_email']); 
    $modelprimary->set('title',$data['cr_cargo']); 
    $modelprimary->set('phone',$data['cr_telef1']); 
    //$modelprimary->set('assigned_user_id',$_SESSION['authenticated_user_id']); 
     $modelprimary->set('assigned_user_id',$assigned_user_id); 
    $modelprimary->save();
    $idget = $modelprimary->getId();
    $sql = "UPDATE vtiger_contactdetails SET accountid = '{$recordId}' where contactid ={$idget} ";
    $resultado = $adb->pquery( 	$sql );
    
  } catch(Exception $e){
     echo $e->getMessage();
  }
}
?>