Can't Relate Contact and Opportunity in SuiteCRM / SugarCRM CE - rest

I'm working on a script to import data into SuiteCRM / SugarCRM CE. 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: 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://example.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.

Related

Create new contact to Mailjet and adding to an existing list

After subscription on my website form, I need to create the new contact on Mailjet and then add it to a specific list that already exist.
I have this following code which I execute after insert my user to database.
$firstname = "Michael";
$email = myemail#gmail.com;
$mj = new \Mailjet\Client($MJ_APIKEY_PUBLIC,$MJ_APIKEY_PRIVATE);
// create new contact on Mailjet
$contact_params = [
"Email" => $email
];
$response_add = $mj->post(Resources::$Contact, ['body' => $contact_params]);
// adding the created contact to my list
$list_params = [
'Name' => $firstname,
'Action' => "addnoforce",
'Email' => $email
];
$response_list = $mj->post(Resources::$ContactslistManagecontact, ['id' => 10181436, 'body' => $list_params]);
The contact is correctly created on Mailjet but never add to the list. The $response_list always give me back NULL and I don't really know why. I'm pretty new with Mailjet API, but I've following the official API and the way I want to do it seems correct. (https://dev.mailjet.com/email/reference/contacts/subscriptions#v3_post_contactslist_list_ID_managecontact)
Thanks for help :)
In case you are yet to find a solution.
Try this out, it works for me.
Check the "/contactslist/{list_ID or list_address}/managecontact" section on this documentation; https://dev.mailjet.com/email/reference/contacts/subscriptions#v3_post_listrecipient
Enjoy

Fuel PHP - to_array() method and multiple belongs_to relationships and eager loading

I am attempting to migrate some legacy data models/schemas to a fuel API, and have run into an odd issue with the to_array() method on a model that has two $_belongs_to properties.
When I load the model without the to_array() method, I properly receive both related items with eager loading, but as soon as I pass them through this function to convert the data to make it digestable by the new API, it will strip out the second $_belongs_to property. If I re-order the props in the $belongs_to array, it will show whichever item is first in the array.
My question is, how can I convert this data to an array without losing the second relationship?
Here are some cleaned up examples for ease of reference:
Transaction Model:
protected static $_belongs_to = array(
'benefactor' => array(
'key_from' => 'from_user_id',
'model_to' => 'Model\\Legacy\\User',
'key_to' => 'id',
),
'user' => array(
'key_from' => 'user_id',
'model_to' => 'Model\\Legacy\\User',
'key_to' => 'id',
),
);
Transaction Controller:
$result = array();
$id = $this->param('id');
if (!empty($id)) {
$transaction = Transaction::find($id, array('related' => array('user', 'benefactor',)));
if (!empty($transaction)) {
// Works -- both benefactor and user are returned
$result['transaction_works'] = $transaction;
// Does not work -- only the benefactor is returned
$result['transaction_doesnt_work'] = $transaction->to_array();
}
}
return $this->response($result);
For any googlers looking for help on this issue, I was seemingly able to return all relationships by simply executing the to_array() method before setting the return/results variable:
$result = array();
$id = $this->param('id');
if (!empty($id)) {
$transaction = Transaction::find($id, array('related' => array('user', 'benefactor',)));
if (!empty($transaction)) {
$transaction->to_array();
$result['transaction_works'] = $transaction;
}
}
return $this->response($result);
Good luck!

Can someone provide a php sample using nusoap/sugarcrm api to create an acct/lead in sugarcrn?

