Answers.Body: <p>You can base it on the code found in CRM/Case/Form/CaseView.php. You'll need to adjust references to $this, etc.</p>
  
  <pre><code>  $session            = CRM_Core_Session::singleton();
    $this-&gt;_uid         = $session-&gt;get('userID');
    $xmlProcessor       = new CRM_Case_XMLProcessor_Process();
    $xmlProcessorParams = array(
      'clientID' =&gt; $this-&gt;_contactID,
      'creatorID' =&gt; $this-&gt;_uid,
      'standardTimeline' =&gt; 0,
      'activity_date_time' =&gt; date('YmdHis'),
      'caseID' =&gt; $this-&gt;_caseID,
      'caseType' =&gt; $this-&gt;_caseType,
      'activitySetName' =&gt; $params['timeline_id'],
    );
    $xmlProcessor-&gt;run($this-&gt;_caseType, $xmlProcessorParams);
    $reports = $xmlProcessor-&gt;get($this-&gt;_caseType, 'ActivitySets');
  
    CRM_Core_Session::setStatus(ts('Activities from the %1 activity set have been added to this case.',
        array(1 =&gt; $reports[$params['timeline_id']])
    ), ts('Done'), 'success');
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-09T16:19:32.030Z
Answers.LastActivityDate: 2015-10-09T16:19:32.030Z
Answers.UserDisplayName: Demerit
Answers.Body: <p>That's just what I needed, thanks Demerit!  For reference for anyone trying to do something similar to what I'm doing, here's code that works for me.  I'm starting with:</p>
  
  <p>$rid = the ID of a recipient of an inbound email</p>
  
  <p>$mid = ID of the case coordinator</p>
  
  <p>...then fetching the person's case, changing the case status, and adding the timeline:</p>
  
  <pre><code>   $caseresult = civicrm_api3('Case', 'get', array(
      'sequential' =&gt; 1,
      'return' =&gt; "id, case_type_id",
      'contact_id' =&gt; $rid, 
      'status_id' =&gt; "Open",
      'is_deleted' =&gt; 0,
      'options' =&gt; array('limit' =&gt; 1),
    ));
    if ( $caseresult['count'] == 1) { //case found, so proceed
      $case_id = $caseresult['values'][0]['id'];
      $case_type_id = $caseresult['values'][0]['case_type_id'];
      //update case status
      civicrm_api3('Case', 'create', array(
        'sequential' =&gt; 1,
        'contact_id' =&gt; $rid,
        'status_id' =&gt; 3,
        'id' =&gt; $case_id,
      ));
      $xmlProcessor = new CRM_Case_XMLProcessor_Process();
      $xmlProcessorParams = array(
        'clientID' =&gt; $rid,
        'creatorID' =&gt; $mid,
        'standardTimeline' =&gt; 0,
        'activity_date_time' =&gt; date('YmdHis'),
        'caseID' =&gt; $case_id,
        'caseType' =&gt; 'member_application',
        'activitySetName' =&gt; 'timeline_1',
      );
      $xmlProcessor-&gt;run('member_application', $xmlProcessorParams);
    }
  </code></pre>
  
  <p>Note that 'member_application' and 'timeline_1' are hard coded.  I found the right values to use by temporarily modifying CRM/Case/Form/CaseView.php and adding the Timeline I want to case manually, then looking at the output in my debug.txt file:</p>
  
  <pre><code>$xmlProcessor-&gt;run($this-&gt;_caseType, $xmlProcessorParams);
  $file = fopen("/tmp/debug.txt", "a");
  fwrite($file, print_r($xmlProcessorParams, TRUE));
  fclose($file);
  $reports = $xmlProcessor-&gt;get($this-&gt;_caseType, 'ActivitySets');
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-09T23:59:17.817Z
Answers.LastActivityDate: 2015-10-09T23:59:17.817Z
Answers.UserDisplayName: Norris
Body: <p>Is it possible to Add a predefined Timeline to a Case from a hook, via API or otherwise?  That is, duplicate the effect of a user clicking the "Add Timeline" button in a case?</p>
  
Tags: <civicase>
Title: Can I add a timeline to a Case programatically?
ThreadText: # Can I add a timeline to a Case programatically?
  
  (Asked by: username_0 on 2015-10-09)
  
  Is it possible to Add a predefined Timeline to a Case from a hook, via API or otherwise? That is, duplicate the effect of a user clicking the "Add Timeline" button in a case?
  
  **Tags:** civicase
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-10-09. Score: 2 | Accepted answer
  
  You can base it on the code found in CRM/Case/Form/CaseView.php. You'll need to adjust references to $this, etc.
    $session            = CRM_Core_Session::singleton();
    $this->_uid         = $session->get('userID');
    $xmlProcessor       = new CRM_Case_XMLProcessor_Process();
    $xmlProcessorParams = array(
      'clientID' => $this->_contactID,
      'creatorID' => $this->_uid,
      'standardTimeline' => 0,
      'activity_date_time' => date('YmdHis'),
      'caseID' => $this->_caseID,
      'caseType' => $this->_caseType,
      'activitySetName' => $params['timeline_id'],
    );
    $xmlProcessor->run($this->_caseType, $xmlProcessorParams);
    $reports = $xmlProcessor->get($this->_caseType, 'ActivitySets');
  
    CRM_Core_Session::setStatus(ts('Activities from the %1 activity set have been added to this case.',
        array(1 => $reports[$params['timeline_id']])
    ), ts('Done'), 'success');
  
  
  ---
  
  ### Answer by username_0 on 2015-10-09. Score: 1
  
  That's just what I needed, thanks username_1!  For reference for anyone trying to do something similar to what I'm doing, here's code that works for me.  I'm starting with:
  $rid = the ID of a recipient of an inbound email
  $mid = ID of the case coordinator
  ...then fetching the person's case, changing the case status, and adding the timeline:
     $caseresult = civicrm_api3('Case', 'get', array(
      'sequential' => 1,
      'return' => "id, case_type_id",
      'contact_id' => $rid, 
      'status_id' => "Open",
      'is_deleted' => 0,
      'options' => array('limit' => 1),
    ));
    if ( $caseresult['count'] == 1) { //case found, so proceed
      $case_id = $caseresult['values'][0]['id'];
      $case_type_id = $caseresult['values'][0]['case_type_id'];
      //update case status
      civicrm_api3('Case', 'create', array(
        'sequential' => 1,
        'contact_id' => $rid,
        'status_id' => 3,
        'id' => $case_id,
      ));
      $xmlProcessor = new CRM_Case_XMLProcessor_Process();
      $xmlProcessorParams = array(
        'clientID' => $rid,
        'creatorID' => $mid,
        'standardTimeline' => 0,
        'activity_date_time' => date('YmdHis'),
        'caseID' => $case_id,
        'caseType' => 'member_application',
        'activitySetName' => 'timeline_1',
      );
      $xmlProcessor->run('member_application', $xmlProcessorParams);
    }
  
  Note that 'member_application' and 'timeline_1' are hard coded.  I found the right values to use by temporarily modifying CRM/Case/Form/CaseView.php and adding the Timeline I want to case manually, then looking at the output in my debug.txt file:
  $xmlProcessor->run($this->_caseType, $xmlProcessorParams);
  $file = fopen("/tmp/debug.txt", "a");
  fwrite($file, print_r($xmlProcessorParams, TRUE));
  fclose($file);
  $reports = $xmlProcessor->get($this->_caseType, 'ActivitySets');
  
  
  ---
  
  