Set meetings_users (invitees) & accept_status through REST API using set_relationship

Hey folks,

I have been beating my head against this all day and am unfortunately unable to crack it so far. I am trying to add a Meeting to a user’s calendar through web services. I have previously accomplished this internally via a logic hook in the method described here. After looking at the set_accept_status function in /modules/Meetings/Meeting.php, to me it LOOKS like the function is really just adding a relationship and then setting the value of the accept_status field. So, in order to try to accomplish this through web services, I decided to try to use the set_relationship rest call. Unfortunately, I keep getting the following response:


[created] => 0
[failed] => 1
[deleted] => 0

I am wondering if anyone else has ever accomplished the same functionality, or might have any insights into what I may be doing wrong? My code is below (I have hardcoded the meeting record’s ID and the user ID for testing):


$url = "https://oursuitecrminstance.com/service/v4_1/rest.php";
$username = "admin";
$password = "password";

function call($method, $parameters, $url)
{
	ob_start();
	$curl_request = curl_init();

	curl_setopt($curl_request, CURLOPT_URL, $url);
	curl_setopt($curl_request, CURLOPT_POST, 1);
	curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
	curl_setopt($curl_request, CURLOPT_HEADER, 1);
	curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);

	$jsonEncodedData = json_encode($parameters);

	$post = array(
		 "method" => $method,
		 "input_type" => "JSON",
		 "response_type" => "JSON",
		 "rest_data" => $jsonEncodedData
	);

	curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
	$result = curl_exec($curl_request);
	curl_close($curl_request);

	print_r($result);
	$result = explode("\r\n\r\n", $result, 2);
	$response = json_decode($result[1]);
	ob_end_flush();

	return $response;
}

$login_parameters = array(
	 "user_auth" => array(
		  "user_name" => $username,
		  "password" => md5($password),
		  "version" => "1"
	 ),
	 "application_name" => "RestTest",
	 "name_value_list" => array(),
);

$login_result = call("login", $login_parameters, $url);
$session_id = $login_result->id;
	
// ACTION TO ADD MEETING TO USER'S CALENDAR

if($session_id){
	$set_relationship_parameters = array(
		'session' => $session_id,
		'module_name' => 'Meetings',
		'module_id' => 'ede7c2ad-f180-39ef-571a-563b6ab6f10e',
		'link_field_name' => 'meetings_users',
		'related_ids' => array(
			'1',
		),
		'name_value_list' => array(
			array(
				'name' => 'accept_status',
				'value' => 'none'
			),
		),
		'delete'=> 0,
	);
	$set_relationship_result = call('set_relationship', $set_relationship_parameters, $url);
	print_r($set_relationship_result);
}

