Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upsert method #11

Open
patrickwarner opened this issue Mar 2, 2016 · 3 comments
Open

upsert method #11

patrickwarner opened this issue Mar 2, 2016 · 3 comments

Comments

@patrickwarner
Copy link

There does not seem to be a method which allows you to call the upsert API call.

Create a new contact or update an existing, based on a value of a filter or a set of filters. At least a single filter - query parameter - is required. If no parameters are present, the request will return an error.

@andriytkachiv
Copy link

UP

@quinncomendant
Copy link

Anybody working on this? I'll very soon need to implement leads->upsert. I would be willing to contribute if nobody has already started working on it.

FYI, here's the leads upsert docs, which have examples for only curl, ruby, and python.

@quinncomendant
Copy link

Until there is an official release of an upsert method, I've gotten this to work by adding this method to class ContactsService:

/**
 * Upsert a contact
 *
 * post '/contacts/upsert?{filters}'
 *
 * Create a new contact or update an existing, based on a value of a filter
 * or a set of filters. At least a single filter - query parameter - is
 * required. If no parameters are present, the request will return an error.
 *
 * @param array $filters A set of attributes that select an existing record to update. (If $filters is left empty, this function will behave the same as create().)
 * @param array $contact This array's attributes describe the object to be updated.
 *
 * @return array The resulting object representing updated resource.
 */
public function upsert(array $filters, array $contact)
{
  $attributes = array_intersect_key($contact, array_flip(self::$keysToPersist));
  if (isset($attributes['custom_fields']) && empty($attributes['custom_fields'])) unset($attributes['custom_fields']);

  if (empty($filters)) {
      list($code, $upsertedContact) = $this->httpClient->post("/contacts", $attributes);
  } else {
      list($code, $upsertedContact) = $this->httpClient->post(sprintf("/contacts/upsert?%s", http_build_query($filters)), $attributes);
  }

  return $upsertedContact;
}

And used like this:

$filter = [
	'email' => '[email protected]',
];
$contact = [
	'email' => '[email protected]',
    'name' => 'John Doe',
	'phone' => '1111111111111',
	'custom_fields' => [
		'Custom A' => '…',
		'Custom B' => '…',
	]
];

$basecrm = new \BaseCRM\Client($config);
$basecrm->contacts->upsert($filter, $contact);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants