Need A Hand
Need A Hand

Reputation: 577

Update and Save a record programmatically in vTiger

Currently I'm trying to change the 'Assigned To' user for a lead to the current user whenever a user enters the lead details screen.

I have the following code:

function checkPermission(Vtiger_Request $request) {

    $moduleName = $request->getModule();
    $recordId = $request->get('record');

    $recordModel = Vtiger_Record_Model::getInstanceById($recordId, $moduleName);

    $recordModel->set('assigned_user_id',$current_user->id);

    $recordModel->save();

    ...

    return true;
}

The problem is, instead of saving the current record with a new assigned user, vTiger duplicates this record into a new record and saves it with the current user as a new assigned user.

Working on vTiger v6.2.0

Upvotes: 2

Views: 4355

Answers (3)

Haidang Nguyen
Haidang Nguyen

Reputation: 171

You need to set mode is edit to recordModel before save

$recordModel->set('mode','edit');

Upvotes: 4

Laila
Laila

Reputation: 1

Tray with event Handler;

create a file: /modules/yourmodulename/handlers/RecordUpdater.php then put the code below in your file RecordUpdater.php :

require_once 'include/events/VTEventHandler.inc';

class yourmodulename_RecordUpdater_Handler extends VTEventHandler {

    function handleEvent($eventName, $data) {
        global $adb;
        $currentUserModel = Users_Record_Model::getCurrentUserModel();
        $module           = $data->getModuleName();

        if ($eventName === 'vtiger.entity.beforesave' AND $module === "yourmodulename") {

                require_once 'modules/yourmodulename/yourmodulename.php';

                $currentUserId = $currentUserModel->getId();

                $data->set('assigned_user_id', $currentUserId);
                }

        }
}

finally don't forget to insert in vtiger_eventhandlers table:

INSERT INTO `vtigercrm`.`vtiger_eventhandlers` (
`eventhandler_id` ,
`event_name` ,
`handler_path` ,
`handler_class` ,
`cond` ,
`is_active` ,
`dependent_on`
)
VALUES (
NULL , 'vtiger.entity.beforesave', 'modules/yourmodulename/handlers/RecordUpdater.php', 'yourmodulename_RecordUpdater_Handler', NULL , '1', '[]'
);

then increment vtiger_eventhandlers_seq with 1

that's it :)

I hope, that helps you

Upvotes: 0

The Fire TITAN
The Fire TITAN

Reputation: 124

Try something like this:

            $recordModel= Vtiger_Record_Model::getInstanceById($recordId,$moduleName);
            $recordModel->set(my_fields, my_new_value);
            $recordModel->set(my_fields2, my_new_value2);
            $recordModel->set('mode', 'edit');
            $recordModel->save();

Upvotes: 0

Related Questions