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) »
  • Location API Update does not work
Pages: [1] 2

Author Topic: Location API Update does not work  (Read 4081 times)

chirayu

  • Guest
Location API Update does not work
August 19, 2010, 05:03:22 am
Hi All

I am trying to update location (email, phone and address) for a particular but get an error.
Code: [Select]
 $home_phone = array( 'phone'           => $line[$PHONE_COLUMN],
                       'location_type'   => 'Home'
                     );
  $mobile = array( 'phone'         => $line[$MOBILE_COLUMN],
                   'location_type'   => 'Main'
                 );
  $phones = array( $home_phone, $mobile);

  $email_first = array( 'email'            => $line[$EMAIL_COLUMN],
                        'location_type_id' => 2,
                        'is_primary'       => true
                      );
  $email_second = array( 'email'            => $line[$CC_EMAIL_COLUMN],
                         'location_type_id' => 2
                       );
  $emails = array($email_first, $email_second);

  $street_address;
  if ($line[$ADDRESS_COLUMN] != null) {
    $street_address = getAddress($line[$ADDRESS_COLUMN]);
  }
  $address = array( 1 => array(
                                'city'           => $line[$CITY_COLUMN],
                                'postal_code'    => $line[$POSTCODE_COLUMN],
                                'street_address' => $street_address,
                                'country'        => $line[$COUNTRY_COLUMN],
                                'is_primary'     => true
                              )
                  );

  $params = array( 'version'    => '3.2',
                   'contact_id' => $contact_id,
                   'address'    => $address,
                   'email'      => $emails,
                   'phone'      => $phones,
                 );
  $location =& civicrm_location_update( $params);

$line is an array containing data about contacts.

The error message that I get is:
Code: [Select]
missing or invalid location_type_id

I have tried many different things but get some error related to location type. If I put in a 'location_type_id' key in $params then I get a different error saying that the id is invalid.

Please help!

Chirayu
« Last Edit: August 19, 2010, 05:05:40 am by chirayu »

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Location API Update does not work
August 19, 2010, 05:59:40 am
Quote
$address = array( 1 => array(
                                'city'           => $line[$CITY_COLUMN],
                                'postal_code'    => $line[$POSTCODE_COLUMN],
                                'street_address' => $street_address,
                                'country'        => $line[$COUNTRY_COLUMN],
                                'is_primary'     => true
                              )
                  );

Here there is no location type. Check http://wiki.civicrm.org/confluence/display/CRMDOC32/Location+APIs

HTh
Kurund
Found this reply helpful? Support CiviCRM

chirayu

  • Guest
Re: Location API Update does not work
August 19, 2010, 06:19:23 am
Did that, it still generates the same error message. I think the missing location_type_id is in $params.

But if I add, location_type_id to $params:
Code: [Select]
 $params = array( 'version'    => '3.2',
                   'contact_id' => $contact_id,
                   'address'    => $address,
                   'email'      => $emails,
                   'phone'      => $phones,
                   'location_type_id' => 1
                 );

then the error messages changes to:
Code: [Select]
Invalid Location Type(s) : Home

« Last Edit: August 19, 2010, 06:24:26 am by chirayu »

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: Location API Update does not work
August 19, 2010, 06:45:14 am
There should be a location type in the array address, phones and email.... I am just testing myself with the following code:


Code: [Select]
$phones = array(array(
    "phone"         =>  "012-5656561",
    "phone_type"    =>  "Geheim",
    "primary"       =>  "1"),
    array(
    "phone"         =>  "06-77545454",
    "phone_type"    =>  "Mobiel",
    "location_type" =>  "Werk"),
    array(
    "phone"         =>  "012-9988556"));

$email = array(array(
    "email"         =>  "stijn@hommelvanderelst.nl",
    "location_type" =>  "Overig",
    "primary"       =>  "1"),
    array(
    "email"         =>  "stijnepijn@google.com"));

