How to fetch the completed goal as an entity with the Rules module? Followup question for the question about "How to access fields from a goal when creating a node with Rules?".

I'm trying to create a rule that finds and uses the fields of the goal that triggered the goal completed event. They can be fetched if you know the goal id (as shown in the answer to the previous question). But creating individual rules for each goal is not viable because there are quite a few of them and I'd like administrators to be able to easily add them without having to write new rules.

So how can I create a rule without specifying a Rules Condition to check the id of the goal that was completed?
You could use the Views Rules to get this to work. Read on for more details about how to do so ...

Step 1: Create an appropriate view

Create a view of all goals completed by the current user, sorted (descending) by the date the goal was completed, and a "Rules" display type, in which you use a pager to only show 1 result (i.e. the most recent one ...).

Here is a view (in export format) with (a) a page display (to list all goals completed by the current user) and (b) a Rules display (to only list the most recent completed goal):

$view = new view();
$view-&gt;name = 'goals_info_about_completed_goals';
$view-&gt;description = 'A list of goals completed by a user.';
$view-&gt;tag = 'Goals';
$view-&gt;base_table = 'goals';
$view-&gt;human_name = 'Goals completed by a user';
$view-&gt;core = 7;
$view-&gt;api_version = '3.0';
$view-&gt;disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view-&gt;new_display('default', 'Master', 'default');
$handler-&gt;display-&gt;display_options['title'] = 'Goals completed by a user';
$handler-&gt;display-&gt;display_options['use_more_always'] = FALSE;
$handler-&gt;display-&gt;display_options['access']['type'] = 'none';
$handler-&gt;display-&gt;display_options['cache']['type'] = 'none';
$handler-&gt;display-&gt;display_options['query']['type'] = 'views_query';
$handler-&gt;display-&gt;display_options['exposed_form']['type'] = 'basic';
$handler-&gt;display-&gt;display_options['pager']['type'] = 'none';
$handler-&gt;display-&gt;display_options['pager']['options']['offset'] = '0';
$handler-&gt;display-&gt;display_options['style_plugin'] = 'table';
$handler-&gt;display-&gt;display_options['style_options']['columns'] = array(
  'goal_img' =&gt; 'goal_img',
);
$handler-&gt;display-&gt;display_options['style_options']['default'] = '-1';
$handler-&gt;display-&gt;display_options['style_options']['info'] = array(
  'goal_img' =&gt; array(
    'sortable' =&gt; 0,
    'default_sort_order' =&gt; 'asc',
    'align' =&gt; '',
    'separator' =&gt; '',
    'empty_column' =&gt; 0,
  ),
);
/* Relationship: Goal: Goal - completed by users */
$handler-&gt;display-&gt;display_options['relationships']['goals_completed_goals']['id'] = 'goals_completed_goals';
$handler-&gt;display-&gt;display_options['relationships']['goals_completed_goals']['table'] = 'goals';
$handler-&gt;display-&gt;display_options['relationships']['goals_completed_goals']['field'] = 'goals_completed_goals';
$handler-&gt;display-&gt;display_options['relationships']['goals_completed_goals']['label'] = 'Completed goal';
/* Field: Goal: Goal Image */
$handler-&gt;display-&gt;display_options['fields']['goal_img']['id'] = 'goal_img';
$handler-&gt;display-&gt;display_options['fields']['goal_img']['table'] = 'field_data_goal_img';
$handler-&gt;display-&gt;display_options['fields']['goal_img']['field'] = 'goal_img';
$handler-&gt;display-&gt;display_options['fields']['goal_img']['label'] = 'Goal image';
$handler-&gt;display-&gt;display_options['fields']['goal_img']['element_label_colon'] = FALSE;
$handler-&gt;display-&gt;display_options['fields']['goal_img']['click_sort_column'] = 'fid';
$handler-&gt;display-&gt;display_options['fields']['goal_img']['settings'] = array(
  'image_style' =&gt; 'goal_image',
  'image_link' =&gt; '',
);
/* Field: Goal: URL */
$handler-&gt;display-&gt;display_options['fields']['url']['id'] = 'url';
$handler-&gt;display-&gt;display_options['fields']['url']['table'] = 'views_entity_goal';
$handler-&gt;display-&gt;display_options['fields']['url']['field'] = 'url';
$handler-&gt;display-&gt;display_options['fields']['url']['exclude'] = TRUE;
$handler-&gt;display-&gt;display_options['fields']['url']['link_to_entity'] = 0;
/* Field: Goal: Title */
$handler-&gt;display-&gt;display_options['fields']['title_1']['id'] = 'title_1';
$handler-&gt;display-&gt;display_options['fields']['title_1']['table'] = 'goals';
$handler-&gt;display-&gt;display_options['fields']['title_1']['field'] = 'title';
$handler-&gt;display-&gt;display_options['fields']['title_1']['alter']['make_link'] = TRUE;
$handler-&gt;display-&gt;display_options['fields']['title_1']['alter']['path'] = '[url]';
/* Field: Goal: Goal ID */
$handler-&gt;display-&gt;display_options['fields']['goal_id']['id'] = 'goal_id';
$handler-&gt;display-&gt;display_options['fields']['goal_id']['table'] = 'goals';
$handler-&gt;display-&gt;display_options['fields']['goal_id']['field'] = 'goal_id';
/* Field: Goal: User Points */
$handler-&gt;display-&gt;display_options['fields']['goal_userpoints']['id'] = 'goal_userpoints';
$handler-&gt;display-&gt;display_options['fields']['goal_userpoints']['table'] = 'field_data_goal_userpoints';
$handler-&gt;display-&gt;display_options['fields']['goal_userpoints']['field'] = 'goal_userpoints';
$handler-&gt;display-&gt;display_options['fields']['goal_userpoints']['settings'] = array(
  'thousand_separator' =&gt; '',
  'prefix_suffix' =&gt; 1,
);
/* Field: Goal: Goal Category */
$handler-&gt;display-&gt;display_options['fields']['field_goal_category']['id'] = 'field_goal_category';
$handler-&gt;display-&gt;display_options['fields']['field_goal_category']['table'] = 'field_data_field_goal_category';
$handler-&gt;display-&gt;display_options['fields']['field_goal_category']['field'] = 'field_goal_category';
/* Field: Goal: Goal Level */
$handler-&gt;display-&gt;display_options['fields']['field_goal_level']['id'] = 'field_goal_level';
$handler-&gt;display-&gt;display_options['fields']['field_goal_level']['table'] = 'field_data_field_goal_level';
$handler-&gt;display-&gt;display_options['fields']['field_goal_level']['field'] = 'field_goal_level';
/* Field: Goal - completed by users: Created */
$handler-&gt;display-&gt;display_options['fields']['created']['id'] = 'created';
$handler-&gt;display-&gt;display_options['fields']['created']['table'] = 'goals_completed_goals';
$handler-&gt;display-&gt;display_options['fields']['created']['field'] = 'created';
$handler-&gt;display-&gt;display_options['fields']['created']['relationship'] = 'goals_completed_goals';
$handler-&gt;display-&gt;display_options['fields']['created']['label'] = 'Completion date';
$handler-&gt;display-&gt;display_options['fields']['created']['date_format'] = 'short';
$handler-&gt;display-&gt;display_options['fields']['created']['second_date_format'] = 'long';
/* Field: Goal - completed by users: User */
$handler-&gt;display-&gt;display_options['fields']['uid']['id'] = 'uid';
$handler-&gt;display-&gt;display_options['fields']['uid']['table'] = 'goals_completed_goals';
$handler-&gt;display-&gt;display_options['fields']['uid']['field'] = 'uid';
$handler-&gt;display-&gt;display_options['fields']['uid']['relationship'] = 'goals_completed_goals';
/* Field: Goal - completed by users: Goal */
$handler-&gt;display-&gt;display_options['fields']['goal_id_1']['id'] = 'goal_id_1';
$handler-&gt;display-&gt;display_options['fields']['goal_id_1']['table'] = 'goals_completed_goals';
$handler-&gt;display-&gt;display_options['fields']['goal_id_1']['field'] = 'goal_id';
$handler-&gt;display-&gt;display_options['fields']['goal_id_1']['relationship'] = 'goals_completed_goals';
/* Field: Goal - completed by users: Goal - completed by users ID */
$handler-&gt;display-&gt;display_options['fields']['id']['id'] = 'id';
$handler-&gt;display-&gt;display_options['fields']['id']['table'] = 'goals_completed_goals';
$handler-&gt;display-&gt;display_options['fields']['id']['field'] = 'id';
$handler-&gt;display-&gt;display_options['fields']['id']['relationship'] = 'goals_completed_goals';
$handler-&gt;display-&gt;display_options['fields']['id']['label'] = 'Completed Goal Seqnr';
/* Sort criterion: Goal - completed by users: Created */
$handler-&gt;display-&gt;display_options['sorts']['created']['id'] = 'created';
$handler-&gt;display-&gt;display_options['sorts']['created']['table'] = 'goals_completed_goals';
$handler-&gt;display-&gt;display_options['sorts']['created']['field'] = 'created';
$handler-&gt;display-&gt;display_options['sorts']['created']['relationship'] = 'goals_completed_goals';
$handler-&gt;display-&gt;display_options['sorts']['created']['order'] = 'DESC';
/* Contextual filter: Goal - completed by users: User */
$handler-&gt;display-&gt;display_options['arguments']['uid']['id'] = 'uid';
$handler-&gt;display-&gt;display_options['arguments']['uid']['table'] = 'goals_completed_goals';
$handler-&gt;display-&gt;display_options['arguments']['uid']['field'] = 'uid';
$handler-&gt;display-&gt;display_options['arguments']['uid']['relationship'] = 'goals_completed_goals';
$handler-&gt;display-&gt;display_options['arguments']['uid']['default_action'] = 'default';
$handler-&gt;display-&gt;display_options['arguments']['uid']['default_argument_type'] = 'current_user';
$handler-&gt;display-&gt;display_options['arguments']['uid']['summary']['number_of_records'] = '0';
$handler-&gt;display-&gt;display_options['arguments']['uid']['summary']['format'] = 'default_summary';
$handler-&gt;display-&gt;display_options['arguments']['uid']['summary_options']['items_per_page'] = '25';

