phpxero-api

Xero API: updateOrCreateInvoices Overwriting Contact Details


I am using Xero Accounting API with PHP(OAuth2), specifically the updateOrCreateInvoices function in the Xero API to sync transactions. However, I've noticed that everytime I run this function, some contact details are being updated or overwritten, particularly the "additional persons" field. This field gets cleared out after the sync process.

I would appreciate any insights or clarifications on the behaviour of the updateOrCreateInvoices function. Is there a specific way to structure the request to maintain the integrity of the contact information? Any help or suggestions would be greatly appreciated.

Function Used: updateOrCreateInvoices

Problem: Contact details, specifically the "additional persons" field, are being overwritten or cleared.

Here is a snippet of code that I am using to sync invoices.

public function create_or_update_invoice($xero_tenant_id, $api_instance, $invoice_list = array()){
    $arr_invoices = array();
    foreach ($invoice_list as $invoice) {
        //Retrieve Existing Contact Details (using the getContacts function):
        $contact = $this->get_contact_by_id($xero_tenant_id, $api_instance, $xero_contact_id, $contact_name);
    
        //Prepare Invoice Data with Existing Contact & Invoice Details:
        $new_invoice = new XeroAPI\XeroPHP\Models\Accounting\Invoice;
        $new_invoice->setType(XeroAPI\XeroPHP\Models\Accounting\Invoice::TYPE_ACCREC)
        ->setContact($contact)
        ...
        ...
        ->setStatus(XeroAPI\XeroPHP\Models\Accounting\Invoice::STATUS_AUTHORISED);
    
        $arr_invoices[] = $new_invoice;
    }

    $invoices = new XeroAPI\XeroPHP\Models\Accounting\Invoices;
    $invoices->setInvoices($arr_invoices);

    try{
        //Update or Create Invoice:
        $result = $api_instance->updateOrCreateInvoices($xero_tenant_id, $invoices); 
        return $result;
    }catch(Exception $e){
        return $e;
    }
}

After the above steps, the "additional persons" field in the contact details is cleared out or overwritten.

As answered by sallyhornet, I have tested and confirmed that by only tagging the ContactID in the invoice, I am able to sync invoices into Xero without overwriting contact details.

Updated example snippet:

        ...
        //Retrieve Existing Contact Details (using the getContacts function):
        //original
        //$contact = $this->get_contact_by_id($xero_tenant_id, $api_instance, $xero_contact_id, $contact_name);
        //new
        $contact = new XeroAPI\XeroPHP\Models\Accounting\Contact;
        $contact->setContactID($xero_contact_id);
    
        //Prepare Invoice Data with Existing Contact & Invoice Details:
        $new_invoice = new XeroAPI\XeroPHP\Models\Accounting\Invoice;
        $new_invoice->setType(XeroAPI\XeroPHP\Models\Accounting\Invoice::TYPE_ACCREC)
        ->setContact($contact)
        ...

Solution

  • If you include the contact persons field you need to include all the additional people as the contact persons details in Xero are updated to match what you send in this field.

    The invoice only needs the contact id rather than full contact details and so if you don't want to update the contact you can just include the id rather than the full contact details.