$params = array(
    "contact_id"    =>  281,
    "location_type" =>  "Thuis",
    "street_name"   =>  "Pompoenenplantsoen",
    "street_number" =>  "123",
    "street_suffix" =>  "A derde bel",
    "postcode"      =>  "2121 AG",
    "city"          =>  "'s-Gravenhage",
    "phone"         =>  $phones,
    "email"         =>  $email);

Also, the location types should be valid ones in CiviCRM, check table civicirm_location_type if you are not sure!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

chirayu

  • Guest
Re: Location API Update does not work
August 19, 2010, 07:16:50 am
Thanks Erik. I think I have tried that before, but it does not help.  :(

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: Location API Update does not work
August 19, 2010, 07:20:40 am
Strange......I have just used this for a migration about 2 hours ago. It worked fine!?!? Have you checked your tables? Are you sure there is actually a location_type Home and someone has not accidently changed that?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

chirayu

  • Guest
Re: Location API Update does not work
August 19, 2010, 08:04:55 am
SOLVED,

I have checked my tables and I have got the standard default location types (home, main, work, other...)

The solution is to add location_type_id for each phone, address and email. Adding location_type does not work for civicrm_location_update. However, civicrm_location_add works if you have location_type. So this code below, is the code that works:

Code: [Select]
$home_phone = array( 'phone'           => $line[$PHONE_COLUMN],
                       'phone_type_id'   => 1,
                       'location_type_id'   => 1
                     );
  $mobile = array( 'phone'         => $line[$MOBILE_COLUMN],
                   'phone_type_id'   => 2,
                   'location_type_id' => 3
                 );
  $phones = array( $home_phone, $mobile);

  $email_first = array( 'email'            => $line[$EMAIL_COLUMN],
                        'location_type_id' => '3',
                        'is_primary'       => True
                      );
  $email_second = array( 'email'            => $line[$CC_EMAIL_COLUMN],
                         'location_type_id' => '4'
                       );
  $emails = array($email_first, $email_second);

  $street_address;
  if ($line[$ADDRESS_COLUMN] != null) {
    $street_address = getAddress($line[$ADDRESS_COLUMN]);
  }
  $address = array( 1 => array( 'location_type_id'  => 1,
                                'city'           => $line[$CITY_COLUMN],
                                'postal_code'    => $line[$POSTCODE_COLUMN],
                                'street_address' => $street_address,
                                'country'        => $line[$COUNTRY_COLUMN],
                                'is_primary'     => True
                              )
                  );

  $params = array( 'version'    => '3.0',
                   'contact_id' => $contact_id,
                   'address'    => $address,
                   'email'      => $emails,
                   'phone'      => $phones,
                 );

 
  $location =& civicrm_location_update( $params);
  //  or
  //$location =& civicrm_location_add( $params);

NOTE that location_type_id is specified for each of the phones, emails, addresses. This is what corrected my error.

Hope it saves someone some time!

chirayu

  • Guest
Re: Location API Update does not work
August 19, 2010, 08:05:20 am
Also Thanks to Erik for actively giving suggestions

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Location API Update does not work
August 19, 2010, 08:10:27 am
Yes, the location api is a bit hairy.

Now that you have a fresh view on what was weird, could you suggest improvements ? eg. better error messages, or being able to set parameter X when you call api Y, or better documentation...

X+

P.S. The documentation is on a wiki, if you find an information missing or incorrect, please update it.
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

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: Location API Update does not work
August 19, 2010, 09:08:55 am
I am in the middle of testing some API's and will update the documentation next week after my first migrations.
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

chirayu

  • Guest
Re: Location API Update does not work
August 19, 2010, 09:09:42 am
Hi

Already updated the wiki.

The main problem was with civicrm_location_update($params).

When you pass in phones, emails and addresses to the function, each one of them should have a 'location_type_id' (can be seen above from my correct code). Also, the $params does not need to have a 'location_type_id' itself.

As I said, I have updated civicrm_location_update in:
http://wiki.civicrm.org/confluence/display/CRMDOC32/Location+APIs

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: Location API Update does not work
August 19, 2010, 09:23:40 am
Thanks, I did the same in my code.  ;)
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

