CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviCase (Moderator: Dave Greenberg) »
  • Assign case when contacts are imported
Pages: [1]

Author Topic: Assign case when contacts are imported  (Read 2423 times)

Tiffani Chin

  • I post occasionally
  • **
  • Posts: 101
  • Karma: 2
  • CiviCRM version: 4.4.4
  • CMS version: Drupal 7.26
  • MySQL version: 5.5.30
  • PHP version: 5.3.17
Assign case when contacts are imported
February 07, 2012, 10:42:51 am
Hi All-

I'm new to CiviCase and trying to put together a system where we can track student progress in our nonprofit's college access program.

I've made pretty good progress in terms of setting up cases with timelines, activities, etc.

My question:  I will import about 800 students into the database (using a csv file).  All students will start with the same case and same standard timeline (we can add other timelines as needed for a particular student throughout the program).

The contact import function is great.  But, I have not been able to find a way to assign the case/timeline on import.  In fact, the only way I have found to assign cases/timelines is contact by contact (which will be very time consuming!).

Am I missing something?  (It's probably something obvious... but days later I can't find it.)

I really appreciate your help!
-tiffani

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Assign case when contacts are imported
February 07, 2012, 12:37:15 pm
You would need to develop an import script using the API. Are you familiar with the API and how are your PHP skills?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Tiffani Chin

  • I post occasionally
  • **
  • Posts: 101
  • Karma: 2
  • CiviCRM version: 4.4.4
  • CMS version: Drupal 7.26
  • MySQL version: 5.5.30
  • PHP version: 5.3.17
Re: Assign case when contacts are imported
February 07, 2012, 12:53:40 pm
Thanks for the response.  I guess I'm glad to know that I wasn't just looking in the wrong place.

I am familiar with the API.  My php skills are not great, but they are improving (out of necessity!). 

Can you recommend any piece of code (either from the core or somewhere here in the forum) that I might be able to use as a starting point/reference?

I'm a definite newbie, so any and all advice is much appreciated.

Thanks.

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Assign case when contacts are imported
February 07, 2012, 01:51:53 pm
There are some examples on the wiki, check for the code snippets. Do not hesitate to contact me either here or on the CiviCRM IRC channel on freenode (nick is erik_h). I am in the European timezone and off to bed now :-)
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Tiffani Chin

  • I post occasionally
  • **
  • Posts: 101
  • Karma: 2
  • CiviCRM version: 4.4.4
  • CMS version: Drupal 7.26
  • MySQL version: 5.5.30
  • PHP version: 5.3.17
Re: Assign case when contacts are imported
February 08, 2012, 01:53:44 pm
Thanks!  I'll check it out and see if I can make it happen. 

I'll probably hit you up for some more help once I get into it.  :)

Thanks again,
-tiffani

Tiffani Chin

  • I post occasionally
  • **
  • Posts: 101
  • Karma: 2
  • CiviCRM version: 4.4.4
  • CMS version: Drupal 7.26
  • MySQL version: 5.5.30
  • PHP version: 5.3.17
Re: Assign case when contacts are imported
February 09, 2012, 02:46:06 pm
Hi Erik,

Question for you (I'm in Los Angeles so I think the time difference makes this forum the best way to catch you).

I found the code for a hook that looked very similar to what I wanted (the tag_import) hook (http://wiki.civicrm.org/confluence/display/CRMDOC40/CiviCRM+hook+specification#CiviCRMhookspecification-hookcivicrmimport). 

I THINK, I understand what the code is doing, so I tried to adjust it to grab a value (case_import) from my import table and assign it to the civicrm_case table. 
I was able to create and enable the module in Drupal (so I know that at least something is being read), but the script isn't working.

I'm enough of a newbie that I don't even know how to tell if the script is running and not working, or not running at all.

The civicrm_tag table is also structured differently from the civicrm_case table.  I tried to make some adjustments based on my logic (just mental logic; very little computing logic going on here) but it's not working.

So, two questions: 

1) Is there a good way to see if a script (or hook) like this is functional? And, if it's even being picked up?

2) Does anything in the code jump out at you as obviously wrong?  The thing I'm guessing at the most is that I want to put the value from my import table into "case_type_id" -- this is counterintuitive.  I would think that I would want to put it in "case_id" but the case ids seem to be sequential (everytime I assign a case to a contact, it gets a new id number that is not at all connected to the contact_id).   

