public function fetch_data_key() {
        global $CFG, $DB;

        // Only generate a key if Inbound Message is actually enabled, and the handler is enabled.
        if (!isset($CFG->messageinbound_enabled) || !$this->handler || !$this->handler->enabled) {
            return null;
        }

        if (!isset($this->datakey)) {
            // Attempt to fetch an existing key first if one has not already been specified.
            $datakey = $DB->get_field('messageinbound_datakeys', 'datakey', array(
                    'handler' => $this->handler->id,
                    'datavalue' => $this->datavalue,
                ));
            if (!$datakey) {
                $datakey = $this->generate_data_key();
            }
            $this->datakey = $datakey;
        }

        return $this->datakey;
    }