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 »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Can I use the REST API to modify address fields for a contact?
Pages: [1]

Author Topic: Can I use the REST API to modify address fields for a contact?  (Read 2222 times)

taiga

  • I’m new here
  • *
  • Posts: 14
  • Karma: 0
Can I use the REST API to modify address fields for a contact?
March 25, 2010, 12:26:29 am
I have a question related the REST API.
I'd like to "update" address related fields in a contact record using the REST API. I've successfully tested this call:

works! ---> civicrm/extern/rest.php?q=civicrm/contact/add&first_name=Scott&last_name=Thomas&contact_id=113&contact_type=Individual&key=<mysitekey>

I can update contact's last and first name, with no problem but when I get to updating email in the contact, it stops working, i.e. this URL:

doesn't work!! ---> civicrm/extern/rest.php?q=civicrm/contact/add&email=scott@test.com&first_name=Scott&last_name=Thomas&contact_id=113&contact_type=Individual&key=<mysitekey>

NOTE: What I mean by "doesn't work!! --->" is that the exit code indicates that it worked, but the update was not performed

The syntax outlined in the REST API documentation http://wiki.civicrm.org/confluence/display/CRMDOC/REST+interface says to use email[CRM:1][CRM:email] which does not work. What I can't find out from the documentation is what the 1 (number one) in email[CRM:1][CRM:email] stands for?:

doesn't work!! --->  civicrm/extern/rest.php?q=civicrm/contact/add&email[CRM:1][CRM:email]=scott@test.com&first_name=Scott&last_name=Thomas&contact_id=113&contact_type=Individual&key=mysitekey

doesn't work!! --->  civicrm/extern/rest.php?q=civicrm/contact/add&email[CRM:113][CRM:email]=scott@test.com&first_name=Scott&last_name=Thomas&contact_id=113&contact_type=Individual&key=mysitekey

doesn't work!! --->  civicrm/extern/rest.php?q=civicrm/contact/add&email[CRM:<email_id_here>][CRM:email]=scott@test.com&first_name=Scott&last_name=Thomas&contact_id=113&contact_type=Individual&key=<mysitekey>

Eventually I would like to be able to do this for an existing contact with address:

want this! --->  civicrm/extern/rest.php?q=civicrm/contact/add&first_name=Scott&last_name=Thomas&contact_id=113&city=Baltimore&postal_code=12345&contact_type=Individual&key=<mysitekey>

Could someome point me to the right parameters to pass, or tell me if the above address update will work using the REST API calls?

Many thanks in advance,

taiga
« Last Edit: March 25, 2010, 12:28:45 am by taiga »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Can I use the REST API to modify address fields for a contact?
March 25, 2010, 02:54:11 am
Hi,

I *think* that it's primary-email. Have a look at the name from the contact edit form, the names you have to use are most of the time the same.

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

taiga

  • I’m new here
  • *
  • Posts: 14
  • Karma: 0
Re: Can I use the REST API to modify address fields for a contact?
March 25, 2010, 04:13:47 pm
I've tried numerous variations including looking at the contact input form fields names etc., thus none of these work:

email, primary-email, email-Primary, city-Primary

Here's a result for a contact record gotten using the REST API call:

http://mydomain.com/sites/all/modules/civicrm/extern/rest.php?q=civicrm/contact/get&contact_id=18&key=SITE_KEY

<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Result>
    <contact_id>18</contact_id>
    <contact_type>Individual</contact_type>
    <sort_name>Alder, Norbert</sort_name>
    <display_name>Norbert Alder</display_name>
    <do_not_email>0</do_not_email>
    <do_not_phone>0</do_not_phone>
    <do_not_mail>0</do_not_mail>
    <do_not_sms>0</do_not_sms>
    <do_not_trade>0</do_not_trade>
    <is_opt_out>0</is_opt_out>
    <external_identifier>155</external_identifier>
    <preferred_mail_format>Both</preferred_mail_format>
    <first_name>Norbert</first_name>
    <last_name>Alder</last_name>
    <is_deceased>0</is_deceased>
    <current_employer>Jimmy Jimmy Inc.</current_employer>
    <address_id>16</address_id>
    <street_address>PO BOX 123</street_address>
    <city>My City</city>
    <postal_code>12345</postal_code>
    <geo_code_1>33.222111</geo_code_1>
    <geo_code_2>-74.321</geo_code_2>
    <state_province_id>1031</state_province_id>
    <state_province_name>New York</state_province_name>
    <state_province>NY</state_province>
    <country_id>1228</country_id>
    <country>United States</country>
    <phone_id>8</phone_id>
    <phone_type_id>1</phone_type_id>
    <phone>123-222-2222</phone>
    <worldregion_id>2</worldregion_id>
    <world_region>America South, Central, North and Caribbean</world_region>