/* Display: Page */
$handler = $view-&gt;new_display('page', 'Page', 'page_1');
$handler-&gt;display-&gt;display_options['path'] = 'user/%/completed-goals';

/* Display: RulesList */
$handler = $view-&gt;new_display('views_rules', 'RulesList', 'views_rules_1');
$handler-&gt;display-&gt;display_options['pager']['type'] = 'some';
$handler-&gt;display-&gt;display_options['pager']['options']['items_per_page'] = '1';
$handler-&gt;display-&gt;display_options['pager']['options']['offset'] = '0';
$handler-&gt;display-&gt;display_options['defaults']['fields'] = FALSE;
/* Field: Goal: URL */
$handler-&gt;display-&gt;display_options['fields']['url']['id'] = 'url';
$handler-&gt;display-&gt;display_options['fields']['url']['table'] = 'views_entity_goal';
$handler-&gt;display-&gt;display_options['fields']['url']['field'] = 'url';
$handler-&gt;display-&gt;display_options['fields']['url']['exclude'] = TRUE;
$handler-&gt;display-&gt;display_options['fields']['url']['link_to_entity'] = 0;
/* Field: Goal: Title */
$handler-&gt;display-&gt;display_options['fields']['title_1']['id'] = 'title_1';
$handler-&gt;display-&gt;display_options['fields']['title_1']['table'] = 'goals';
$handler-&gt;display-&gt;display_options['fields']['title_1']['field'] = 'title';
$handler-&gt;display-&gt;display_options['fields']['title_1']['alter']['make_link'] = TRUE;
$handler-&gt;display-&gt;display_options['fields']['title_1']['alter']['path'] = '[url]';
/* Field: Goal: Goal ID */
$handler-&gt;display-&gt;display_options['fields']['goal_id']['id'] = 'goal_id';
$handler-&gt;display-&gt;display_options['fields']['goal_id']['table'] = 'goals';
$handler-&gt;display-&gt;display_options['fields']['goal_id']['field'] = 'goal_id';
$handler-&gt;display-&gt;display_options['rules_parameter'] = array(
  'uid' =&gt; array(
    'enabled' =&gt; 1,
    'type' =&gt; 'integer',
    'label' =&gt; 'Goal completed by user with id',
    'name' =&gt; 'goal_completed_by_uid',
  ),
);
$handler-&gt;display-&gt;display_options['rules_variables'] = array(
  'url' =&gt; array(
    'enabled' =&gt; 0,
    'rendered' =&gt; 0,
    'type' =&gt; '',
    'label' =&gt; 'URL',
    'name' =&gt; 'url',
  ),
  'title_1' =&gt; array(
    'enabled' =&gt; 1,
    'rendered' =&gt; 0,
    'type' =&gt; 'text',
    'label' =&gt; 'Title',
    'name' =&gt; 'goal_title',
  ),
  'goal_id' =&gt; array(
    'enabled' =&gt; 1,
    'rendered' =&gt; 0,
    'type' =&gt; 'integer',
    'label' =&gt; 'Goal ID',
    'name' =&gt; 'goal_id',
  ),
  'field_goal_category' =&gt; array(
    'enabled' =&gt; 1,
    'rendered' =&gt; 0,
    'type' =&gt; 'text',
    'label' =&gt; 'Goal Category',
    'name' =&gt; 'goal_category',
  ),
  'field_goal_level' =&gt; array(
    'enabled' =&gt; 1,
    'rendered' =&gt; 0,
    'type' =&gt; 'text',
    'label' =&gt; 'Goal Level',
    'name' =&gt; 'goal_level',
  ),
  'goal_userpoints' =&gt; array(
    'enabled' =&gt; 0,
    'rendered' =&gt; 0,
    'type' =&gt; '',
    'label' =&gt; 'User Points',
    'name' =&gt; 'goal_userpoints',
  ),
  'uid' =&gt; array(
    'enabled' =&gt; 1,
    'rendered' =&gt; 0,
    'type' =&gt; 'integer',
    'label' =&gt; 'Id of User completing goal',
    'name' =&gt; 'goal_uid',
  ),
);


