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) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Update enployes in contact using API
Pages: [1]

Author Topic: Update enployes in contact using API  (Read 1721 times)

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
Update enployes in contact using API
March 16, 2009, 04:49:03 am
I am using API's to load data into CiviCRM. This works fine, but I have one problem: how do I add an employer to a contact with the contact_add API? I can update the employer_id field, but that does not show in CiviCRM because the organization_name is still empty. However, if I add organization_name in the API, it updates the name of the contact and not the name of the employer  :(
Can you help me?
Regards
Erik
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

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: Update enployes in contact using API
March 16, 2009, 10:32:36 am

a small confession :)

current employer is a nicely crafted hack onto the data model and utilizes a few fields and conventions for efficiency (i.e. we cache organization_name in the individual's record so to avoid another query)

i think your best would be to write a small function and write to the DB directly which update the organization_name, employer_id for a specific contact_id

if you get on IRC (http://embed.mibbit.com/?server=irc.freenode.net&channel=%23civicrm&forcePrompt=true), we can help you out. It will be a small 5 line function :)

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: Update enployes in contact using API
March 16, 2009, 10:45:15 am
Hi Donald,
that is exactly what I have done  ;) I just have to add the bit that takes care of the new multi-language support.
What is the best way or place to post the code so that others can benefit whenever they encounter the same problem?
regards
Erik
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

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: Update enployes in contact using API
March 16, 2009, 01:12:12 pm

can you cut-n-paste the code here and also add to this page: http://wiki.civicrm.org/confluence/display/CRMDOC/Using+CiviCRM+APIs+-+Code+Snippets

thanx

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: Update enployes in contact using API
March 17, 2009, 12:15:57 am
I have developed a function to update the employer data in the file civicrm_contact. In this specific example, I have a large import file containing data from a legacy system in a separate MySQL table. I take 2 steps to create the contact data in CiviCRM, because I need to build relationships so I have to have the civicrm contact_id. I store this in the source table during my first read. When I build the relationships, I also update the employer data in the contact file:

   // Connect to the database to update employer data in contact table:
   $udbc = new mysqli($host, $user, $password, $databse);

   // Only if any relations present in source table:
   if (!empty($relatie_met1) and !empty($relatietype1)) {
   
      // Set relationshiptype:
      $relationship_type_id = NULL;
      switch ($relatietype1) {
         case "Employee":
            $relationship_type_id = 4;
            break;
         case "Household":
            $relationship_type_id = 7;
            break;
         case "Contact":
            $relationship_type_id = 8;
            break;
         case "Social worker":
            $relationship_type_id = 9;
            break;
         case "Projectpartner":
            $relationship_type_id = 12;
            break;
         case "Child":
            $relationship_type_id = 13;
            break;
         case "Relative":
            $relationship_type_id = 14;
            break;
         case "Rents with institution":
            $relationship_type_id = 15;
            break;
         case "Looking for house":
            $relationship_type_id = 16;
            break;
      } // ENDswitch($relatietype1)
      
      // Retrieve id of relation_b:
      $get_id = "SELECT civicrm_id, name FROM demo1 WHERE id = $relatie_met1";
      $r_id = mysqli_query($dbc, $get_id);
      if (mysqli_num_rows($r_id) == 1) {
         list($relatie_id_b, $employer) = mysqli_fetch_array($r_id, MYSQLI_NUM);
      }   
 
      $today= date('Ymd');
   
      // Parameter array forCiviCRM API to add relationship:   
      $params = array(
                    'contact_id_a'             => $civicrm_id,
                    'contact_id_b'             => $relatie_id_b,
                    'relationship_type_id'  => $relationship_type_id,
             'start_date'          => $today,
                    'is_active'            => 1
                    );

      // CallCiviCRM API to add relationship:
       $relatie = & civicrm_relationship_create( $params );
       if( civicrm_error( $relatie)) {
         $errors[$i] = "Error building relationship with : ".$civicrm_id." to contact : "
            .$relatie_id_b." : ". $relatie['error_message'];
         $i++;
   }
      
   // If employer relation than update civicrm_contact table with employer data:      
        if ($relationship_type_id == 4) {

         // Build and execute update query:
         $update = "UPDATE civicrm_contact SET organization_name_nl_NL = '$employer', employer_id = '$relatie_id_b' WHERE
               id = $civicrm_id";
         $ru = $udbc->query($update);
         }
      } // END if ($relationship_type == 4)         
   } // END if (!empty($relatie_met1) and !empty($relatietype1))

Any questions or remarks, let me know!

Erik

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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Update enployes in contact using API

This forum was archived on 2017-11-26.