Can someone provide a sample code chunk of php using the sugarcrm API/nusoap for adding creating an acct. and then linking a lead to the acct?
I've got a sample function that adds a lead, and I can see how to create an acct, but I can't see how to tie a lead to the acct, to simulate the subpanel process in the sugarcrm acct/subpanel process.
thanks
// Create a new Lead, return the SOAP result
function createLead($data)
{
// Parse the data and store it into a name/value list
// which will then pe passed on to Sugar via SOAP
$name_value_list = array();
foreach($data as $key => $value)
array_push($name_value_list, array('name' => $key, 'value' => $value));
// Fire the set_entry call to the Leads module
$result = $this->soap->call('set_entry', array(
'session' => $this->session,
'module_name' => 'Leads',
'name_value_list' => $name_value_list
));
return $result;
}
$result = $sugar->createLead(array(
'lead_source' => 'Web Site',
'lead_source_description' => 'Inquiry form on the website',
'lead_status' => 'New',
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'email1' => $_POST['email'],
'description' => $_POST['message']
));
You need to find the ID for the account and assign that ID to whatever the account_id field name is in the Lead Module. I have run into a couple things like this before and I have found it easier to go straight to the Sugar database. So, write a statement that will return the account is, for example: SELECT id WHERE something_in_the_account_table = something else;
Then you can assign that id in your $result array. I hope it helps. I didn't have any code or documentation in front of me or I would have helped more.

Yii CJuiAutoComplete for Multiple values

