Create a contact in Xero and get its contact Id using php api (oauth2)
Get the solution ↓↓↓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("[email protected]")
->setIncludeInEmails(true);
$arr_persons = [];
array_push($arr_persons, $person);
$contact = new XeroAPI\XeroPHP\Models\Accounting\Contact;
$contact->setName('FooBar')
->setFirstName("Foo")
->setLastName("Bar")
->setEmailAddress("[email protected]")
->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.
Answer
Solution:
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();
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: integrity constraint violation: 1452 cannot add or update a child row: a foreign key constraint fails
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.