Before trying to import the above view in your own site, make sure to define these custom fields for the goals you configured (these fields are select lists with a single value allowed):


Goal Category (machine name field_goal_category), eg with values such as "Privilege, Contribution, Participation, Moderation, None".
Goal Level (machine name field_goal_level), eg with values such as "Bronze, Silver, Gold, None".


Step 2: Create a rule that contains a Views loop

Create a rule which contains a "Views loop" as a Rules Action, which is similar to a regular Rules loop, but allows looping over rows of view results. Use the Rules Display type of the view created in the previous step. Obviously, because of the number of results we configured in the Rules display of the view, this list consists of only 1 list item (i.e. related to the most recently completed goal).


Within that "Views loop", perform a 1st Rules Action to "Fetch an entity by id", and with these additional details:


Parameter: Entity type: Goal
Identifier: [goal-id]
Provides variables: Completed goal (goal_completed)

Then add a 2nd Rules Action, within that same "Views loop", something like "Show a message on the site", with a message that looks something like so (note the 2 custom fields included, with machine names field_goal_category and  field_goal_level):


  Details about completed goal:
  
  
  Goal ID: [goal-completed:goal-id]
  Goal Title: [goal-completed:title]
  Goal Category: [goal-completed:field_goal_category]
  Goal Level: [goal-completed:field_goal_level]
  