I’m using the set_relationship REST call using the example from Sugar’s 6.5 Developer Guide. I have also read through HOWTO: Dealing with set_relationship failures and I don’t believe any of those errors apply to me. Lastly, I turned on debugging mode and I believe found the relevant portion of the log:


Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] Hook called: Users::after_retrieve
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Begin: SoapHelperWebServices->login_success
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Users language is = en_us
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Found language file: en_us.lang.php
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Found extended language file: en_us.lang.ext.php
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Found custom language file: en_us.lang.php
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] End: SoapHelperWebServices->login_success
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Begin: SoapHelperWebServices->validate_authenticated - passed
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] End: SoapHelperWebServices->validate_authenticated
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] End: SoapHelperWebServices->checkSessionAndModuleAccess - 
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Begin: SoapHelperWebServices->new_handle_set_relationship
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] rebuilding cache for Meetings
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Query:SELECT * FROM fields_meta_data WHERE  custom_module='Meetings' AND  deleted = 0
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Query Execution Time:0.0046300888061523
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] Hook called: Meetings::before_retrieve
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] Including module specific hook file for custom/modules/Meetings
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] Retrieve Meeting : SELECT meetings.*,meetings_cstm.* FROM meetings  LEFT JOIN meetings_cstm ON meetings.id = meetings_cstm.id_c  WHERE meetings.id = 'ede7c2ad-f180-39ef-571a-563b6ab6f10e' AND meetings.deleted=0
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] Limit Query:SELECT meetings.*,meetings_cstm.* FROM meetings  LEFT JOIN meetings_cstm ON meetings.id = meetings_cstm.id_c  WHERE meetings.id = 'ede7c2ad-f180-39ef-571a-563b6ab6f10e' AND meetings.deleted=0 Start: 0 count: 1
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Query:SELECT meetings.*,meetings_cstm.* FROM meetings  LEFT JOIN meetings_cstm ON meetings.id = meetings_cstm.id_c  WHERE meetings.id = 'ede7c2ad-f180-39ef-571a-563b6ab6f10e' AND meetings.deleted=0 LIMIT 0,1
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Query Execution Time:0.0067169666290283
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] rebuilding cache for Opportunities
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Query:SELECT * FROM fields_meta_data WHERE  custom_module='Opportunities' AND  deleted = 0
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Query Execution Time:0.00054788589477539
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Query:SELECT id ,opportunities.name as parent_name , opportunities.assigned_user_id owner FROM opportunities WHERE deleted=0 AND id='17f5f258-d8ee-7934-a7ad-563b6afe4814'
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Query Execution Time:0.00022506713867188
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] SugarBean[Meeting].load_relationships, Loading relationship (contacts).
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] rebuilding cache for Contacts
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Query:SELECT * FROM fields_meta_data WHERE  custom_module='Contacts' AND  deleted = 0
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Query Execution Time:0.0046939849853516
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] rebuilding cache for Contacts
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Query:SELECT * FROM fields_meta_data WHERE  custom_module='Contacts' AND  deleted = 0
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Query Execution Time:0.0068349838256836
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Query:SELECT contact_id id FROM meetings_contacts  WHERE meetings_contacts.meeting_id = 'ede7c2ad-f180-39ef-571a-563b6ab6f10e' AND meetings_contacts.deleted=0
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] Query Execution Time:0.00028109550476074
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] SugarBean[Meeting].load_relationships, Loading relationship (user_id1_c).
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] SugarBean.load_relationships, Error Loading relationship (user_id1_c)
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] SugarBean[Meeting].load_relationships, Loading relationship (user_id_c).
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] SugarBean.load_relationships, Error Loading relationship (user_id_c)
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] Hook called: Meetings::after_retrieve
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] SugarBean[Meeting].load_relationships, Loading relationship (meetings_users).
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] SugarBean.load_relationships, Error Loading relationship (meetings_users)
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] End: SoapHelperWebServices->new_handle_set_relationship
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] End: SugarWebServiceImpl->set_relationship
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] End: SugarRestJSON->serve
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][INFO] End: SugarRestService->serve
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] Hook called: ::server_round_trip
Tue Dec  1 13:26:00 2015 [27908][206706da-3720-83a1-bdc3-52ab1f0f9fe0][DEBUG] Calling MySQLi::disconnect()

From there, it looks like it might be failing to load the relationship. I have checked and I don’t think any of my custom logic hooks would be affecting it, but I will keep looking there. Otherwise, I’m unsure which route to take to keep going.

Any help would be greatly appreciated!

Thanks!

Yeah… that was my fault. I was using the relationship name instead of the link field name for the ‘link_field_name’ parameter. For those who experience the same problem, I opened up /modules/Meetings/vardefs.php and searched for meetings_users. Here’s what the link field for the relationship looks like:


  'users' =>
  array (
  	'name' => 'users',
    'type' => 'link',
    'relationship' => 'meetings_users',
    'source'=>'non-db',
		'vname'=>'LBL_USERS',
  ),

So, in this case, I needed to use ‘users’ as the link_field_name parameter of the API call. Working code below:


<?php

$url = "https://OURSUITEURL.com/sandbox/service/v4_1/rest.php"; //sandbox
$username = "admin";
$password = "password";

function call($method, $parameters, $url)
{
	ob_start();
	$curl_request = curl_init();

	curl_setopt($curl_request, CURLOPT_URL, $url);
	curl_setopt($curl_request, CURLOPT_POST, 1);
	curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
	curl_setopt($curl_request, CURLOPT_HEADER, 1);
	curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);

	$jsonEncodedData = json_encode($parameters);

	$post = array(
		 "method" => $method,
		 "input_type" => "JSON",
		 "response_type" => "JSON",
		 "rest_data" => $jsonEncodedData
	);

	curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
	$result = curl_exec($curl_request);
	curl_close($curl_request);

	$result = explode("\r\n\r\n", $result, 2);
	$response = json_decode($result[1]);
	ob_end_flush();

	return $response;
}

$login_parameters = array(
	 "user_auth" => array(
		  "user_name" => $username,
		  "password" => md5($password),
		  "version" => "1"
	 ),
	 "application_name" => "RestTest",
	 "name_value_list" => array(),
);

$login_result = call("login", $login_parameters, $url);
$session_id = $login_result->id;

if($session_id){
	$set_relationship_parameters = array(
		'session' => $session_id,
		'module_name' => 'Meetings',
		'module_id' => 'ede7c2ad-f180-39ef-571a-563b6ab6f10e',
		'link_field_name' => 'users',
		'related_ids' => array(
			'1',
		),
		'name_value_list' => array(
			array(
				'name' => 'accept_status',
				'value' => 'none'
			),
		),
		'delete'=> 0,
	);
	$set_relationship_result = call('set_relationship', $set_relationship_parameters, $url);
	print_r($set_relationship_result);
}
1 Like