Jump to content


Photo

How do I create a Note using the API and have the Note attached to a Contact?

API

  • Please log in to reply
9 replies to this topic

#1 jimf

jimf

    Advanced Member

  • Members
  • PipPipPip
  • 64 posts

Posted 08 October 2012 - 05:32 PM

I need to add a note (activity) to a contact every time I take action on the contact in my application. As currently documented in the API docs, when you add a note it does not get attached to a contact. I need to be able to tell the API what contact I want the note attached too. I tried the modelRelations below, but this throws error

"You can add relations only for HAS_MANY and MANY_MANY relations"


$authenticationData = login(variable_get('zurmourl').'/zurmo/api/login',variable_get('zurmouser'),variable_get('zurmopass')) ;
	   if  ($authenticationData)  {
	   $headers = array(
				'Accept: application/json',
				'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
				'ZURMO_TOKEN: ' . $authenticationData['token'],
				'ZURMO_API_REQUEST_TYPE: REST',
			);
		  
		   $data = Array
   (
	   'description' => 'Jim Save Note description',
	'occurredOnDateTime' => date('Y-m-d H:i:s'),
  
   );
		   $data['modelRelations'] = array(
			 'contacts' => array(
			   array(
				 'action' => 'add',
				 'modelId' => 31,
			   )
		  )
		   );
		  
		  
	  $response = createApiCall(variable_get('zurmourl').'/notes/note/api/create/'  , 'POST', $headers, array('data' => $data));
	  $response = json_decode($response, true);	


#2 jimf

jimf

    Advanced Member

  • Members
  • PipPipPip
  • 64 posts

Posted 08 October 2012 - 05:46 PM

While I have your attention.... How can I go the other way too? I need to be able to get all Notes associated to a Contact.

#3 Ivica

Ivica

    Advanced Member

  • Moderators
  • 565 posts

Posted 09 October 2012 - 07:24 AM

While I have your attention.... How can I go the other way too? I need to be able to get all Notes associated to a Contact.


We just this feature into bleeding version of Zurmo, please check unit test below to see how you can use this function:
ApiRestNoteTest::testGetNotesThatAreRelatedWithContactModel()

#4 Ivica

Ivica

    Advanced Member

  • Moderators
  • 565 posts

Posted 09 October 2012 - 07:28 AM

I need to add a note (activity) to a contact every time I take action on the contact in my application. As currently documented in the API docs, when you add a note it does not get attached to a contact. I need to be able to tell the API what contact I want the note attached too. I tried the modelRelations below, but this throws error

"You can add relations only for HAS_MANY and MANY_MANY relations"


$authenticationData = login(variable_get('zurmourl').'/zurmo/api/login',variable_get('zurmouser'),variable_get('zurmopass')) ;
	   if  ($authenticationData)  {
	   $headers = array(
				'Accept: application/json',
				'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
				'ZURMO_TOKEN: ' . $authenticationData['token'],
				'ZURMO_API_REQUEST_TYPE: REST',
			);
		  
		   $data = Array
   (
	   'description' => 'Jim Save Note description',
	'occurredOnDateTime' => date('Y-m-d H:i:s'),
  
   );
		   $data['modelRelations'] = array(
			 'contacts' => array(
			   array(
				 'action' => 'add',
				 'modelId' => 31,
			   )
		  )
		   );
		  
		  
	  $response = createApiCall(variable_get('zurmourl').'/notes/note/api/create/'  , 'POST', $headers, array('data' => $data));
	  $response = json_decode($response, true);	

For this issue please check unit test: ApiRestNoteTest::testCreatewithRelations(). Here is code example:
$contact  = ContactTestHelper::createContactByNameForOwner('Simon', $super);
			$contactItemId = $contact->getClassId('Item');
			$occurredOnStamp		  = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
			$data['description']	= "Note description";
			$data['occurredOnDateTime'] = $occurredOnStamp;
			$data['modelRelations'] = array(
				'activityItems' => array(
					array(
						'action' => 'add',
						'modelId' => $contact->id,
						'modelClassName' => 'Contact'
					),
				),
			);
			$response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/notes/note/api/create/', 'POST', $headers, array('data' => $data));


#5 jimf

jimf

    Advanced Member

  • Members
  • PipPipPip
  • 64 posts

Posted 09 October 2012 - 11:53 AM

Awesome!! You are the man! Thanks Ivica

#6 jimf

jimf

    Advanced Member

  • Members
  • PipPipPip
  • 64 posts

Posted 09 October 2012 - 12:16 PM

I used something close to this. See below. 31 is the Contact->id I got from loading a contact. But when I execute the above code I get "The related model ID specified was invalid."

So I'm assuming that the Contact->Id above is actually some an activity ID of some kind. But how can I generate this ID using the API. I can't execute this kind of call from the api ( $contactItemId = $contact->getClassId('Item');)



$data = Array
(
'description' => 'Jim Save Note description',
'occurredOnDateTime' => date('Y-m-d H:i:s'),


);
$data['modelRelations'] = array(
'activityItems' => array(
array(
'action' => 'add',
'modelId' => 31,
'modelClassName' => 'Contact'
)
)
);


$response = createApiCall(variable_get('zurmourl').'/notes/note/api/create/' , 'POST', $headers, array('data' => $data));

#7 Ivica

Ivica

    Advanced Member

  • Moderators
  • 565 posts

Posted 09 October 2012 - 01:50 PM

Relation API code is modified and that is probably reason why it didn't work, please upgrade to 0.7.50.

You don't have to use $contactItemId, it is used just for unit test asserts, and you can remove line with this variable.

#8 jimf

jimf

    Advanced Member

  • Members
  • PipPipPip
  • 64 posts

Posted 10 October 2012 - 09:32 AM

This is working great now, thanks.

#9 mledwards

mledwards

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 25 January 2013 - 04:13 PM

Exactly what I needed, thanks :-)



#10 qualtrax1

qualtrax1

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 11 November 2015 - 11:19 PM

These are good answers, but I need the opposite.  Given a note, I need to know which lead/contact it belongs to.

 

Goal:

1) Get yesterdays notes (done)

2) For each note, retrieve information on the lead/contact (???)

 

How can I get a lead/contact id from a note id?  Thanks in advance!







Also tagged with one or more of these keywords: API

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users