Autopopulate a field from Community Builder

To populate a field with a value from Community Builder, you will require a script in the Default Value area, for example:

$user   = JFactory::getUser();
$db   = JFactory::getDbo();
$userId  = $user->get('id');
// Is the user logged in?
if ($userId) {
  // Grab the value from the database.
    $db->setQuery("SELECT `firstname` FROM `#__comprofiler` WHERE `user_id`='".$userId."'");
    return $db->loadResult();

The above script returns the first name stored in the #__comprofiler table.