0

Just learning Xero API (php) but I'm unsure how to proceed. The docs are pretty good in general. I've successfully created the oauth2 integration and this connects no problem (even for multiple organisations/tenants), I'm able to get the existing contacts in Xero but I now need to create a new contact (I have the name of this conteact - call her Jane Doe) i then wish to update my database record with this new Contacts contactId.

So the docs are a bit confusing but looking at the php api i think i can use:

$response = $accountingApi->setContacts( $xeroTenantId, '{"Name": "Jane Doe"}' );

would this be the right kind of approach (where $accountingApi is defined in a call earlier in the cycle and is connected)? does anyone have an example on how to add a new contact to Xero and return this new contacts contactId?

The docs don't say what (if any) response is returned after adding a new contact.

Lastly somewhat related to this, Some of my contacts are in more than one linked organisations, would these have the same clientID or will i need to somehow define one for each connected organisation?

thanks in advance

ADDITONAL

the api docs on github have this snippet:

try {
            $person = new XeroAPI\XeroPHP\Models\Accounting\ContactPerson;
            $person->setFirstName("John")
                ->setLastName("Smith")
                ->setEmailAddress("john.smith@24locks.com")
                ->setIncludeInEmails(true);

            $arr_persons = [];
            array_push($arr_persons, $person);

            $contact = new XeroAPI\XeroPHP\Models\Accounting\Contact;
            $contact->setName('FooBar')
                ->setFirstName("Foo")
                ->setLastName("Bar")
                ->setEmailAddress("ben.bowden@24locks.com")
                ->setContactPersons($arr_persons);

            $arr_contacts = [];
            array_push($arr_contacts, $contact);
            $contacts = new XeroAPI\XeroPHP\Models\Accounting\Contacts;
            $contacts->setContacts($arr_contacts);

            $apiResponse = $accountingApi->createContacts($xeroTenantId,$contacts);
            $message = 'New Contact Name: ' . $apiResponse->getContacts()[0]->getName();
        } catch (\XeroAPI\XeroPHP\ApiException $e) {
            $error = AccountingObjectSerializer::deserialize(
                $e->getResponseBody(),
                '\XeroAPI\XeroPHP\Models\Accounting\Error',
                []
            );
            $message = "ApiException - " . $error->getElements()[0]["validation_errors"][0]["message"];
        }

I require only the name on Xero (all other details are in my linked App) and to obtain the contactId.

ThurstonLevi
  • 664
  • 13
  • 34

1 Answers1

1

Ok so having reviewed the docs I am going with:

                $contact = new XeroAPI\XeroPHP\Models\Accounting\Contact;
                $contact->setName('Jane Doe');
                $arr_contacts = [];
                array_push($arr_contacts, $contact);
                $contacts = new XeroAPI\XeroPHP\Models\Accounting\Contacts;
                $contacts->setContacts($arr_contacts);

                $apiResponse = $accountingApi->createContacts($xeroTenantId,$contacts);
                //$message = 'New Contact Name: ' . $apiResponse->getContacts()[0]->getName();
                $contactId = $apiResponse->getContacts()[0]->getContactId();

ThurstonLevi
  • 664
  • 13
  • 34
  • When I create a new contact, I look at the response as you are doing, which does include the Xero ID for the new contact. Same for invoices, payments, credit notes, the response always includes the ID for the new "thing" you just created. In fact if you send an invoice, you get all the invoice details you just sent, along with the contact details for whoever the invoice is to/from. I don't use PHP so I can't say whether your approach is correct, but if it gives the correct information, all good. – droopsnoot May 22 '20 at 17:34