</Result>
</ResultSet>


To be less confusing both from an access and retrieve point of view, the XML response returned should reflect the variable names that are used to get/update/add information. So for instance to update the above record, one ought to be able to use the attribute names returned by the REST API call to update those same values:

such as: street_address, current_employer, email

But if there are multiple choices e.g. in emails with different ids, the API IMHO should return a parent email attribute with the corresponding children attributes.

Could it be that the REST API currently can't handle creation of extra table entries, i.e. adding something to the address table, since a contact with an address will need an entry in the address table as well (see the variable address_id)?

If someone who's used the REST API in practice could comment on their experience it would help me greatly.

Thanks,

Taiga

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Can I use the REST API to modify address fields for a contact?
March 25, 2010, 04:34:43 pm
Hi,

Just checked on an existing custom code I use the location api to update the address/email, with this syntax (same variable names for the rest interface)

   $address = array( 1 => array( 'location_type_id'       => 1,
                                      'is_primary'             => 1,
                                      'country_id'             => $params["country"]
                                     )
                        );
    $location = array('version'    => '3.0',
                        'contact_id' => $params['contact_id'],
                        'phone'      => array($Phone),
                        'address'    => $address,
                        'email' => array ( 1=> array('is_primary' => 1,'email' =>$params['email']))
                        );

/*
    $params['location'] = array( 1 => array( 'location_type_id' => 2,
                                                'is_primary' => 1,
                                              'address' => array( 'country' => $params["country"])),
                     'phone' => array( 'phone' => $params['phone']),
                     'email' => array ( 1=> array('is_primary' => 1,'email' =>$params['email']))
                     );
*/
    $contact =&civicrm_contact_add( $params );
        require_once 'api/v2/Location.php';
        $newLocation =& civicrm_location_add($location);

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

taiga

  • I’m new here
  • *
  • Posts: 14
  • Karma: 0
Re: Can I use the REST API to modify address fields for a contact?
March 26, 2010, 01:45:55 am
Thanks Xavier,

I've gotten a step closer to getting it to work. So here is what I did based on your suggestion:

http://localhost/sites/all/modules/civicrm/extern/rest.php?q=civicrm/location/add&location_type=Home&contact_id=18&street_address=Washington Street&city=Springfield&key=MY_SITE_KEY

Which updates the record as expected. However when I try to add a different location_type to the existing contact record, it seems to add it on the backend and the REST API response looks good, but then a subsequent "get" call on the contact results in only one address showing up:

http://localhost/sites/all/modules/civicrm/extern/rest.php?q=civicrm/location/get&location_type=Home&contact_id=18&key=MY_SITE_KEY

When calling "get" on the location for a given contact_id it returns all locations as expected.

So the REST API works, but in a surprising way. Hope this post helps future users of the REST API.

Thanks again,

Taiga

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Can I use the REST API to modify address fields for a contact?
March 26, 2010, 03:57:09 am
Hi,

The get returns only the main location. Could you add that information in the API doc if this isn't specified ?

Tip: if you want to test the interface, you can use the ajax interface instead of the rest, the url is shorter and you don't have to carry around the the api key. I also prefer using it with json=1, more readable I think

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

taiga

  • I’m new here
  • *
  • Posts: 14
  • Karma: 0
Re: Can I use the REST API to modify address fields for a contact?
March 26, 2010, 06:01:29 pm
Hi Xavier,

I'd like to rewrite whole sections of the REST API section when I've figured out how to use it more concretely.

Thanks for the tip about the ajax interface, it works like a charm. Do you know if it keeps track of sessions after authentication, i.e. is there such a thing as session timeout for ajax calls, or will they work for the length of the period that I'm authenticated?

For the benefit of those reading this post, here's the URL to the documentation:

http://wiki.civicrm.org/confluence/display/CRMDOC/AJAX+Interface



xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Can I use the REST API to modify address fields for a contact?
March 27, 2010, 03:17:25 am
That would be great.

Was with Piotr and some developers from the Plone community working on an integration with python and plone using the REST interface.

Some things are definitely not clear about the keys(user/site), and the authentication in general should be redone.

The Ajax interface uses the same cookie than for your normal drupal session, so it will last as long as you are authenticated.

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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Can I use the REST API to modify address fields for a contact?
April 24, 2010, 06:05:08 am
Hi,

Do you feel more comfortable with the rest interface and have you started modifying the documentation ?

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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Can I use the REST API to modify address fields for a contact?

This forum was archived on 2017-11-26.