REST and the One-To-Many relationships

sugarcrmdevelopers —  March 3, 2011 — 4 Comments

I have had more than a few cases this week dealing with web services and how to set One-to-Many relationships between modules.  Most issue dealt with a custom module and a stock module but I think this applies to any One-To-Many relationship.  The issue starts when you try to use the set_relationship command.  So far what I have found is that the set_relationship command only works with Many-To-Many relationships.  You set One-To-Many (and by extension Many-To-One) relationships with the set_entry command.

So I set up a new copy of SugarCRM on my server and created a new module called Soap_Test in ModuleBuilder.  I made it a basic module and added a Many-To-One relationship with Accounts to it.  This means that Accounts will have a SubPanel for Soap_Test and Soap_Test will have a select button to enter an Account.

I based this on previous examples of REST that i am sure you have all seen and there are a few things that need mentioning.  In the lines that look like this:

$postArgs = ‘method=login&input_type=JSON&response_type=JSON&rest_data=’ . $json;

the two JSONs need to be upper case

and in the lines that look like this

$response = curl_exec($ch);

you need to pass it the CURL object and not the session id.

As for the field name for the relationship (kab_soap_t567bccounts_ida) in my example script.  I found this by looking at the EditView HTML for my custom module.  Mine looked line this

<input type="text" name="kab_soap_te_accounts_name" class="sqsEnabled" id="kab_soap_te_accounts_name" value="" autocomplete="off">
<input type="hidden" name="kab_soap_t567bccounts_ida" id="kab_soap_t567bccounts_ida" value="">

 

The hidden field is where SugarCRM would put the ID of the Account that you chose to relate this record to.  So thats the field name that needs to be used in the set_entry and set_entries commands.

Now for the REST code, I commented it alot so it should be easy to follow, I’ll attach a copy of the code somewhere as cutting and pasting from here is next to impossible.  If you do what for the single tick marks, they will copy incorrectly.


<?php
// CREATE A REST SESSION RELATIONSHIP
// ****************************************************************************
// I created a new basic module in ModuleBuilder and called it Soap_Test
// I added a Many-To-One relationship with accounts and Deployed it.
// **************************************************************************** 

// specify the REST web service to interact with
$url = ‘//localhost/play/SugarEnt-Full-6.2.0beta/service/v2/rest.php‘;
// Get curl handle
$ch = curl_init($url);
// Configure curl
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set POST arguments
$parameters = array( ‘user_auth‘ => array( ‘user_name‘ => ‘admin‘,
password‘ => md5(‘sugarcrm‘) ) );
//Convert then to JSON
$json = json_encode($parameters);
$postArgs = ‘method=login&input_type=JSON&response_type=JSON&rest_data=‘ . $json;
//Set the POST packet created above to CURL
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArgs);
// Make the REST call, returning the result
$response = curl_exec( $ch );
// Convert the result from JSON format to a PHP array
$result = json_decode( $response, TRUE );
// Get the newly created Session id, You will need this for all the other REST calls.
$session = $result[‘id‘] ;

// ****************************************************************************
// Lets add a new Accounts record, if you have other required
// fields you will have to add them into the name_value_list
$parameters = array(
session‘ => $session,
module‘ => ‘Accounts‘,
name_value_list‘ => array(
array(‘name‘ => ‘name‘, ‘value‘ => ‘New SOAP TEST Account‘),
array(‘name‘ => ‘description‘, ‘value‘ => ‘Created from a REST web services call‘),
),
);
$json = json_encode($parameters);
$postArgs = ‘method=set_entry&input_type=JSON&response_type=JSON&rest_data=‘ . $json;
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArgs);
$response = curl_exec($ch);
$result = json_decode($response, TRUE);
$accountId = $result[‘id‘];

// ****************************************************************************
// Now, lets add a new KAB_soap_test record
$parameters = array(
session‘ => $session,
module‘ => ‘KAB_soap_test‘,
name_value_list‘ => array(
array(‘name‘ => ‘name‘, ‘value‘ => ‘New set_entry Record‘),
array(‘name‘ => ‘kab_soap_t567bccounts_ida‘, ‘value‘ => $accountId),
),
);
$json = json_encode($parameters);
$postArgs = ‘method=set_entry&input_type=JSON&response_type=JSON&rest_data=‘ . $json;
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArgs);
$response = curl_exec($ch);
$result = json_decode($response, TRUE);
$KAB_soap_testId = $result[‘id‘];

// ****************************************************************************
//OK, Now lets do it with set_entries
$parameters = array(
session‘ => $session,
module‘ => ‘KAB_soap_test‘,
name_value_lists‘ => array(
array(
0‘ => array(‘name‘ => ‘name‘, ‘value‘ => ‘set_entries Record #1‘),
1‘  => array(‘name‘ => ‘kab_soap_t567bccounts_ida‘, ‘value‘ => $accountId),
),
array(
0‘ => array(‘name‘ => ‘name‘, ‘value‘ => ‘set_entries Record #2‘),
1‘  => array(‘name‘ => ‘kab_soap_t567bccounts_ida‘, ‘value‘ => $accountId),
),
)
);
$json = json_encode($parameters);
$postArgs = ‘method=set_entries&input_type=JSON&response_type=JSON&rest_data=‘ . $json;
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArgs);
$response = curl_exec($ch);
$result = json_decode($response, TRUE);
$customIds = $result;

echo “Done”;
?>

 

 

 

 

 

 

 

 

 

4 responses to REST and the One-To-Many relationships

  1. 
    Chris Georgakopoulos July 4, 2011 at 1:42 pm

    Two full days lost while searching why set_relationships was not working. Thank you so much!

  2. 

    Thanks a lot. Set_relationship did not work for me. This did!

  3. 

    Perfect! this worked for me!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s