<?php # @km if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class getContacts extends SugarApi { public function registerApiRest() { return array( //GET 'getContacts' => array( //request type 'reqType' => 'POST', //endpoint path 'path' => array('CONTACTS', 'getContacts'), //endpoint variables 'pathVars' => array('', ''), //method to call 'method' => 'fn_getContacts', //short help string to be displayed in the help documentation 'shortHelp' => 'get Contacts details', //long help to be displayed in the help documentation //'longHelp' => '', ), ); } /** * Method to be used for my MyEndpoint/GetExample endpoint */ public function fn_getContacts($api, $args) { global $log, $timedate; $Conid = $args['id']; if(!empty($Conid)){ $Contact->retrieve($Conid); $link = 'contacts_cases'; if ($Contact->load_relationship($link)) { $relatedBeans = $Contact->$link->getBeans(); $parentBean = false; if (!empty($relatedBeans)) { reset($relatedBeans); $responsearry = array(); foreach($relatedBeans as $ft) { $element = array(); $element['Number'] = $ft->case_number; $element['Subject'] = $ft->name; $element['Account Name'] = $ft->account_name; $element['Status'] = $ft->status; $element['Priority'] = $ft->priority; $element['Type'] = $ft->type; $element['Source'] = $ft->source; $element['Description'] = $ft->description; $element['Primary Contact'] = $ft->primary_contact_name; $responsearry[]=$element; $return_data = array('contact_id' => $contactDetails[0]['id'],'First Name:' => $contactDetails[0]['first_name'],'Last Name:' => $contactDetails[0]['last_name'],'Cases' => $responsearry); return json_encode($return_data); } }} $return_data = array('error' => 'Not Found'); return json_encode($return_data); } } }
and what is exactly your question? The code you posted will return the first case related to the specified contact, if the contact can be found and if there are any cases related to that specific contact and if the json you create is valid according to json_encode..
I think your code should return what you are after - it looks like it will provide the list rather than just the first one to me. However, your 'path' is not correct as you have the module in full upper case: 'CONTACTS'. This should be just in title case, 'Contacts' so the line needs to read:
//endpoint path 'path' => array('Contacts', 'getContacts'),
That is the first thing I would fix if debugging this code sample.
Thanks,
JH.
I think your code should return what you are after - it looks like it will provide the list rather than just the first one to me. However, your 'path' is not correct as you have the module in full upper case: 'CONTACTS'. This should be just in title case, 'Contacts' so the line needs to read:
//endpoint path 'path' => array('Contacts', 'getContacts'),
That is the first thing I would fix if debugging this code sample.
Thanks,
JH.
Also, in your $return_data array you are not correctly referencing the Contact details. You have put:
$return_data = array('contact_id' => $contactDetails[0]['id'],'First Name:' => $contactDetails[0]['first_name'],'Last Name:' => $contactDetails[0]['last_name'],'Cases' => $responsearry);
although you do not have any object called $contactDetails (I guess this is from previous adjustments when debugging). This needs to be:
$return_data = array('contact_id' => $Contact->id,'First Name:' => $Contact->first_name,'Last Name:' => $Contact->last_name,'Cases' => $responsearry);
Thanks,
JH.