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) »
  • Does "address","replace" work?
Pages: [1]

Author Topic: Does "address","replace" work?  (Read 1104 times)

nickholden

  • I post occasionally
  • **
  • Posts: 111
  • Karma: 1
  • CiviCRM version: 4.4.1
  • CMS version: Drupal 7
  • MySQL version: 5.5.32
  • PHP version: 5.4
Does "address","replace" work?
September 30, 2013, 08:31:39 am
I'm trying to use the API to add some data to a test site, where the contacts being supplied to the API will either need creating (if they don't exist) or updating (if they do). I understood that the correct process here was to use the API "Contact","create" call with a parameter for the contact's ID if it is found. That works OK, updating the contact's name, for instance, and creating the necessary additional records, until it comes to address data.

If I chain an "address","create" call into the "contact","create" call, the API creates a new address, even if it is a duplicate of one that already exists. So I figured I could use "address","replace" instead but in that case the contact record is not updated or created at all - the insertion of the "api.address.replace" or "api.address.replace.0" (I've tried both) array into the "contact","create" array must result in an error.

I've also tried using option.match set to "location_type_id" but I get an error: "Ambiguous match criteria" when I do that.

I'm using PHP to access the api, if that makes a difference.

What am I doing wrong?

Thanks

nickholden

  • I post occasionally
  • **
  • Posts: 111
  • Karma: 1
  • CiviCRM version: 4.4.1
  • CMS version: Drupal 7
  • MySQL version: 5.5.32
  • PHP version: 5.4
Re: Does "address","replace" work?
September 30, 2013, 08:45:40 am
A quick update on this: I've refined my use of options.match so that the match criteria specify both the contact_id and the location_type_id. It now works - although presumably if any of our contacts have more than one home address we will get the 'ambiguous' error message again.

So "address.create.0" with options.match set will allow me to update a record including an effective replace of their home address. It would be good to know if "address","replace" also works, and if so, how to use it.

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Does "address","replace" work?
September 30, 2013, 09:57:18 am
Interesting that address.create with a contact id and location type id doesn't just work on its own, it used to in 4.2.
Contacts are not supposed to have more than one address at the same location - the ui enforces this, and the api did too, apparently not anymore.
Try asking your question on the new CiviCRM help site.

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Does "address","replace" work?
October 01, 2013, 02:58:04 pm
There shouldn't be anything special about "Address." I just wrote a test/example which works for me:

https://github.com/civicrm/civicrm-core/pull/1724

Some unordered thoughts:

 * The "options.match"  feature is only implemented in 4.4 -- it'll probably misbehave/fail silently in older releases.
 * If the call to Address.replace is chained from Contact.create, then the "contact_id" *should* be unnecessary/unharmful.
 * The error, "Ambiguous match criteria," means that it tried to find a single pre-existing record and encountered multiple pre-existing records. For example, if the DB already contains two records with "contact_id==1,location_type_id==2" and if you try to replace with one record having "contact_id==1,location_type_id==2", then it would generate the error.
 * It might be interesting to examine the list of records that exist when you run your code.
 * Maybe there's a use-case that isn't covered by the tests?

nickholden

  • I post occasionally
  • **
  • Posts: 111
  • Karma: 1
  • CiviCRM version: 4.4.1
  • CMS version: Drupal 7
  • MySQL version: 5.5.32
  • PHP version: 5.4
Re: Does "address","replace" work?
October 02, 2013, 05:43:16 am
Thanks, totten, so the solution I've employed works on my test system only because I'm currently testing 4.4.beta3, and won't work on my production (4.3.5) system? Pity.

My attempts to chain Address.replace from Contact.create all fail. There's no explicit use of contact_id in these attempts, I anticipated (as you say) that it would be unnecessary/harmful. That's the point of chaining, after all, right? The documentation suggests that "Address.replace" should replace the entire block of addresses linked to a contact (whether 0,1 or more exist) with a new block of addresses (1 in my case, but potentially more). But with no error message, and not even the contact being created in this situation, it is hard to debug.

Can you do chaining in the API explorer? I might try that...

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Does "address","replace" work?

This forum was archived on 2017-11-26.