API set_relationship - Please Help!

I’m working on a script to import data into SuiteCRM. I need to create a Contact and an Opportunity. I then need to relate the two.

I have a many to many relationship between Contacts and Opportunities. Each contact should be able to create multiple opportunities. Each opportunity should be able to be assigned to multiple contacts.

When I run the code it says “1 Relationship(s) created”, but when I check Suite there’s nothing listed under the contact or opportunity subpanels.

FYI, I renamed the Opportunities module “Gigs” and am using this API Wrapper: https://github.com/asakusuma/SugarCRM-REST-API-Wrapper-Class

Here’s the code:


<?php

// Load Composer Dependencies for Sugar API Wrapper
require_once('vendor/autoload.php');

// Create Sugar Object
$sugar = new \Asakusuma\SugarWrapper\Rest;

// Set Sugar Connection Items
$sugar->setUrl('https://mysite.com/suitecrm/service/v2/rest.php');
$sugar->setUsername('User');
$sugar->setPassword('Pass');

// Connect to Sugar 
$sugar->connect();

// Did something go wrong with the connection? Report it. 
$error = $sugar->get_error();

if($error !== FALSE) {
	return $error['name'];
}
// Ok... We're going to try and create a test entry in Sugar/Suite

// Create a Contact
$modules = 'Contacts';

// Set Values
$values = array(
	'contact_type_c' => 'Prospect',
	'lead_source' => 'Website',
	'first_name' => 'Test',
	'last_name' => 'Contact',
	'phone_mobile' => '(123) 456-7890',
	'email1' => 'test@test.com'
	);

// Put it in Suite
$result = $sugar->set($modules, $values);

$contactID = $result['id'];

// Ok, now let's create a Opportunity
$modules = "Opportunities";

$values = array(
	'name' => 'My Test Gig',
	'sales_stage' => 'New Inquiry',
	'amount' => '400'
);
	
$result = $sugar->set($modules, $values);

$gigID = $result['id'];

// Lastly, let's relate the two - HERE'S WHERE I HAVE PROBLEMS!

// Set Relationship
$moduleName = 'Contacts';
$moduleID = $contactID;
$linkFieldName = 'opportunities';
$relatedIDs = array($gigID);
$nameValueList = array(); // Passing empty array because we don't have any fields that need it
$delete = 0;

$result = $sugar->set_relationship($moduleName, $moduleID, $linkFieldName, $relatedIDs, $nameValueList, $delete);

echo $result['created'] . " relationship(s) made";

?>

The Contact and Opportunity are created just fine. It’s the relationship that’s not happening.

Ugh… Ok, I get it.

I looked at the API documentation and was passing $relatedIDs back as an array. What I didn’t realize is that the API was doing this as well. So what got passed into Suite was a multidimensional array instead of a single array.

I’d delete this post but I don’t see an option for that. Mods… Feel free to make this disappear if you don’t feel it adds value. Sorry everyone. :slight_smile:

1 Like

Hi, can you share code to correct above coding. I still got error to relatedID.

thanks in advance