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) »
  • Addres- update in API v3
Pages: [1]

Author Topic: Addres- update in API v3  (Read 619 times)

jimurl

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 0
  • CiviCRM version: 3.4.6
  • CMS version: drupal 6.22
  • MySQL version: 5+
  • PHP version: 5+
Addres- update in API v3
June 09, 2011, 10:41:52 am
Hi everybody,

I have been experimenting with the v3, and I like it. But I am having trouble using the API to "update"  the country_id field.  Setting a Country ID, I look this up with a sql based on the name entered by the user on a node form. At the bottom is the snippet of code I am using. It is called from within hook_nodeapi - so that's where all those $node->field_name-name-name comes from.

If I use the API Explorer, I can set the country_id just fine using 'update'. When using the 'create' operation instead of 'update' ,it works.  That operation sets the country_id value just fine. I have also tried to use 'create' even if I know that the address record exists- hoping to overwrite the value, still no luck.   Other fields- postal code, address, and state code- are set just fine. Even state code is appropriately set according to which country is picked.

I can imagine what Lobo will say- "Jump on in and fix 'er!" So I browsed /api/v3/Address.php  and found the create, delete and get functions, but not update; though it does appear that the create function also possibly handles update(?) I'm not sure about that.

I also looked into CRM/CORE/BAO/Address.php , but I lack the php skills to find where the problem may lie.

Should this be palced in the issue tracker?

Do I need to do the test for:  if (address exists) then use 'update', if not 'create' ?

Thanks foryour help, core folks.

Code: [Select]
           
$existing_loc = civicrm_api('Address','get', array( 'contact_id' => $civiid, 'is_primary' => TRUE, 'location_type_id' => 1, 'version' => 3) );
         
            $state = $node->field_state[0]['value'];
            $country = $node->field_country[0]['value'];
            $countryCode = db_result(db_query("SELECT id FROM civicrm_country WHERE name ='$country' or iso_code ='$country'"));
            $stateCode = db_result(db_query("SELECT id FROM civicrm_state_province WHERE (abbreviation = '$state' OR name = '$state') AND country_id = '$countryCode'")); 
            if( $stateCode == NULL) $stateCode == '';
            // get country ID
            $data_address = array(
              'country_id ' => $countryCode,
              'version' => 3,
              'contact_type' => 'Individual',
              'contact_id' => $civiid,
              'location_type_id' => 1,
              'street_address'   => $node->field_address1[0]['value'],
              'supplemental_address_1' => $node->field_address2[0]['value'],
              'city' => $node->field_city[0]['value'],
              'state_province_id' => $stateCode,
              'postal_code' => $node->field_zip[0]['value'],
              'is_primary' => TRUE
            );
            watchdog ('vsa', $countryCode." ---  ".$stateCode);

            if ($existing_loc['count']){
              $data_address['id'] = $existing_loc['id'];
              $ret_val = civicrm_api('Address','update',$data_address);
              if ($ret_val['is_error']) watchdog ('vsa', $ret_val['error_message']." --- ");
//var_dump($ret_val);
            }else{  // there was no address entered yet for that person
              $ret_val = civicrm_api('Address','create',$data_address);
              if ($ret_val['is_error']) watchdog ('vsa', $ret_val['error_message']." --- ");     
            }



xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Addres- update in API v3
June 09, 2011, 10:56:12 am
Hi,

Have you tried using create and setting id => the address id ? It should work, if it doesn't, try address_id.

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

jimurl

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 0
  • CiviCRM version: 3.4.6
  • CMS version: drupal 6.22
  • MySQL version: 5+
  • PHP version: 5+
Re: Addres- update in API v3
June 09, 2011, 11:49:57 am
Thanks Xavier,

I use a 'get' to find if there is an existing address for the contact; and to find the id of an that address for that contact.

I set it in this line:
 
Code: [Select]
...
            if ($existing_loc['count']){
              $data_address['id'] = $existing_loc['id'];
...

It updates things like street address and postal code fine- just not country.

Setting $data_address['address_id']   doesn't work- I get this error in my logs:
"Mandatory parameter missing id"

jim

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Addres- update in API v3
June 09, 2011, 12:55:56 pm
In general create + id should always work and when it doesn't it's a bug (unfortunately there are a few instances where there are still bugs).

UPDATE in api v3 just does a GET & Create on your behalf. It was put there because of the occasional bugs in create + id & sometimes it works better.
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

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

This forum was archived on 2017-11-26.