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) »
  • API call updates contribution status; setting it causes constraint violation
Pages: [1]

Author Topic: API call updates contribution status; setting it causes constraint violation  (Read 760 times)

Sophie.SK

  • I’m new here
  • *
  • Posts: 17
  • Karma: 0
  • CiviCRM version: Various
  • CMS version: Drupal
API call updates contribution status; setting it causes constraint violation
October 30, 2013, 05:10:59 am
We have an API call that updates pending contributions and sets the value of a custom field after they have been exported through the UI:
Code: [Select]
civicrm_api('contribution', 'create', array(
            'version' => 3,
            'id' => $contribution_id,
            'custom_' . $custom_fields['sent'] => 1
        ));
This works just fine - the custom field is set to 1 for the existing contribution with the ID provided, as you'd expect. However, these contributions are then set to "Completed" - which they very much aren't - even though we haven't told the API to update that.

Changing this to (where we've done an array flip on CRM_Contribute_PseudoConstant::contributionStatus() to retrieve the status ID):
Code: [Select]
civicrm_api('contribution', 'create', array(
            'version' => 3,
            'id' => $contribution_id,
            'custom_' . $custom_fields['sent'] => 1,
            'contribution_status_id' => $status_id['Pending']
        ));
causes constraint violations when the code is run (the export takes place, which triggers the update), throwing our error of "Unable to mark contribution %1 as 'sent'".

Using the API explorer, I've been able to replicate this. Create a contribution, then set it up to run:
  • Entity: contribution; action: create
  • Parameters: Contribution ID: created contribution; Contribution status: 2
This causes a constraint violation.

Setting the contribution status to 1 (completed) or 3 (cancelled) is fine. Setting it to 2 (pending) or 4 (failed) causes the constraint violation.

We ended up solving this by writing an SQL statement, as this API bug means we are unable to use the API to run this update, since we have other code relying on the contributions being pending.

Any thoughts?
Circle Interactive

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: API call updates contribution status; setting it causes constraint violation
October 30, 2013, 11:56:42 am
1. You should try getting some more debug information from the API to find out more about the constraint violation, e.g.

Code: [Select]
$r = civicrm_api('contribution', 'create', array(
            'version' => 3,
            'id' => $contribution_id,
            'custom_' . $custom_fields['sent'] => 1,
            'debug' => 1,
        ));
print_r($r);

The $r record should include values for $r['sql'] and $r['debug'].

2. I just tried a work-around with the CustomValue API. This seems to work:

Code: [Select]
civicrm_api('CustomValue', 'create', array(
            'version' => 3,
            'entity_id' => $contribution_id,
            'custom_' . $custom_fields['sent'] => 1,
        ));

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API call updates contribution status; setting it causes constraint violation
October 30, 2013, 12:07:26 pm
Hi,

Wild guess: some changes of status triggers "plenty of stuff", including updating and activity (?). the accounting part of civi has expectations about which status can go after another one and you might bump into one transition that is unexpected.
Is "sent" one you created?
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Sophie.SK

  • I’m new here
  • *
  • Posts: 17
  • Karma: 0
  • CiviCRM version: Various
  • CMS version: Drupal
Re: API call updates contribution status; setting it causes constraint violation
October 31, 2013, 12:48:58 am
Hi both,

I think you're misunderstanding :-)

first of all, Xavier - "Sent" is not a contribution we set up, it's a boolean custom field (custom_55) on the contributions that we want to update. The workflow is approximately this:
  • Contributions are created during the month in a number of ways, and are marked as "pending".
  • At the end of each month, our client exports all the "pending" contributions, and sends them in a file off to their bank.
  • So that the contributions can't be changed, at this point, we mark them as "sent", but they aren't completed yet.
  • A few days later, the client receives a file from the bank and imports it to the site. Some magic happens, contributions are matched up, and those pending contributions are now marked as "completed".
Does that make a bit more sense?

The "unable to mark as sent" error we got was part of the custom code that we wrote, a user-friendly error. The API gives us the constraint violation error.

So to clarify, the problem we're having is that, when updating the custom field via contribution create (as in my first example), it was also updating the contribution status, which we neither asked for nor wanted it to do.

To try and get around this, we explicitly set the status (as in my second example), and that gave us the constraint violation error.

I'll have a go using debug => 1 in a short while, and get back to you on what that says.

If it helps, this is on Civi 4.3.3.
Circle Interactive

adixon

  • I post frequently
  • ***
  • Posts: 314
  • Karma: 19
    • Blackfly Solutions
Re: API call updates contribution status; setting it causes constraint violation
January 24, 2014, 02:06:32 pm
Just to confirm this thread:

1. The debug setting is super helpful here.

2. The issue for this user was almost surely the same as I ran into, which is various state change restrictions for contributions due to the new financial model, as xavier guessed. For example - you can't go from failed to completed. The debug setting makes this clear ...

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • API call updates contribution status; setting it causes constraint violation

This forum was archived on 2017-11-26.