Using Rules to create an Entity Reference if two fields in different entities match? I'm attempting to use a "registration code" entered during a user's registration to create an entity reference to a node with a corresponding code.  

Any help is greatly appreciated.

Specific use case:

"Professor" (users) have "Classes" (nodes) that contain a text-field with a unique 5-digit code (created with Computed Field).

"Students" have a "Registration Code" field on their registration &amp; user profile page (aka a text field on the user entity) for manually entering a 5-digit code that should "assign" them to a "Class" that has the same registration code.

I'm not a programmer, but this might make more sense:

if [Student user's 5-digit registration code] = ["Class" node's 5-digit registration code] then an entity reference is created between [student user] and ["class" node]

I'd like the rule to trigger when a user updates the registration code field.

Essentially, I'm attempting to avoid displaying Entity Reference's autocomplete widget to users.
I don't think I would start with Rules, especially from a performance standpoint. I would first try something like this:


in the Student content type, create an Entity Reference field for the reference, making sure it has a default value, say 0 for ease of access later
Using Field Permissions, hide that field to everyone but admin



  The Field Permissions module allows site administrators to set
  field-level permissions to edit, view and create fields on any entity.



create a custom module
using hook_node_validate(), check the list of Classes for the existence of the correct code
using hook_node_presave(), update the hidden reference field once it is validated


Now you have something that allows the Student to enter a value, have it validated, and then copy that value into a proper reference field for later use

EDIT

I was curious if I was correct in my initial answer, so I figured it out to check, and it worked - for this example:


Create a content type Class, using the title field as the Class Code
Create a content type Student
Add field to Student content type: field_student_class_code, entity reference to Class content type
use Field Permissions to hide field_student_class_code to everyone but Admin
Add field to Student content type: field_student_visible_class_code, text field
Create a new module called custom_node_reference, and include this code:

function custom_node_reference_node_validate($node, $form, &amp;$form_state)
{
  if($node-&gt;type == 'student')
  {
    $lang = $form['field_student_visible_class_code']['#language'];
    $class_value = check_plain($form['field_student_visible_class_code'][$lang][0]['value']['#value']);

    $result = db_select('node', 'table_alias')
        -&gt;condition('type', 'class', '=')
        -&gt;condition('title', $class_value, '=')
        -&gt;fields('table_alias')
        -&gt;execute();

    if($result-&gt;rowCount() &lt;= 0) 
    {
      form_set_error('field_student_visible_class_code', 'That class code does not exist, please re-enter a valid class code');
    }
  }
}



function custom_node_reference_node_presave($node)
{
  if($node-&gt;type == 'student')
  {
    $lang = $node-&gt;language;

    // get entity_id for current class code
    $val = db_select('node', 'n')
      -&gt;fields('n', array('nid'))
      -&gt;condition('type', 'class', '=')
      -&gt;condition('title', $node-&gt;field_student_visible_class_code[$lang][0]['value'], '=')
      -&gt;execute()
      -&gt;fetchField();   

      $node-&gt;field_student_class_code[$lang][0]['target_id'] = $val;
    }
  }



If you change the machine names of the fields, you will need to adjust the code to suit