Answers.Body: <p>As per this old issue and PR</p>
  <p><a href="https://github.com/colemanw/webform_civicrm/pull/58#issuecomment-289561525" rel="nofollow noreferrer">https://github.com/colemanw/webform_civicrm/pull/58#issuecomment-289561525</a></p>
  <p>you want to specify the price_field_value_id or else CiviCRM will try and be clever and guess one for you and will often get it wrong. I think it's using the same mechanism it uses when you specify values for a payment page without using a real price set - i.e. it access a hidden price set that it uses for the purpose.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-06T20:04:39.373Z
Answers.Text: Ah yes price_field_value_id seems to be what's missing. Thanks.
Answers.UserDisplayName: Demerit
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-06T20:00:40.757Z
Answers.LastActivityDate: 2021-05-06T20:00:40.757Z
Answers.UserDisplayName: Alan Dixon
Body: <p>Say I have two price fields, thumbtacks at $1/unit and ketchup at $20/unit (it's very good ketchup). Suppose they each have a different financial type, such as &quot;Office&quot; and &quot;Condiments&quot;.</p>
  <p>If I use the order api to create a contribution where you've bought one of each this looks fine. I get correct looking records in civicrm_line_item etc. The problem is when I record the payment. It properly records two items in civicrm_financial_item corresponding to the line items, but then in civicrm_entity_financial_trxn, it properly creates two records, but the entity_id for both of them points to the same entry in civicrm_financial_item. This then shows up in the UI on the contribution as &quot;Condiments, Condiments&quot; for the payment instead of &quot;Office, Condiments&quot;.</p>
  <p>When I record the same contribution from the start using the UI and the price set, it has entity_id pointing to the separate items, as I'd expect, and in the UI shows &quot;Office, Condiments&quot; for the payment.</p>
  <p>The code looks something like this:</p>
  <pre><code>$result = civicrm_api3('Order', 'create', [
    'contact_id' =&gt; 17,
    'financial_type_id' =&gt; 'Donation', // this seems irrelevant
    'receive_date' =&gt; date('YmdHis'),
    'contribution_status_id' =&gt; 'Pending',  // this is required according to the docs - the payment below completes it
    'line_items' =&gt; [
      [
        'params' =&gt; [],
        'line_item' =&gt; [
          [
            'label' =&gt; 'line 1',
            'qty' =&gt; 1,
            'unit_price' =&gt; '1',
            'line_total' =&gt; '1',
            'financial_type_id' =&gt; 22, // Office
            'price_field_id' =&gt; 12, // this seems almost irrelevant - using &quot;1&quot; has the same effect but then oddly doesn't show the line item breakdown when viewing the contribution
          ],
          // It doesn't seem to make a difference if I put the 2nd line item here or as a separate array in the outer array.
          // It's the payment part that is creating the wrong entity_id.
          [
            'label' =&gt; 'line 2',
            'qty' =&gt; 1,
            'unit_price' =&gt; '20',
            'line_total' =&gt; '20',
            'financial_type_id' =&gt; 23, // Condiments
            'price_field_id' =&gt; 13,
          ],
        ],
      ],
    ],
    'api.Payment.create' =&gt; ['total_amount' =&gt; 21],
  ]);
  </code></pre>
  
Tags: <financial-types>
Title: Order API + Payment with multiple line items creating wrong financial trxn record?
ThreadText: # Order API + Payment with multiple line items creating wrong financial trxn record?
  
  (Asked by: username_0 on 2021-05-06)
  
  Say I have two price fields, thumbtacks at $1/unit and ketchup at $20/unit (it's very good ketchup). Suppose they each have a different financial type, such as "Office" and "Condiments".
  
  If I use the order api to create a contribution where you've bought one of each this looks fine. I get correct looking records in civicrm\_line\_item etc. The problem is when I record the payment. It properly records two items in civicrm\_financial\_item corresponding to the line items, but then in civicrm\_entity\_financial\_trxn, it properly creates two records, but the entity\_id for both of them points to the same entry in civicrm\_financial\_item. This then shows up in the UI on the contribution as "Condiments, Condiments" for the payment instead of "Office, Condiments".
  
  When I record the same contribution from the start using the UI and the price set, it has entity\_id pointing to the separate items, as I'd expect, and in the UI shows "Office, Condiments" for the payment.
  
  The code looks something like this:
  
  ```
  $result = civicrm_api3('Order', 'create', [
    'contact_id' => 17,
    'financial_type_id' => 'Donation', // this seems irrelevant
    'receive_date' => date('YmdHis'),
    'contribution_status_id' => 'Pending',  // this is required according to the docs - the payment below completes it
    'line_items' => [
      [
        'params' => [],
        'line_item' => [
          [
            'label' => 'line 1',
            'qty' => 1,
            'unit_price' => '1',
            'line_total' => '1',
            'financial_type_id' => 22, // Office
            'price_field_id' => 12, // this seems almost irrelevant - using "1" has the same effect but then oddly doesn't show the line item breakdown when viewing the contribution
          ],
          // It doesn't seem to make a difference if I put the 2nd line item here or as a separate array in the outer array.
          // It's the payment part that is creating the wrong entity_id.
          [
            'label' => 'line 2',
            'qty' => 1,
            'unit_price' => '20',
            'line_total' => '20',
            'financial_type_id' => 23, // Condiments
            'price_field_id' => 13,
          ],
        ],
      ],
    ],
    'api.Payment.create' => ['total_amount' => 21],
  ]);
  
  ```
  
  **Tags:** financial-types
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-05-06. Score: 1 | Accepted answer
  
  As per this old issue and PR
  
  <https://github.com/colemanw/webform_civicrm/pull/58#issuecomment-289561525>
  
  you want to specify the price\_field\_value\_id or else CiviCRM will try and be clever and guess one for you and will often get it wrong. I think it's using the same mechanism it uses when you specify values for a payment page without using a real price set - i.e. it access a hidden price set that it uses for the purpose.
  
  > **Comments:**
  > **username_0**: Ah yes price_field_value_id seems to be what's missing. Thanks.  
  
  ---
  
  