How to link a invoice with a contract using the AP

Hello,
I’m developing an order/contract module in a web site and I need to send the contract to Dolibarr. I want to create a contract and its lines using the API, then validate the contract and then create an invoice linked with the contract. I have succeed in creating both using the API but I can’t link them.
Thanks in advance.

When you create or update the contract or invoice, try to fill the array linkedObjectsIds

Thanks for your answer. I’ve tried to modify the linkedobjectID, but I haven’t succeed in finding the right syntax. I’ve seen in the data sent by the API
‘linkedObjectsIds’ =>
array (
‘contrat’ =>
array (
7 => ‘3’,
),
),

but I don’t understand what 7 means.
Regards

The 7 is in the database the link id between the contract and the invoice.

I have created via API, the link between the contract and the invoice with:
invoice[‘linkedObjectsIds’][‘contrat’] = contractId
I hope that helps

The 7 is in the database the link id between the contract and the invoice.

I have created via API, the link between the contract and the invoice with:

invoice['linkedObjectsIds']['contrat'] = contractId

I hope that helps

PS. Some texts got interpreted by bbcode

I tried linking a subscription and an invoice with json like this

{
  "linkedObjectsIds": {
    "subscription": "4"
    }
}

on an url like this http://localhost/api/index.php/invoices/2

I don’t get any errors, but I also do not get any link.

If I use the GUI to create a subscription and an invoice, and I then use the API to get that invoice I get data like this:

  "linkedObjectsIds": {
    "subscription": {
      "1": "3"
    }
  },

Where 1 is the rowid in llx_element_element, but I can not just guess the next free number and I can not look it up in the API can I?

llx_element_element

Please prove me wrong

I no longer think this is possible to link an invoice object using the API to update an existing invoice because it seems like this function:

is the function used to link objects and it is only mentioned in 2 create public functions in

450:	public function create(User $user, $notrigger = 0, $forceduedate = 0)
735:							$ret = $this->add_object_linked($origin, $origin_id);
743:						$ret = $this->add_object_linked($origin, $origin_id);
1128:	public function createFromCurrent(User $user, $invertdetail = 0)
1204:					$facture->add_object_linked($typeObject, $fk_object);
1208:			$facture->add_object_linked('facture', $this->fk_facture_source);
1222:	public function createFromClone(User $user, $fromid = 0)

I ended up extending htdocs/adherents/class/subscription.class.php

with this code

				// Add object linked
				if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
					foreach ($this->linked_objects as $origin => $tmp_origin_id) {
						if (is_array($tmp_origin_id)) {       // New behaviour, if linked_object can have several links per type, so is something like array('contract'=>array(id1, id2, ...))
							foreach ($tmp_origin_id as $origin_id) {
								$ret = $this->add_object_linked($origin, $origin_id);
								if (!$ret) {
									$this->error = $this->db->lasterror();
									$error++;
								}
							}
						} else { // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
							$origin_id = $tmp_origin_id;
							$ret = $this->add_object_linked($origin, $origin_id);
							if (!$ret) {
								$this->error = $this->db->lasterror();
								$error++;
							}
						}
					}
				}

from htdocs/fourn/class/fournisseur.facture-rec.class.php

The only change I did was some indentation :slight_smile:

Afterwards, if posting this json to create a subscription it made the link that I needed :slight_smile:

{
  "linkedObjectsIds": {
    "facture": "6"  
  },
  "note_public": "api Contribution 2024",
  "datec": 1710699079,
  "datem": 1710699079,
  "dateh": 1710509000,
  "datef": 1744999200,
  "fk_type": "1",
  "fk_adherent": "1",
  "amount": "100.00000000"
}

Will submit a pull request