emilyf

  • Ask me questions
  • ****
  • Posts: 696
  • Karma: 54
  • CiviCRM version: 2.x - 4.x
  • CMS version: Drupal 5, 6, 7
Re: Location API Update does not work
August 26, 2010, 08:46:43 am
I am trying to do this same thing and have followed what you've said chirayu but it's not working unless i run civicrm_location_add. However, if I run it as civicrm_location_add, then if a person already has info in the phone field it will add yet another phone every time instead of updating. I would much rather get it to work with civicrm_location_update, but nothing happens when I run that. Here's my code, which I believe follows the same code you're using (I am on 3.1):

Code: [Select]
<?php

 $Phone 
= array(
          
'is_primary' => 0,
          
'phone' => $myphone,
          
'phone_type_id' => 1,
          
'location_type_id' => 6,
      );

 
$address = array(
        
1 => array(
            
'location_type_id' => 6,
            
'is_primary' => 0,
            
'city' => $city,
            
'state_province' => $state,
            
'postal_code' => $postal_code,
            
'street_address' => $street1,
            
'supplemental_address_1' => $street2,
            
'country' => $countries[0]['country_iso_code_2']
        )
    );

    
// Put phone in an array and then add to $params array along with address
    
$phones = array( $Phone );
    
$params = array(
        
'version' => '3.0',
        
'contact_id' => $cid,
        
'phone' => $phones,
        
'address' => $address
    
);


    
$newLocation =& civicrm_location_update( $params ); // works if i run civicrm_location_add but then i will get duplicate phones if a contact already has a phone of this type
?>



Any help? Thanks!

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Location API Update does not work
August 26, 2010, 10:23:00 am
Hi all,

Obviously, the location API is confusing.

Those who have struggled with it recently, do you have an idea how to improve it?

I don't see as an issue having to set the location type, but may be I'm missing something obvious ?

Beside, it might simply be a matter of throwing a proper error message instead of silently ignoring the request ?

Please share where you got lost, and if you have an idea (or even better, a patch) on how to either avoid these pitfall or come up with a proper error to clarify what's wrong, please share.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

chirayu

  • Guest
Re: Location API Update does not work
September 02, 2010, 03:43:09 am
Quote from: emilyf on August 26, 2010, 08:46:43 am
I am trying to do this same thing and have followed what you've said chirayu but it's not working unless i run civicrm_location_add. However, if I run it as civicrm_location_add, then if a person already has info in the phone field it will add yet another phone every time instead of updating. I would much rather get it to work with civicrm_location_update, but nothing happens when I run that. Here's my code, which I believe follows the same code you're using (I am on 3.1):

Code: [Select]
<?php

 $Phone 
= array(
          
'is_primary' => 0,
          
'phone' => $myphone,
          
'phone_type_id' => 1,
          
'location_type_id' => 6,
      );

 
$address = array(
        
1 => array(
            
'location_type_id' => 6,
            
'is_primary' => 0,
            
'city' => $city,
            
'state_province' => $state,
            
'postal_code' => $postal_code,
            
'street_address' => $street1,
            
'supplemental_address_1' => $street2,
            
'country' => $countries[0]['country_iso_code_2']
        )
    );

    
// Put phone in an array and then add to $params array along with address
    
$phones = array( $Phone );
    
$params = array(
        
'version' => '3.0',
        
'contact_id' => $cid,
        
'phone' => $phones,
        
'address' => $address
    
);


    
$newLocation =& civicrm_location_update( $params ); // works if i run civicrm_location_add but then i will get duplicate phones if a contact already has a phone of this type
?>



Any help? Thanks!

That code should work on my system. I just ran a test to check that. Only thing is that I cannot add 2 addresses for the same contact using location update.

Could you show us what the error message is ?
Try changing the version field to 3.1
Also you could try checking for location types in your civicrm.

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Location API Update does not work

This forum was archived on 2017-11-26.