I created a rule that uses the action Entities > Create a new entity with entity type set to CiviCRM Contact. For the Contact Type field I just entered 1 because that is the ID of the "Individual" contact type in my CiviCRM installation. The only other fields that get populated in this action are First Name and Last Name.

The rules log shows the following error when the rule is triggered:

Unable to create entity civicrm_contact: Entity property civi_user doesn't support writing.

I'm using 7.x-2.0-beta1

CommentFileSizeAuthor
#9 Screenshot from 2016-02-17 09:36:41.png32.78 KBlunk rat

Comments

lunk_rat created an issue. See original summary.

lunk rat’s picture

Issue summary: View changes
markusa’s picture

can you post or send me the export code for your Rule?

lunk rat’s picture

[EDIT] Here is a barebones simple component, which can be executed manually from the components screen. It takes two text parameters (First Name and Last Name). If successful it displays the created CiviCRM contact ID as a site message.

{ "rules_create_contact_in_civicrm" : {
    "LABEL" : "Create contact in CiviCRM",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "first_name" : { "label" : "First Name", "type" : "text" },
      "last_name" : { "label" : "Last Name", "type" : "text" }
    },
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "civicrm_contact",
            "param_contact_type" : "1",
            "param_civi_user" : [ "" ],
            "param_id" : [ "" ],
            "param_contact_sub_type" : [ "" ],
            "param_do_not_email" : [ "" ],
            "param_do_not_phone" : [ "" ],
            "param_do_not_mail" : [ "" ],
            "param_do_not_sms" : [ "" ],
            "param_do_not_trade" : [ "" ],
            "param_is_opt_out" : [ "" ],
            "param_legal_identifier" : [ "" ],
            "param_external_identifier" : [ "" ],
            "param_sort_name" : [ "" ],
            "param_display_name" : [ "" ],
            "param_nick_name" : [ "" ],
            "param_legal_name" : [ "" ],
            "param_image_URL" : [ "" ],
            "param_preferred_communication_method" : [ "" ],
            "param_preferred_language" : [ "" ],
            "param_preferred_mail_format" : [ "" ],
            "param_hash" : [ "" ],
            "param_api_key" : [ "" ],
            "param_first_name" : [ "first-name" ],
            "param_middle_name" : [ "" ],
            "param_last_name" : [ "last-name" ],
            "param_prefix_id" : [ "" ],
            "param_suffix_id" : [ "" ],
            "param_formal_title" : [ "" ],
            "param_communication_style_id" : [ "" ],
            "param_email_greeting_id" : [ "" ],
            "param_email_greeting_custom" : [ "" ],
            "param_email_greeting_display" : [ "" ],
            "param_postal_greeting_id" : [ "" ],
            "param_postal_greeting_custom" : [ "" ],
            "param_postal_greeting_display" : [ "" ],
            "param_addressee_id" : [ "" ],
            "param_addressee_custom" : [ "" ],
            "param_addressee_display" : [ "" ],
            "param_job_title" : [ "" ],
            "param_gender_id" : [ "" ],
            "param_birth_date" : [ "" ],
            "param_is_deceased" : [ "" ],
            "param_deceased_date" : [ "" ],
            "param_household_name" : [ "" ],
            "param_primary_contact_id" : [ "" ],
            "param_organization_name" : [ "" ],
            "param_sic_code" : [ "" ],
            "param_user_unique_id" : [ "" ],
            "param_created_date" : [ "" ],
            "param_modified_date" : [ "" ],
            "param_source" : [ "" ],
            "param_employer_id" : [ "" ],
            "param_is_deleted" : [ "" ],
            "param_current_employer" : [ "" ],
            "param_dupe_check" : [ "" ],
            "param_civicrm_user" : [ "" ]
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      },
      { "drupal_message" : { "message" : "Contact created with ID [entity-created:id]" } }
    ]
  }
}

Here is the result from running this component on a fresh install of Drupal/CiviCRM:

0 ms Executing action rule: Create contact in CiviCRM.
0.591 ms Evaluating the action component_rules_create_contact_in_civicrm.
" Evaluating rule Create contact in CiviCRM. 
0 ms Evaluating rule Create contact in CiviCRM.
0.143 ms Evaluating conditions of rule Create contact in CiviCRM. 
0.152 ms AND evaluated to TRUE.
" Rule Create contact in CiviCRM fires. 
0 ms Rule Create contact in CiviCRM fires.
3.969 ms Evaluating the action entity_create.
5.418 ms Unable to create entity civicrm_contact": Entity property civi_user doesn't support writing.
7.183 ms Unable to evaluate action entity_create. 
8.951 ms Evaluating the action drupal_message. 
9.074 ms Rule Create contact in CiviCRM has fired.
9.267 ms Finished evaluation of rule Create contact in CiviCRM.
10.3 ms Finished executing of action rule: Create contact in CiviCRM.
markusa’s picture

ok and what CiviCRM version are you using? This shouldn't matter but I'd like to know.

lunk rat’s picture

@markusa I'm on 4.6.9 in the dev sandbox that I am working with. I could upgrade to 4.6.12 I suppose.

