How to sync user profile fields with civicrm when a user account is created (Drupal 6)
Last updated on
30 April 2025
Make sure you have the Rules module installed.
Create a new triggered rule that occurs when a new user is created.
Create a new action for this rule that executes custom php.
I added this code as my custom php (omit php tags):
profile_load_profile($account);
$firstname = $account->profile_first_name;
$lastname = $account->profile_last_name;
$displayname = $account->name;
$email = $account->mail;
$street = $account->profile_street;
$city = $account->profile_city;
$state = $account->profile_state;
$postalcode = $account->profile_postal_code;
$country = $account->profile_country;
$fax = $account->profile_fax_number;
$phone = $account->profile_phone_number;
$mobile = $account->profile_mobile;
db_set_active('contacts');
$result = db_query("SELECT contact_id FROM civicrm_email WHERE email = '$email'");
while ($node = db_fetch_object($result)) {
$userId = $node->id;
}//end while statement
//insert names into civicrm
//db_query("SELECT * FROM civicrm_contact WHERE id = $userId");
db_query("UPDATE civicrm_contact SET display_name = '$displayname' WHERE id = $userId");
db_query("UPDATE civicrm_contact SET first_name = '$firstname' WHERE id = $userId");
db_query("UPDATE civicrm_contact SET last_name = '$lastname' WHERE id = $userId");
//insert addresses into civicrm
if ($street != '' || $street != NULL || $street != 0 || $city != '' || $city != NULL || $city != 0)
{
db_query("INSERT INTO civicrm_address (street_address, city, contact_id) values ('$street', '$city', $userId)");
}
db_set_active('default');
This code selects the database that stores the civicrm data, loads the profile fields from drupal and writes to the database that stores the civicrm data.
For this code snippet to work you'll need to modify your Drupal settings file so that it allows you to select an alternative database.
Eg.
$db_url['default'] = 'mysqli://user:pass@localhost/drupaldb';
$db_url['contacts'] = 'mysqli://user:pass@localhost/civicrmdb';
Help improve this page
Page status: Not set
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion