Answers.Body: <p>Since you are passing $entity_id to id in participant params without checking it to be not empty. If $entity_id is empty then new participant will be created else it will be updated. Make sure your id is set before calling to participant api if you want to update the participant</p>
  
  <p>Cheers</p>
  
  <p>Pradeep</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-24T15:05:02.017Z
Answers.Text: Hi @Pradeep , even when it has values it does not work. debugging it further we are using a batch operation, does that have any reason for the api to not work as when I add die() inside function custom_changes_actions_participant_status_attended_action() it changes the status, otherwise it does not
Answers.UserDisplayName: harshal
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-25T09:19:54.660Z
Answers.Text: That sounds different to your original question ... not changing status vs creating new participants.  Can you clarify in the question what you are seeing?
Answers.UserDisplayName: Aidan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-24T09:12:41.570Z
Answers.LastActivityDate: 2019-01-24T11:20:01.007Z
Answers.LastEditDate: 2019-01-24T11:20:01.007Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>Instead of updating the Participant,its creating new participant.I need to change participant status from <strong>registered</strong> to <strong>attended</strong> and vice versa.</p>
  
  <pre><code>civicrm_initialize();
    $result = civicrm_api3('Participant', 'create', [
    'id' =&gt; $entity_id,
    'contact_id' =&gt; $context['civicrm_participant']-&gt;contact_id,
    'event_id'   =&gt; $context['civicrm_participant']-&gt;event_id,
    'status_id' =&gt; "Registered",
  ]);
  </code></pre>
  
  <p>I am executing the above code in the hook_action_info() drupal 7 using views bulk operations &amp; custom entity module</p>
  
  <pre><code>function custom_changes_action_info() {
    return array(
       'custom_changes_actions_participant_status_attended_action' =&gt; array(
           'type' =&gt; 'civicrm_participant',
           'label' =&gt; t('Change Participant Status to Attended'),
           'configurable' =&gt; FALSE,
           'behavior' =&gt; array('changes_property'),
           'triggers' =&gt; array('any'),
       ),
        'custom_changes_actions_participant_status_registered_action' =&gt; array(
           'type' =&gt; 'civicrm_participant',
           'label' =&gt; t('Change Participant Status to Registered'),
           'configurable' =&gt; FALSE,
           'behavior' =&gt; array('changes_property'),
           'triggers' =&gt; array('any'),
       ),
    ); 
  }
  
  function custom_changes_actions_participant_status_attended_action($entity, $context = array()) {
  
  
    $entity_id = $entity-&gt;{$info['entity keys']['id']};
  
    civicrm_initialize();
   $result = civicrm_api3('Participant', 'create', [
    'id' =&gt; $entity_id,
    'contact_id' =&gt; $context['civicrm_participant']-&gt;contact_id,
    'event_id'   =&gt; $context['civicrm_participant']-&gt;event_id,
    'status_id' =&gt; "Attended",
  ]);
    //drupal_set_message($entity_id. 'entity_id');
   // $base_url = str_replace('_', '-', $context['entity_type']); // civicrm-participant
    //civicrm_entity_drupal_goto($base_url . '/' . $entity_id . '/edit');
  }
  </code></pre>
  
Tags: <drupal><api><participants>
Title: Unable to update participants using civicrm api?
ThreadText: # Unable to update participants using civicrm api?
  
  (Asked by: username_0 on 2019-01-24)
  
  Instead of updating the Participant,its creating new participant.I need to change participant status from **registered** to **attended** and vice versa.
  
  ```
  civicrm_initialize();
    $result = civicrm_api3('Participant', 'create', [
    'id' => $entity_id,
    'contact_id' => $context['civicrm_participant']->contact_id,
    'event_id'   => $context['civicrm_participant']->event_id,
    'status_id' => "Registered",
  ]);
  
  ```
  
  I am executing the above code in the hook\_action\_info() drupal 7 using views bulk operations & custom entity module
  
  ```
  function custom_changes_action_info() {
    return array(
       'custom_changes_actions_participant_status_attended_action' => array(
           'type' => 'civicrm_participant',
           'label' => t('Change Participant Status to Attended'),
           'configurable' => FALSE,
           'behavior' => array('changes_property'),
           'triggers' => array('any'),
       ),
        'custom_changes_actions_participant_status_registered_action' => array(
           'type' => 'civicrm_participant',
           'label' => t('Change Participant Status to Registered'),
           'configurable' => FALSE,
           'behavior' => array('changes_property'),
           'triggers' => array('any'),
       ),
    ); 
  }
  
  function custom_changes_actions_participant_status_attended_action($entity, $context = array()) {
  
    $entity_id = $entity->{$info['entity keys']['id']};
  
    civicrm_initialize();
   $result = civicrm_api3('Participant', 'create', [
    'id' => $entity_id,
    'contact_id' => $context['civicrm_participant']->contact_id,
    'event_id'   => $context['civicrm_participant']->event_id,
    'status_id' => "Attended",
  ]);
    //drupal_set_message($entity_id. 'entity_id');
   // $base_url = str_replace('_', '-', $context['entity_type']); // civicrm-participant
    //civicrm_entity_drupal_goto($base_url . '/' . $entity_id . '/edit');
  }
  
  ```
  
  **Tags:** drupal, api, participants
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-01-24. Score: 3
  
  Since you are passing $entity\_id to id in participant params without checking it to be not empty. If $entity\_id is empty then new participant will be created else it will be updated. Make sure your id is set before calling to participant api if you want to update the participant
  
  Cheers
  
  username_1
  
  > **Comments:**
  > **username_0**: Hi @username_1 , even when it has values it does not work. debugging it further we are using a batch operation, does that have any reason for the api to not work as when I add die() inside function custom_changes_actions_participant_status_attended_action() it changes the status, otherwise it does not  
  > **username_2**: That sounds different to your original question ... not changing status vs creating new participants.  Can you clarify in the question what you are seeing?  
  
  ---
  
  