It's the case_type_id that seems to determine which case is assigned to that particular contact.  However, I don't know if changing that value will register with the system as "create a case."

Here's the code:

Code: [Select]
function civiVS_civicrm_import( $object, $usage, &$objectRef, $params ) {

    if ( $object != 'Contact' &&
         $usage  != 'process' ) {
        return;
    }

//during import, accept special case_import column for processing
$cType           = $objectRef->_contactType;
$dupeType        = $objectRef->_onDuplicate;
$contactID       = $params['contactID'];
$importID        = $params['importID'];
$importTempTable = $params['importTempTable'];
$fields          = $params['fields'];
   
require_once 'CRM/Core/DAO.php';
$sqlTags = "SELECT case_import
FROM $importTempTable
WHERE _id = $importID;";
$taglist = CRM_Core_DAO::singleValueQuery( $sqlTags );
   
if ( $caselist ) {
       
require_once 'api/v3/Case.php';
       
$keywords = array();
$keywords = explode( '|', $caselist );
       
foreach ( $keywords as $keyword ) {
$params = array( 'name' => $keyword );
           
//lookup tag; create new if nonexist
$case = civicrm_case_get($params);
if ( $case['is_error'] ) {
$case = civicrm_case_create($params);
$caseid = $case['case_type_id'];
} else {
$caseid = $case['case_type_id'];

Thanks again for your help and your patience.  I'm learning a ton but I know that I have a long way to go!
Best,
-tiffani

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: Assign case when contacts are imported
February 13, 2012, 12:20:30 pm
Tiffani - Since you are creating the same case type w/ the same information for each contact, the code for the import hook is quite a bit simpler. I've posted code below for a sample module, and the required .info file here. You should review the case parameters for subject, details, medium_id, location and modify them as needed (they'll be the same for all the cases). Due to a small bug in the current case api, you'll also need to supply a real contact ID for the creator_id parameter. This should be the ID of YOUR contact record in CiviCRM (or of the person who is doing the import) - replace the 102 value in the module.

I have only tested this with a small import (4 contacts) - so I'm not sure what the performance will be like with a larger set of data. In any case, you should test this w/ just a few rows first - and always remember to use the "Add imported contacts to a group" feature on the Preview step so you can easily see the contacts you've just imported (and delete them if necessary).

createCaseImport.module
Code: [Select]
<?php


/*import hook allows you to interact with the import data post process
 *create/assigns case to the imported contact record using a predefined case type and case details 
 */

function createCaseImport_civicrm_import( $object, $usage, &$objectRef, $params ) {

    if ( 
$object != 'Contact' &&
         
$usage  != 'process' ) {
        return;
    }

    
$caseParams['contact_id'] = $params['contactID'];
    
$caseParams['case_type_id'] = 1;
    
$caseParams['version'] = 3;
    
$caseParams['creator_id'] = 102;
    
$caseParams['subject'] = 'Student Tracking';
    
$caseParams['details'] = 'Case details here.';
    
$caseParams['location'] = 'Office';
    
$caseParams['duration'] = '30';
    
$caseParams['medium_id'] = 2; // 2 for phone, 1 for in person, 3 for email, 5 for postal mail


require_once 'api/v3/Case.php';
$case = civicrm_api3_case_create($caseParams);
}     

?>


createCaseImport.info
Code: [Select]
name = Create Case CiviCRM Import
description = Creates a new case for each imported individual contact.
core = 7.x
package = CiviCRM
dependencies[] = civicrm
Protect your investment in CiviCRM by  becoming a Member!

Tiffani Chin

  • I post occasionally
  • **
  • Posts: 101
  • Karma: 2
  • CiviCRM version: 4.4.4
  • CMS version: Drupal 7.26
  • MySQL version: 5.5.30
  • PHP version: 5.3.17
Re: Assign case when contacts are imported
February 13, 2012, 05:23:19 pm
Hi Dave,

Thank you for the help.  That's definitely simpler than what I was trying to do!

Unfortunately, it doesn't seem to be working quite yet.  I'm back to one of my original newbie questions, which is: is there anyway to tell if a module is running?  The module shows up on the Drupal Module page and is tagged as "enabled."  But, beyond that, I'm not even sure where to look to see what's going on.

The import function works just fine (as ever) but the new contacts do not have the case attached -- nor do I get any kind of error message (would I?).

Any thoughts?

Thanks again,
-tiffani

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Assign case when contacts are imported
February 13, 2012, 11:54:28 pm
Hi Tiffani,

the hook looks fine, pity it does not work for you. The set up of the CiviCase tables is pretty involved, with civicrm_case holding the case details, civicrm_case_contact linking the contact to the case, and civicrm_case_activity linking the related activities to the case.

If you want to check if the module is called anyway, you can always add a little debug statement in the function:
Code: [Select]
CRM_Core_Error::debug("your text", $the_var_to_be_debugged);
exit();
This wil at least tell you if your hook is called in the first place, and check if it creates the desired result. You might want to check the return $case from the api call for example to see what information that gives you? You could also try your specific API call with the API explorer.....
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: Assign case when contacts are imported
February 14, 2012, 01:02:00 pm
Tiffani - I had originally tested that module in my local 4.1 sandbox (and it worked). However, I just tried it in my 3.4 local sandbox (same codebase as 4.0), and it failed silently (same results  as you're getting). :-(

Since you're just starting out, might make sense to jump right into 4.1. The stable release is scheduled to happen this week and you can upgrade your 4.0 site with the upgrade instructions:

http://wiki.civicrm.org/confluence/display/CRMDOC41/Upgrade+Drupal+Sites+to+4.1+-+Drupal+7
Protect your investment in CiviCRM by  becoming a Member!

Tiffani Chin

  • I post occasionally
  • **
  • Posts: 101
  • Karma: 2
  • CiviCRM version: 4.4.4
  • CMS version: Drupal 7.26
  • MySQL version: 5.5.30
  • PHP version: 5.3.17
Re: Assign case when contacts are imported
February 15, 2012, 09:57:47 am
Erik-

Thanks for the debug code -- I assume that works for any kind of function?  Good to know.  Thank you.

Dave-

Thanks for the heads-up about the upgrade.  I'm hoping to move at least our basic CiviCRM functionality to the live site this week, so I'll go ahead and upgrade so we got the most current version before we do so.   I'll keep you posted on how the module works!

Thanks again for all of your help,
-tiffani

Tiffani Chin

  • I post occasionally
  • **
  • Posts: 101
  • Karma: 2
  • CiviCRM version: 4.4.4
  • CMS version: Drupal 7.26
  • MySQL version: 5.5.30
  • PHP version: 5.3.17
Re: Assign case when contacts are imported
February 15, 2012, 01:27:36 pm
Ok, 4.1 upgrade complete.  Everything seems to be up and running (amazingly easy!). 

But, the little import/case module is still not running (still failing silently). 

Erik, I tried implement your bug test and (so sorry for the very newbie implementation, wasn't sure where to put it!).  I'm going to repost the code so that if I did anything obviously wrong, you guys will catch it.

Code: [Select]
<?php


/*import hook allows you to interact with the import data post process
 *create/assigns case to the imported contact record using a predefined case type and case details 
 */

function createCaseImport_civicrm_import( $object, $usage, &$objectRef, $params ) {


    if ( 
$object != 'Contact' &&
         
$usage  != 'process' ) {
        return;
    }

    
$caseParams['contact_id'] = $params['contactID'];
    
$caseParams['case_type_id'] = 1;
    
$caseParams['version'] = 3;
    
$caseParams['creator_id'] = 1;
    
$caseParams['subject'] = 'Student Tracking';
    
$caseParams['details'] = 'Starting V-SOURCE case';
    
$caseParams['location'] = 'Office';
    
$caseParams['duration'] = '0';
    
$caseParams['medium_id'] = 2; // 2 for phone, 1 for in person, 3 for email, 5 for postal mail


require_once 'api/v3/Case.php';
$case = civicrm_api3_case_create($caseParams);

CRM_Core_Error::debug("Import running but not working :)", $case);
exit();
}     

?>

Just so you know the details of what I'm doing (in case anything else jumps out as the obvious reason for failure):
-Module is enabled.
-CiviCRM is updated and seems to be working well (I can assign cases manually).
-I'm importing 4 contacts (rows), from a csv file.
-Each row has just 3 cols (first, last, email) as a test.
-I'm attaching contacts to a group upon import.
-I'm asking Civi to skip duplicates.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Assign case when contacts are imported
February 15, 2012, 03:13:48 pm

hey tiffani:

can we get ssh access to your server. You might need to ask your hosting provider for that info. Once you have the info, can you please get on IRC.

some details here:

http://wiki.civicrm.org/confluence/display/CRMDOC41/SSH+Access+Requirements

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Assign case when contacts are imported
February 15, 2012, 11:08:57 pm
Hi Tiffani,

I would put a debug in the beginning of the function as well, which tells me the value of the params coming into the function. That will tell me the function itself is being called even if something goes wrong in the middle of it. But do try and get ssh access for dlobo so he can help you on IRC?


Code: [Select]
<?php


/*import hook allows you to interact with the import data post process
 *create/assigns case to the imported contact record using a predefined case type and case details 
 */

function createCaseImport_civicrm_import( $object, $usage, &$objectRef, $params ) {
   
CRM_Core_Error::debug("incoming params", $params);


    if ( 
$object != 'Contact' &&
         
$usage  != 'process' ) {
        return;
    }

    
$caseParams['contact_id'] = $params['contactID'];
    
$caseParams['case_type_id'] = 1;
    
$caseParams['version'] = 3;
    
$caseParams['creator_id'] = 1;
    
$caseParams['subject'] = 'Student Tracking';
    
$caseParams['details'] = 'Starting V-SOURCE case';
    
$caseParams['location'] = 'Office';
    
$caseParams['duration'] = '0';
    
$caseParams['medium_id'] = 2; // 2 for phone, 1 for in person, 3 for email, 5 for postal mail


   
require_once 'api/v3/Case.php';
   
$case = civicrm_api3_case_create($caseParams);
   
   
CRM_Core_Error::debug("Import running but not working :)", $case);
   exit();
}     

?>
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Tiffani Chin

  • I post occasionally
  • **
  • Posts: 101
  • Karma: 2
  • CiviCRM version: 4.4.4
  • CMS version: Drupal 7.26
  • MySQL version: 5.5.30
  • PHP version: 5.3.17
Re: Assign case when contacts are imported
February 24, 2012, 10:45:58 am
Hi Lobo and Erik,

Thanks for the responses -- and sorry for my very late response.  I am the ED of my organization (defacto tech person, but that's not saying much) and the admin/content parts of the program took me over for a while.

Going to try the debug and see if that helps me.

At the moment I'm trying to work with CiviCRM/Case on my dev site on my hard drive.  But, I'm hoping to get it live next week and I'll work on ssh access.

Thanks again -- and I'll be back.
-tiffani

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviCase (Moderator: Dave Greenberg) »
  • Assign case when contacts are imported

This forum was archived on 2017-11-26.