I am calling the Drupal API "user_save" to create a brand new Drupal user. This does create a new Drupal user. But this user does not have the CiviCRM first name or last name fields set. How can I pass first name and last name to the Drupal API "user_save"?

I investigated how CiviCRM handles passing the firstname and last name when someone registers online (which includes a profile that collects first name and last name). CiviCRM only has a mechanism to deal with this situation when someone is online, not using the API, per code in the CiviCRM core file /sites/all/modules/civicrm/drupal/civicrm_user.inc

This issue causes problems with my CiviCRM dedup rule, which is set to check for first name, last name and email. Since for any Drupal user created via the API does not pass the first name or last name, a new CiviCRM contact is created as well. (In addition to the Drupal user)

In my scenario I have a civicrm contact called "testfirst", "testlast", "[email protected]" in the backoffice. I create a Drupal user for this contact (using the API user_save). This contact does get associated with the new Drupal user. However, there is also a brand new contact with the email address "[email protected]" but no other data. This brand new contact is getting created because the unsupervised dedup rule is checking for all 3 fields (first name, last name, and email).

  • Did you check if Drupal Rules would be an option?
    – petednz - fuzion
    Nov 15, 2017 at 19:13

2 Answers 2


The user_save() function can't take CiviCRM data. CiviCRM injects a profile into the user creation page when you create a user through the UI.

To handle your scenario, you'll want to:

  • Call user_save(). It will return a user object, including the UID.
  • Call the CiviCRM UFMatch.get API to get the corresponding Civi contact ID.
  • Call Contact.create on the contact ID to insert the first/last name.

Would it not work to change the dedupe rule to just check for email? Do you have multiple contacts with the same email address and different names?

  • If I change the dedup rule only to look at email, then the issue is avoided. But there are many contacts in my CiviCRM database with shared emails (ie young children have the same email address as a parent). I think the bigger issue is the CiviCRM core logic in "/civicrm_user.inc" assumes that that the only time first name and last name are used is from a screen, never from an API call. Oct 31, 2016 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.