markusa’s picture

@lunk_rat thanks, I'll review this rule on 4.6.9 and 4.6.12, but I don't think its going to be a CiviCRM specific issue anyway.

lunk rat’s picture

Thank you @markusa for looking into this. I updated #4 with a new component more suitable for testing, which I tested on a fresh install. Same result.

lunk rat’s picture

StatusFileSize
new32.78 KB

I think I might have found a clue. Looking at the export of the rule, the second parameter is param_civi_user. However, in the Rules UI, this second parameter appears with a label "Drupal user" and wants data of type User:

Screenshot of Rules UI

I'm not sure if this is supposed to be the Drupal user that has permission to create contacts in CiviCRM, or the to-be Drupal user of the created contact, or ... what. I tried putting 1 for a value. I also tried 2 (the CiviCRM cid of my Drupal user 1). No dice.

At any rate, since the execution error barks about Entity property civi_user doesn't support writing, I think that the problem lies here.

markusa’s picture

I can replicate the issue. I'll have to dig in now and see what's up.

markusa’s picture

yeah, you know this is a pickle. I see why.. The way rules creates the entity is entirely via entity metadata wrappers, and the civi_user property has no way to set....so we need to find the way for that not to error.

It happens like this:
rule action triggers, which is the function:
rules_action_entity_create() in rules/modules/entity.eval.inc

which calls
entity_property_values_create_entity()

which is throwing the exception originally
http://www.drupalcontrib.org/api/drupal/contributions!entity!includes!en...

what we need is for rules to ignore that property and not include it in its form, so as no value, even null is attempted to be set....

lunk rat’s picture

@markusa Okay, thank you for the explanation. What steps are needed to patch our way out of this pickle?! How can I help?

markusa’s picture

@lunk_rat, I'm nearing to a solution, will be trying to get this into a beta2 release very soon, I've got a batch of updates queued up....later this week hopefully...

lunk rat’s picture

Thank you @markusa, your timing couldn't be better. I sat down today to write a hack-job CiviCRM API rules handler for creating contacts because my project deadline has arrived and I need to create contacts in Rules.

Please let me know if I can help your efforts. Much appreciated! Cheers.

markusa’s picture

I've put in a merge request to get this in....should be coming down on Drupal.org from the maintainer in the next release.

today, you can download my fork of civicrm_entity to get it:
https://github.com/jackrabbithanna/civicrm_entity

lunk rat’s picture

@markusa, thank you for the effort you have put into patching this. I installed the latest 7.x-2.0-beta1+17-dev which has the commit that references this issue. However, unfortunately I am still getting the exact same error:

0 ms Rule Create contact in CiviCRM fires.
16.637 ms Evaluating the action entity_create. 
22.434 ms Unable to create entity civicrm_contact": Entity property civicrm_user doesn't support writing.
22.898 ms Unable to evaluate action entity_create. 
22.96 ms Rule Create contact in CiviCRM has fired.

Any clues?

markusa’s picture

ok, make sure you've cleared your caches

lunk rat’s picture

Cleared site caches, cleared Rules caches, ran cron, ran db updates, etc. etc.

markusa’s picture

ok, saw the problem, I forgot 3 lines in the patch that I pushed up

I've updated the github repo with the fix

lunk rat’s picture

I cloned the repo, cleared caches and created a new component from scratch. Now when I execute, I get:

The website encountered an unexpected error. Please try again later.

markusa’s picture

When I use the "Create entity" action it works for me, but it doesn't work with the component posted here.

I had to set the Contact Type to "Individual" and not 1

You need to use the name of the contact type there....
Individual, Household, or Organization

With that change, your component works for me!

lunk rat’s picture

@markusa thank you! That did the trick. I changed the component so that 'Contact type' contains the string Individual and all is working. This is great! Now we can create CiviCRM contacts with Rules . . . brilliant.

Cheers.

markusa’s picture

Version: 7.x-2.0-beta1 » 7.x-2.x-dev
Status: Active » Closed (fixed)
lunk rat’s picture

I have discovered a further problem: Even though the contact is created, the Provided variable "Created entity" always comes back empty. This should be populated with the newly created contact entity for use in subsequent actions.

markusa’s picture

Status: Closed (fixed) » Needs work
lunk rat’s picture

I can reproduce the problem on a fresh install.

lunk rat’s picture

I learned a bit more about what I am encountering. I was trying to use the civicrm-contact:id of the newly created contact entity before it was actually getting saved to the database. So I guess I need to save the entity before it gets its CID from CiviCRM in order to use the CID in subsequent actions. I tried to execute a "Save entity" action but this action refuses to recognize the newly created civicrm_contact entity as an entity that it can save. Thoughts?

lunk rat’s picture

Status: Needs work » Closed (fixed)

Nevermind, it works like any other Create entity action. For some reason I was expecting it to magically save immediately. When the rule is evaluated, the entity is saved. What I needed to do was create a component that creates the entity and then passes that entity as provided by the component after the component is evaluated. Then in a master rule I can use the provided entity in subsequent components/actions. It all works great!