Vtiger workflow- stop operation if field validation fails

Firstly, I need to validate the serial numbers put in by users to make sure it is correct with what is already in database. However, I noticed even if the wrong serial number is inputed, the page reloads (shows the error message) and still fills in the wrong serial number in the field cf_954. So whether it is wrong or right, the field still gets populated.

Secondly, i noticed that the alert script only shows in mobile not on desktop browser.

<?php

/*** 
This code was rewritten by Peter Iteka 
The Original code is placed in Root dir as OLD CODES
*****/

// Enable error reporting for debugging purposes
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);

function Jobs_DeliverBinDelivered_field_Workflow($entity) {
    global $current_user, $adb, $log;

    // Prevent any output before the JSON response
    if (ob_get_length()) ob_end_clean(); // Ensure any prior output buffer is cleaned.

    // Include necessary vtiger utility files
    include_once 'include/utils/CommonUtils.php';
    include_once 'include/database/PearDatabase.php';

    // Extract the entity ID
    $id = explode("x", $entity->get('id'));
    if (count($id) < 2) {
        error_log("Invalid entity ID format.");
        return;
    }
    $potid = $id[1];

    // Ensure required fields are set
    if (!isset($entity->data['cf_accounts_id']) || !isset($entity->data['cf_954'])) {
        error_log("Required fields are missing in the entity data.");
        return;
    }

    // Retrieve field values
    $acc_id = explode("x", $entity->data['cf_accounts_id']);
    if (count($acc_id) < 2) {
        error_log("Invalid cf_accounts_id format.");
        return;
    }
    $accounts_id = $acc_id[1];
    $serialnumber = $entity->data['cf_954'];

    // Define an array of valid product IDs
    $valid_products = [
        6570, 6569, 6571, 6574, 6575, 6576, 6577, 6578, 6580, 6588, 
        6589, 6591, 6593, 6598, 6603, 6605, 6610, 6611, 6628, 6634, 
        6637, 7065, 7479, 11047, 11061, 11196, 12518, 20067, 20210, 
        30015, 51241, 60124, 60546, 63629, 72512, 72513, 72596, 77932, 
        77944, 78159, 79504, 80215, 107739, 132729, 144886, 148546, 
        149185, 152960, 156049, 156354, 160736, 160737
    ];

    // Prepare and execute the SQL query to find assets with the given serial number and a valid product
    $sql = "SELECT * FROM `vtiger_assets` WHERE `serialnumber` = ? AND `product` IN (" . implode(',', $valid_products) . ")";
    $result = $adb->pquery($sql, array($serialnumber));

    if ($result === false) {
        error_log("Database query failed: " . $adb->database->ErrorMsg());
        return;
    }

    $row_find_count = $adb->num_rows($result);

    function phpAlert($msg) {
        echo '<script type="text/javascript">
                alert("' . $msg . '");
                window.location.reload();
              </script>';
    }

    // If no assets are found, reject the serial number and return a JSON error message
    if ($row_find_count == 0) {
        // Log error for internal purposes
        error_log("Invalid serial number: $serialnumber or product mismatch. Contact the office for assistance.");

        // Show an alert to the user
        phpAlert("Invalid serial number or Bin number mismatch. Input the correct one or call the office. Thank you.");

        // Ensure that the response is sent and the connection is closed
        die();
        flush();
        exit(); // Prevent further execution
    }

    // If assets are found, proceed with updating them
    $ACC_ID = array();
    for ($i = 0; $i < $row_find_count; $i++) {
        $ACC_ID[] = $adb->query_result($result, $i, 'assetsid');
    }

    // Update each found asset record with the new account ID
    foreach ($ACC_ID as $row_id) {
        $recordModel = Vtiger_Record_Model::getInstanceById($row_id, 'Assets');
        if ($recordModel) {
            $recordModel->set('account', $accounts_id);
            $recordModel->set('mode', 'edit');
            $recordModel->save();
        } else {
            error_log("Failed to load record model for assetsid: " . $row_id);
        }
    }
}
?>

Used this function to stop the code from running

  if ($row_find_count == 0) {
        // Log error for internal purposes
        error_log("Invalid serial number: $serialnumber or product mismatch. Contact the office for assistance.");

        // Show an alert to the user
        phpAlert("Invalid serial number or Bin number mismatch. Input the correct one or call the office. Thank you.");

        // Ensure that the response is sent and the connection is closed
        die();
        flush();
        exit(); // Prevent further execution
    }

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật