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) »
  • Contributions created with 4.2 API give fatal error on edit
Pages: [1] 2

Author Topic: Contributions created with 4.2 API give fatal error on edit  (Read 4694 times)

Kirk

  • I post occasionally
  • **
  • Posts: 46
  • Karma: 2
  • CiviCRM version: 4.5.8
  • CMS version: Drupal 7.39
  • MySQL version: 5.5.44
  • PHP version: 5.6.13
Contributions created with 4.2 API give fatal error on edit
September 10, 2012, 06:23:06 am
In CiviCRM 4.2, when you use the API to create a contribution, it fails to create the associated line item record (in civicrm_line_item). This violates the integrity of the database because each completed contribution record is supposed to have an associated line item record.

When you edit and submit changes to a contribution that does not have an associated line item record, you get a fatal error. (Specifically, the error occurs when CRM_Contribute_Form_Contribution::postProcess tries to look up a price set using a price set ID from the non-existent line item.)

Creating a contribution through the web interface does create a line item record, so this problem is specific to the API. I think this problem is new to version 4.2. I think the CiviCRM 4.1.5 API did create the line item records as it should.

You can reproduce this error on the CiviCRM 4.2 demo site. Just go to the API explorer and use it to create a contribution record, then go to the web interface and edit the contribution. When you try to save your changes, you'll get an "unexpected error" page.

For us this is quite a serious problem because we need to import thousands of contributions using the API. Does a bug need to be created on the Issue Tracker for this?
« Last Edit: September 10, 2012, 06:24:42 am by kirk »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Contributions created with 4.2 API give fatal error on edit
September 10, 2012, 08:46:21 am
Hi,

It's a bug indeed. Could you investigate at what point when creating a contrib via the UI this line item is created?

I might either be that the line is created in the Form (instead of in the BAO) or that it's rightly created in the BAO but that the API doesn't set a needed param to fill in properly

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

Kirk

  • I post occasionally
  • **
  • Posts: 46
  • Karma: 2
  • CiviCRM version: 4.5.8
  • CMS version: Drupal 7.39
  • MySQL version: 5.5.44
  • PHP version: 5.6.13
Re: Contributions created with 4.2 API give fatal error on edit
September 10, 2012, 02:14:24 pm
Thanks for your response. I'm sorry but I don't think I can investigate further at the moment as I'm too busy. I'll have another look when I can find the time, but that might not be for a week or two. It might be better if someone else can take it on.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Contributions created with 4.2 API give fatal error on edit
September 10, 2012, 03:15:47 pm
My feeling based on this & another ticket is that there is something to do with price sets that should be created / linked & isn't being done in 4.2. I flicked an email to Joe / Pradeep.

It's likely the logic is in the form layer not the BAO layer
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

Kirk

  • I post occasionally
  • **
  • Posts: 46
  • Karma: 2
  • CiviCRM version: 4.5.8
  • CMS version: Drupal 7.39
  • MySQL version: 5.5.44
  • PHP version: 5.6.13
Re: Contributions created with 4.2 API give fatal error on edit
September 10, 2012, 03:35:31 pm
Thanks Eileen. Shall I create a ticket for this?

Kirk

  • I post occasionally
  • **
  • Posts: 46
  • Karma: 2
  • CiviCRM version: 4.5.8
  • CMS version: Drupal 7.39
  • MySQL version: 5.5.44
  • PHP version: 5.6.13
Re: Contributions created with 4.2 API give fatal error on edit
September 13, 2012, 06:14:00 am
I've noticed that a user of CiviCRM has created a ticket for this on the Issue Tracker: http://issues.civicrm.org/jira/browse/CRM-10812 It's currently assigned to Lobo.
« Last Edit: September 13, 2012, 06:20:01 am by kirk »

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: Contributions created with 4.2 API give fatal error on edit
September 13, 2012, 03:17:25 pm
Kirk - We're pretty sure that the problem originally reported in CRM-10812 is an upgrade problem - since Leslie (the original reporter) said they were getting the error on old / existing contribution records. We'll probably try to get a copy of that DB (pre / post upgrade) to verify what's happening there.

Your excellent diagnosis points to a separate issue - which is that the contribution create API is currently creating "broken" contribution records (they are missing the required line item). We are discussing this w/ the API team and the folks who worked on the price set changes for 4.2. Can you please file a separate issue for this in the issue tracker - we hope to address it fairly quickly.
Protect your investment in CiviCRM by  becoming a Member!

Kirk

  • I post occasionally
  • **
  • Posts: 46
  • Karma: 2
  • CiviCRM version: 4.5.8
  • CMS version: Drupal 7.39
  • MySQL version: 5.5.44
  • PHP version: 5.6.13
Re: Contributions created with 4.2 API give fatal error on edit
September 14, 2012, 03:38:01 am
OK Dave, I've created a new issue CRM-10817.

I've investigated a bit further and it seems that the API's failure to create a line item record is not the only problem. When you use the API to update a contribution record that was created with the API, you get a different error that is not caused by a missing line item record:
Quote
#0 /var/www/drupal7.civicrm.org/svn/civicrm42/CRM/Core/DAO.php(1007): CRM_Core_Error::fatal(' is not of type...')
#1 /var/www/drupal7.civicrm.org/svn/civicrm42/CRM/Core/DAO.php(907): CRM_Core_DAO::composeQuery('INSERT INTO civ...', Array, true)
#2 /var/www/drupal7.civicrm.org/svn/civicrm42/CRM/Core/BAO/CustomValueTable.php(236): CRM_Core_DAO::executeQuery('INSERT INTO civ...', Array)
#3 /var/www/drupal7.civicrm.org/svn/civicrm42/CRM/Core/BAO/CustomValueTable.php(340): CRM_Core_BAO_CustomValueTable::create(Array)
#4 /var/www/drupal7.civicrm.org/svn/civicrm42/CRM/Contribute/BAO/Contribution.php(258): CRM_Core_BAO_CustomValueTable::store(Array, 'civicrm_contrib...', '200243')
#5 /var/www/drupal7.civicrm.org/svn/civicrm42/api/v3/Contribution.php(77): CRM_Contribute_BAO_Contribution::create(Array, Array)
#6 /var/www/drupal7.civicrm.org/svn/civicrm42/api/api.php(65): civicrm_api3_contribution_create(Array)
#7 /var/www/drupal7.civicrm.org/svn/civicrm42/api/v3/Generic/Update.php(38): civicrm_api('Contribution', 'create', Array)
#8 /var/www/drupal7.civicrm.org/svn/civicrm42/api/api.php(61): civicrm_api3_generic_update(Array)
#9 /var/www/drupal7.civicrm.org/svn/civicrm42/CRM/Utils/REST.php(426): civicrm_api('Contribution', 'update', Array)
...

Even if you manually insert a line item record for the contribution, this error still occurs when you try to update the contribution using the API. However, this error does not occur with contributions that were created using the web UI; nor does it occur when you try to update the contribution using the web UI. Should I create a new issue for this, or should it be dealt with as part of CRM-10817?

I am concerned that in version 4.2, the logic for creating, updating and deleting contribution records was changed considerably, but instead of the new logic being put into the BAO layer so it would apply to the API as well as the web UI, the logic has been put into the form processing code, which leaves the API behind, and as a result, the contribution API has been rendered unusable.
« Last Edit: September 24, 2012, 02:59:45 am by kirk »

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: Contributions created with 4.2 API give fatal error on edit
September 14, 2012, 11:41:54 am
Kirk - We will be working on this at the code sprints following CiviCon next week and hope to have a "default behaviors" solution in time for a 4.2.2 release.
Protect your investment in CiviCRM by  becoming a Member!

Kirk

  • I post occasionally
  • **
  • Posts: 46
  • Karma: 2
  • CiviCRM version: 4.5.8
  • CMS version: Drupal 7.39
  • MySQL version: 5.5.44
  • PHP version: 5.6.13
Re: Contributions created with 4.2 API give fatal error on edit
September 14, 2012, 12:18:58 pm
Great, thanks, sorry I can't contribute more at the moment. Hope CiviCon goes well.  :)

Yashodha Chaku

  • Forum Godess / God
  • Ask me questions
  • *****
  • Posts: 755
  • Karma: 57
    • CiviCRM
Re: Contributions created with 4.2 API give fatal error on edit
September 23, 2012, 02:58:18 am
kirk :

We have made the fixes in the 4.2 code base,