I am a Yii Beginner and I am currently working on a Tagging system where I have 3 tables:
Issue (id,content,create_d,...etc)
Tag (id,tag)
Issue_tag_map (id,tag_id_fk,issue_id_fk)
In my /Views/Issue/_form I have added a MultiComplete Extension to retrieve multiple tag ids and labels,
I have used an afterSave function in order to directly store the Issue_id and the autocompleted Tag_ids in the Issue_tag_map table, where it is a HAS_MANY relation.
Unfortunately Nothing is being returned.
I wondered if there might be a way to store the autocompleted Tag_ids in a temporary attribute and then pass it to the model's afterSave() function.
I have been searching for a while, and this has been driving me crazy because I feel I have missed a very simple step!
Any Help or advices of any kind are deeply appreciated!
MultiComplete in Views/Issue/_form:
<?php
echo $form->labelEx($model, 'Tag');
$this->widget('application.extension.MultiComplete', array(
'model' => $model,
'attribute' => '', //Was thinking of creating a temporary here
'name' => 'tag_autocomplete',
'splitter' => ',',
'sourceUrl' => $this->createUrl('Issue/tagAutoComplete'),
// Controller/Action path for action we created in step 4.
// additional javascript options for the autocomplete plugin
'options' => array(
'minLength' => '2',
),
'htmlOptions' => array(
'style' => 'height:20px;',
),
));
echo $form->error($model, 'issue_comment_id_fk');
?>
AfterSave in /model/Issue:
protected function afterSave() {
parent::afterSave();
$issue_id = Yii::app()->db->getLastInsertID();
$tag; //here I would explode the attribute retrieved by the view form
// an SQL with two placeholders ":issue_id" and ":tag_id"
if (is_array($tag))
foreach ($tag as $tag_id) {
$sql = "INSERT INTO issue_tag_map (issue_id_fk, tag_id_fk)VALUES(:issue_id,:tag_id)";
$command = Yii::app()->db->createCommand($sql);
// replace the placeholder ":issue_id" with the actual issue value
$command->bindValue(":issue_id", $issue_id, PDO::PARAM_STR);
// replace the placeholder ":tag_id" with the actual tag_id value
$command->bindValue(":tag_id", $tag_id, PDO::PARAM_STR);
$command->execute();
}
}
And this is the Auto Complete sourceUrl in the Issue model for populating the tags:
public static function tagAutoComplete($name = '') {
$sql = 'SELECT id ,tag AS label FROM tag WHERE tag LIKE :tag';
$name = $name . '%';
return Yii::app()->db->createCommand($sql)->queryAll(true, array(':tag' => $name));
actionTagAutoComplete in /controllers/IssueController:
// This function will echo a JSON object
// of this format:
// [{id:id, name: 'name'}]
function actionTagAutocomplete() {
$term = trim($_GET['term']);
if ($term != '') {
$tags = issue::tagAutoComplete($term);
echo CJSON::encode($tags);
Yii::app()->end();
}
}
EDIT
Widget in form:
<div class="row" id="checks" >
<?php
echo $form->labelEx($model, 'company',array('title'=>'File Company Distrubution; Companies can be edited by Admins'));
?>
<?php
$this->widget('application.extension.MultiComplete', array(
'model' => $model,
'attribute' => 'company',
'splitter' => ',',
'name' => 'company_autocomplete',
'sourceUrl' => $this->createUrl('becomEn/CompanyAutocomplete'),
'options' => array(
'minLength' => '1',
),
'htmlOptions' => array(
'style' => 'height:20px;',
'size' => '45',
),
));
echo $form->error($model, 'company');
?>
</div>
Update function:
$model = $this->loadModel($id);
.....
if (isset($_POST['News'])) {
$model->attributes = $_POST['News'];
$model->companies = $this->getRecordsFromAutocompleteString($_POST['News']
['company']);
......
......
getRecordsFromAutocompleteString():
public static cordsFromAutocompleteString($string) {
$string = trim($string);
$stringArray = explode(", ", $string);
$stringArray[count($stringArray) - 1] = str_replace(",", "", $stringArray[count($stringArray) - 1]);
$criteria = new CDbCriteria();
$criteria->select = 'id';
$criteria->condition = 'company =:company';
$companies = array();
foreach ($stringArray as $company) {
$criteria->params = array(':company' => $company);
$companies[] = Company::model()->find($criteria);
}
return $companies;
}
UPDATE
since the "value" porperty is not implemented properly in this extension I referred to extending this function to the model:
public function afterFind() {
//tag is the attribute used in form
$this->tag = $this->getAllTagNames();
parent::afterFind();
}
You should have a relation between Issue and Tags defined in both Issue and Tag models ( should be a many_many relation).
So in IssueController when you send the data to create or update the model Issue, you'll get the related tags (in my case I get a string like 'bug, problem, ...').
Then you need to parse this string in your controller, get the corresponding models and assigned them to the related tags.
Here's a generic example:
//In the controller's method where you add/update the record
$issue->tags = getRecordsFromAutocompleteString($_POST['autocompleteAttribute'], 'Tag', 'tag');
Here the method I'm calling:
//parse your string ang fetch the related models
public static function getRecordsFromAutocompleteString($string, $model, $field)
{
$string = trim($string);
$stringArray = explode(", ", $string);
$stringArray[count($stringArray) - 1] = str_replace(",", "", $stringArray[count($stringArray) - 1]);
return CActiveRecord::model($model)->findAllByAttributes(array($field => $stringArray));
}
So now your $issue->tags is an array containing all the related Tags object.
In your afterSave method you'll be able to do:
protected function afterSave() {
parent::afterSave();
//$issue_id = Yii::app()->db->getLastInsertID(); Don't need it, yii is already doing it
foreach ($this->tags as $tag) {
$sql = "INSERT INTO issue_tag_map (issue_id_fk, tag_id_fk)VALUES(:issue_id,:tag_id)";
$command = Yii::app()->db->createCommand($sql);
$command->bindValue(":issue_id", $this->id, PDO::PARAM_INT);
$command->bindValue(":tag_id", $tag->id, PDO::PARAM_INT);
$command->execute();
}
}
Now the above code is a basic solution. I encourage you to use activerecord-relation-behavior's extension to save the related model.
Using this extension you won't have to define anything in the afterSave method, you'll simply have to do:
$issue->tags = getRecordsFromAutocompleteString($_POST['autocompleteAttribute'], 'Tag', 'tag');
$issue->save(); // all the related models are saved by the extension, no afterSave defined!
Then you can optimize the script by fetching the id along with the tag in your autocomplete and store the selected id's in a Json array. This way you won't have to perform the sql query getRecordsFromAutocompleteString to obtain the ids. With the extension mentioned above you'll be able to do:
$issue->tags = CJSON::Decode($_POST['idTags']);//will obtain array(1, 13, ...)
$issue->save(); // all the related models are saved by the extension, the extension is handling both models and array for the relation!
Edit:
If you want to fill the autocomplete field you could define the following function:
public static function appendModelstoString($models, $fieldName)
{
$list = array();
foreach($models as $model)
{
$list[] = $model->$fieldName;
}
return implode(', ', $list);
}
You give the name of the field (in your case tag) and the list of related models and it will generate the appropriate string. Then you pass the string to the view and put it as the default value of your autocomplete field.
Answer to your edit:
In your controller you say that the companies of this model are the one that you added from the Autocomplete form:
$model->companies = $this->getRecordsFromAutocompleteString($_POST['News']
['company']);
So if the related company is not in the form it won't be saved as a related model.
You have 2 solutions:
Each time you put the already existing related model in you autocomplete field in the form before displaying it so they will be saved again as a related model and it won't disapear from the related models
$this->widget('application.extensions.multicomplete.MultiComplete', array(
'name' => 'people',
'value' => (isset($people))?$people:'',
'sourceUrl' => array('searchAutocompletePeople'),
));
In your controller before calling the getRecordsFromAutocompleteString you add the already existing models of the model.
$model->companies = array_merge(
$model->companies,
$this->getRecordsFromAutocompleteString($_POST['News']['company'])
);

Fetch the Account ID based on Primary Emailaddress in Sugar CRM

We have a website and when a record is inserted / updated we runs a script to update the sugar CRM Accounts module.
The Accounts module have a field website_id_c . When an insert occurs to DB we set the last insert ID to website_id_c of Sugar CRM and the remaining values to get inserted.
Insert script is something like below .
$name_value_field=array(
array('name' => 'name','value' => $name),
array('name' => 'primary_contactperson_c','value' => $ownername),
array('name' => 'billing_address_street','value' => $address),
array('name' => 'billing_address_postalcode','value' => $postalcode),
array('name' => 'email1','value' => $email),
array('name' => 'website_id_c','value' => $id_retrieved),
);
if(!empty($sessn_id))
{
$result=$soap->set_entry("Accounts",$name_value_field);
}
This uses the SOAP nusoap client to communicate with Sugar CRM cloud. Insertion works fine and update also.
$response = $soap->get_entry_list('Accounts',
array('id'),
"website_id_c='$websiteID'");
$account_id = $response['entry_list'][0]['id'];
But currently I want to retrieve the account_id to update the record on the basis on Email. I changed the fields with email . But I feel its stored in some other place and the primary address key is stored in the Accounts module. So first we want to fetch that id from the Email module, and then query the Accounts module on the basis of this Email address id to get the Account Values.
$response = $soap->get_entry_list('Accounts',
array('id'),
"email1='email#example.com'");
Which is the module that stores the Email address ? I have not worked on Sugar before. So feel free to ask me if you didn't get me exactly.
Thanks
The email field and its relation to accounts is stored in two other tables, so you need to retrieve the accounts with a sub select.
$response = $soap->get_entry_list(
$module = 'Accounts',
$fields = array('id'),
$query = "accounts.id IN (
SELECT eabr.bean_id
FROM email_addr_bean_rel eabr
JOIN email_addresses ea ON (ea.id = eabr.email_address_id)
WHERE
eabr.bean_module = 'Accounts'
AND eabr.deleted = 0
AND ea.email_address = 'email#example.com'
)",
$orderBy = "accounts.name",
$offset = 0,
$max = 10
);
My soap clients get_entry_list method is as
get_entry_list($module,
$fields = array(),
$query='',
$order_by='',
$offset=0,
$limit = 1000)
Make the necessary changes accordingly .