how to get case deatails from contacts using api method?

<?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);
		}	
   }
}

Parents Reply Children
  • 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.