public function updateBean(\SugarBean $sugarBean, array $data, $saveMode)
    {
        $code = self::SUGAR_NOTCHANGED;

        // Save my ID
        if (!empty($sugarBean->id)) {
            $this->lastUpdatedId = $sugarBean->id;
        }

        $changedValues = $this->updateBeanFields($sugarBean, $data);
        if ($changedValues === 0) {
            if (!($saveMode & self::MODE_UPDATE)) {
                $msg = 'Error: Won\'t create an empty bean.';
                $this->getLogger()->info($this->logPrefix . $msg);
                throw new UpdateBeanException($msg, self::SUGAR_FIELDS_NOT_MODIFIED);
            }
            // Update mode, this is not an error we just notify that nothing changed.
            $msg = 'Not Saving the bean because the records are identical.';
            $this->getLogger()->info($this->logPrefix . $msg);

            return $code;
        }

        $this->updateBeanFieldsFromCurrentUser($sugarBean);


        if ($saveMode === self::MODE_DRY_RUN) {
            $this->getLogger()->info($this->logPrefix . 'Dry run. Won\'t save anything.');
            return $code;
        }

        if (empty($sugarBean->id)) {
            // We have a new Bean
            if ($saveMode & self::MODE_CREATE) {
                $code = self::SUGAR_CREATED;
                $this->getLogger()->info($this->logPrefix . 'Creating a new record.');
            } else {
                $msg = 'Won\'t create a new record with an empty id when not in CREATE mode.';
                $this->getLogger()->info($this->logPrefix . $msg);
                throw new UpdateBeanException($msg, self::SUGAR_NOTCHANGED);
            }
        } else {
            if ($saveMode & self::MODE_UPDATE) {
                $code = self::SUGAR_UPDATED;
                $this->getLogger()->info($this->logPrefix . "Updating record with id '{$sugarBean->id}'.");
            } elseif ($saveMode & self::MODE_CREATE_WITH_ID) {
                $sugarBean->new_with_id = 1;
                $code = self::SUGAR_CREATED;
                $this->getLogger()->info($this->logPrefix . "Creating new record with id '{$sugarBean->id}'.");
            } else {
                $msg = "Will not save record with id '{$sugarBean->id}'. Because not in UPDATE or CREATE_WITH_ID mode.";
                $this->getLogger()->info($this->logPrefix . $msg);
                throw new UpdateBeanException($msg, self::SUGAR_NOTCHANGED);
            }
        }

        $sugarBean->save();

        // save my new ID
        $this->lastUpdatedId = $sugarBean->id;

        $msg = "Bean with ID {$sugarBean->id} saved because {$changedValues} value(s) ha(s)ve been changed";
        $this->getLogger()->info($this->logPrefix . $msg);

        // clean memory
        $this->cleanMemory();

        return $code;
    }