1

I am working on vTiger 6.5 and I am trying to figure a way to see if a record exists in a custom module of mine. I want to check whether the 'policynumber' is new before saving, here is my code so far. For some reason it seems to act randomly depending on my module number chosen.

class isaHandler extends VTEventHandler { 
function handleEvent($eventName, $entityData) {
    global $adb;
    $moduleName = $entityData->getModuleName(); 
    if($moduleName=='isa'){     
        if($eventName == 'vtiger.entity.beforesave.modifiable') {
            $isNew = $entityData->isNew('policynumber');
                    if ($isNew == false) {
                echo "Duplicate policy number";
                    exit;
        }
        }

        if($eventName == 'vtiger.entity.beforesave') {}}
        if($eventName == 'vtiger.entity.beforesave.final') {
            $price = $entityData->get('currentamount');
                    if($price > 20000){
                    echo "Please go back and enter less than 20000";
                    exit;
            }

        if($eventName == 'vtiger.entity.aftersave') {}
    }   
}

At the moment I am currently using an echo just to see the result. But later on I will perform more than this.

Diddy
  • 43
  • 1
  • 8
  • `if($eventName == 'vtiger.entity.beforesave') { //put your code inside this and try}` – Bhaskar Jain Apr 03 '17 at 15:48
  • @Bhaskar I had previously tried this and I have just tried this again but I am still able to enter two of the same policy numbers. – Diddy Apr 03 '17 at 15:56
  • Possible duplicate of [vTiger Custom Field Validation beforesave](http://stackoverflow.com/questions/43144675/vtiger-custom-field-validation-beforesave) – Milan Malani Apr 08 '17 at 04:56

2 Answers2

0
isNew()

Returns true if new record is being created, false otherwise. More info is here

you should write a custom query to check policynumber already exist or not in your function:

if($eventName == 'vtiger.entity.beforesave.modifiable') {
        global $adb;

$result = $adb->pquery("SELECT your-field-name FROM table_name WHERE policynumber=?", array($policynumbervalue));
if($result && $adb->num_rows($result)) {
  echo "This policy number exist";
  die();
    }else{
    // write your overwrite code
    }
  } //end if($eventName == 'vtiger.entity.beforesave.modifiable')

Update:

I am assuming there is field i.e. policynumber in your form, you enter some value in this field and submit the form. so you will get entered policy number value from this:

$policynumbervalue = $entityData->get('policynumber'); //this is vtiger standard way

if this does not work, you can simply use php global variable $_REQUEST['policynumber'] but I is not a good practice. Hope this will help.

Bhaskar Jain
  • 1,651
  • 1
  • 12
  • 20
  • Thank you for your respose @Bhaskar, here is what I have got, I am not sure what to put in `policynumbe=?` here is what I have so far: `$result = $adb->pquery("SELECT policynumber FROM vtiger_isa WHERE policynumbe=?", array($policynumbervalue)); if($result && $adb->num_rows($result)) { echo "This policy number exist"; die(); }` – Diddy Apr 03 '17 at 16:42
  • Thank you for your update @Bhaskar, here is what I now have although it does not seem to work. `$policynumbervalue = $entityData->get('policynumber'); $result = $adb->pquery("SELECT policynumber FROM vtiger_isa WHERE policynumber=?", array($policynumbervalue)); if($result && $adb->num_rows($result)) { echo "This policy number exist"; die(); }` – Diddy Apr 04 '17 at 17:51
  • Just to let you know I also tried with `$_REQUEST['policynumber']` – Diddy Apr 04 '17 at 18:29
  • @Diddy, I think this should work, do debugging. 1) `echo $entityData->get('policynumber');` getting policynumber? 2) run a query in DB `SELECT policynumber FROM vtiger_isa WHERE policynumber='put-here-policy-number'` and does it return any row in DB. if these are ok, check `$adb` by doing `print_r($adb);` . Hope You will get some to clue to fix yr problem! – Bhaskar Jain Apr 05 '17 at 08:33
  • thank you very much for your help, after doing some debugging I was able to alter the code slightly and get the result I was looking for. Thanks! – Diddy Apr 05 '17 at 13:17
  • @Diddy, sounds good, it would be great if you post solution too, that will help to others. – Bhaskar Jain Apr 05 '17 at 14:55
0

This is the update to my answer, I simply done an if statement on the number of rows displayed.

    if($eventName == 'vtiger.entity.beforesave.modifiable') {

$policynumbervalue = $entityData->get('policynumber');
$sql = $adb->pquery("SELECT policynumber FROM vtiger_isa WHERE policynumber=?",array($policynumbervalue));
$nrows = $adb->num_rows($sql);
if($nrows > 0){
echo "<script type=\"text/javascript\">window.alert('ISA policy number already exists, you will be redirected to the updata module.');
window.location.href = '/vtigercrm/index.php?module=isa&view=List';</script>"; 
    exit;
}
Diddy
  • 43
  • 1
  • 8