Here is an export of a rule you can use for this (if you have both custom fields defined, you should be able to import this rule in your own site):

{ "rules_show_details_about_completed_goal" : {
    "LABEL" : "Show details about completed goal",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "goals" ],
    "REQUIRES" : [ "rules", "goals" ],
    "ON" : { "goal_awarded" : [] },
    "DO" : [
      { "VIEW LOOP" : {
          "VIEW" : "goals_info_about_completed_goals",
          "DISPLAY" : "views_rules_1",
          "USING" : { "goal_completed_by_uid" : [ "awarded-user:uid" ] },
          "ROW VARIABLES" : {
            "goal_title" : { "goal_title" : "Title" },
            "goal_id" : { "goal_id" : "Goal ID" }
          },
          "DO" : [
            { "entity_fetch" : {
                "USING" : { "type" : "goal", "id" : [ "goal-id" ] },
                "PROVIDE" : { "entity_fetched" : { "goal_completed" : "Completed goal" } }
              }
            },
            { "drupal_message" : { "message" : "Details about completed goal:\u003Cbr \/\u003E\u003Cbr \/\u003E\r\n- Goal ID: [goal-completed:goal-id]\u003Cbr \/\u003E\r\n- Goal Title: [goal-completed:title]\u003Cbr \/\u003E\r\n- Goal Category: [goal-completed:field_goal_category]\u003Cbr \/\u003E\r\n- Goal Level: [goal-completed:field_goal_level]\u003Cbr \/\u003E" } }
          ]
        }
      }
    ]
  }
}


Here are the "Replacement patterns for Completed goal" that are shown with the Rules Action to Show a message on the site:

[goal-completed:title]                  Title               Goal "title" property.
[goal-completed:created]                Created             The date the goal was created.
[goal-completed:changed]                Changed             The date the goal was changed.
[goal-completed:Goal ID]                Label               The human readable label.
[goal-completed:url]                    URL                 The URL of the entity.
[goal-completed:field-goal-description] Goal Description    Field "field_goal_description".
[goal-completed:goal-userpoints]        User Points         Field "goal_userpoints".
[goal-completed:goal-img]               Goal Image          Field "goal_img". The following properties may be appended to the token: file (The image file.)
[goal-completed:field-goal-category]    Goal Category       Field "field_goal_category".
[goal-completed:field-goal-level]       Goal Level          Field "field_goal_level".
[goal-completed:field_goal_description] Goal Description    Long text field.
[goal-completed:goal_userpoints]        User Points         Integer field.
[goal-completed:goal_img]               Goal Image          Image field.
[goal-completed:field_goal_category]    Goal Category       List (text) field.
[goal-completed:field_goal_level]       Goal Level          List (text) field.


Obviously, these Replacement patterns have been made available via the Rules Action "Fetch entity by Id" that precedes it. And with that, you have anything available to "find and use the fields of the goal that triggered the goal completed event" (as in your question).

Step 3: Demo

Here is an example of a message shown to a user, after the user completes some goal (if the above view and rule is enabled):


  Details about completed goal:
  
  
  Goal ID: 11
  Goal Title: Citizen Patrol
  Goal Category: Moderation
  Goal Level: Bronze
  


Note: For way more details on using Views Rules, refer to ("Option 2" of) my answer to "How to use Rules to send an E-mail after saving a new comment, to users who checked a boolean in their profile?".

PS: this answer applies to D7 ...