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?
2 Answers
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');
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:
$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');