would be great if you do some testing for import on our demo (http://drupal.demo.civicrm.org/)

or if you have a svn install, then you can pull up all the changes by doing a svn update on your 4.2 code base

-Yashodha
Found this reply helpful? Contribute NOW and help us improve CiviCRM with the Make it Happen! initiative.

Kirk

  • I post occasionally
  • **
  • Posts: 46
  • Karma: 2
  • CiviCRM version: 4.5.8
  • CMS version: Drupal 7.39
  • MySQL version: 5.5.44
  • PHP version: 5.6.13
Re: Contributions created with 4.2 API give fatal error on edit
September 23, 2012, 06:11:37 am
I don't have svn but I have tested on the demo environment. The original error no longer occurs: When I create a contribution using the API and then save it using the web UI, it no longer gives a fatal error. Hopefully, this means that creating a contribution with the API causes a line item record to be created as it does with the web UI. Thanks for your work on this.

However, this has not cleared up the other problem that I identified in my comment above; that if you use the API to update a contribution that was created using the API, you get a different fatal error. This suggests to me that there are still differences in how the web UI creates a contribution in the database and how the API creates a contribution in the database. (Surely they should be using the same code, in the BAO layer?)

Shall I create a new ticket for this second problem, or will it be fixed as part of CRM-10817?

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Contributions created with 4.2 API give fatal error on edit
September 23, 2012, 06:24:16 am
Can you confirm you are using

civicrm('contribution','create', array('id' => x, ......);

NOT 

civicrm('contribution','update', array('id' => x, ......);

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

Kirk

  • I post occasionally
  • **
  • Posts: 46
  • Karma: 2
  • CiviCRM version: 4.5.8
  • CMS version: Drupal 7.39
  • MySQL version: 5.5.44
  • PHP version: 5.6.13
Re: Contributions created with 4.2 API give fatal error on edit
September 23, 2012, 07:27:35 am
Hi Eileen,

When I identified the original problem, I was using
Code: [Select]
civicrm_api("Contribution", "create", array(...to import contributions into CiviCRM, and I noticed that contributions imported in this way were lacking line item records.

Now that Yashodha has posted about a fix, I am unable to test it by calling the API from PHP because I don't have Yashodha's fixed code on my machine, but I have tested it using the demo site's API Explorer to create the contribution records. The original problem does not occur on the demo site now, which suggests that the fixed API now creates line item records.

However, there is still another problem, which seems to be related to the original problem. To reproduce this other problem on the demo environment, follow these steps:
  • Use the API to create a contribution:
          
    • Go to the API Explorer.
    • In the entity dropdown, select Contribution.
    • In the action dropdown, select create.
    • Append the following values to the query string: &contact_id=CONTACTID&contribution_type=Donation&total_amount=123 where CONTACTID is the ID of an existing contact.
    • Click Go.
    • Note the ID of the created contribution.
       
  • Use the API to update the same contribution:
          
    • Still in the API Explorer, in the action dropdown, select update.
    • Append the following values to the query string: &id=CONTRIBUTIONID&fee_amount=0&net_amount=0 where CONTRIBUTIONID is the ID of the contribution you previously created. (Fee amount and net amount are included only because they are required fields.)
    • Click Go.
       
You will get a fatal error with the following trace:
Quote
#0 /var/www/drupal7.civicrm.org/svn/civicrm42/CRM/Core/DAO.php(1007): CRM_Core_Error::fatal(' is not of type...')
#1 /var/www/drupal7.civicrm.org/svn/civicrm42/CRM/Core/DAO.php(907): CRM_Core_DAO::composeQuery('INSERT INTO civ...', Array, true)
#2 /var/www/drupal7.civicrm.org/svn/civicrm42/CRM/Core/BAO/CustomValueTable.php(236): CRM_Core_DAO::executeQuery('INSERT INTO civ...', Array)
#3 /var/www/drupal7.civicrm.org/svn/civicrm42/CRM/Core/BAO/CustomValueTable.php(340): CRM_Core_BAO_CustomValueTable::create(Array)
#4 /var/www/drupal7.civicrm.org/svn/civicrm42/CRM/Contribute/BAO/Contribution.php(258): CRM_Core_BAO_CustomValueTable::store(Array, 'civicrm_contrib...', '29')
#5 /var/www/drupal7.civicrm.org/svn/civicrm42/api/v3/Contribution.php(77): CRM_Contribute_BAO_Contribution::create(Array, Array)
#6 /var/www/drupal7.civicrm.org/svn/civicrm42/api/api.php(65): civicrm_api3_contribution_create(Array)
#7 /var/www/drupal7.civicrm.org/svn/civicrm42/api/v3/Generic/Update.php(38): civicrm_api('Contribution', 'create', Array)
#8 /var/www/drupal7.civicrm.org/svn/civicrm42/api/api.php(61): civicrm_api3_generic_update(Array)
#9 /var/www/drupal7.civicrm.org/svn/civicrm42/CRM/Utils/REST.php(426): civicrm_api('Contribution', 'update', Array)
...

This is not the original error that started this topic, but it seems to be related, because like the original error, it only occurs with contributions that were created using the API, which is why I raised it here, and why Dave Greenberg added the following note to CRM-10817:

Quote
Looks like there may also be an issue using the API to update an existing contribution. This needs more investigation as it might be an artifact of the create problem.

So, to recap, there were two problems:
  • Contributions created using the API cannot be updated using the web UI. This is because the API wasn't creating line item records, and it seems to have been resolved by Yashodha's fix.
  • Contributions created using the API cannot be updated using the API. This problem is not caused by missing line item records - it's caused by some other difference between the way the web UI creates contributions and the way the API creates contributions - and it has not been resolved by Yashodha's fix.
So, my question is this: Should I create a new bug report in the Issue Tracker for this second problem, or will it be fixed as part of CRM-10817? Thanks.
« Last Edit: September 23, 2012, 07:58:23 am by kirk »

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Contributions created with 4.2 API give fatal error on edit
September 23, 2012, 10:31:24 am
OK - so it we assume that UPDATE should not exist and we should shoot Xavier for allowing it to show in the api/explorer and the only valid way to do an update is to use create as the action (& fill in the id field) - does it work with create?
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] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Contributions created with 4.2 API give fatal error on edit

This forum was archived on 2017-11-26.