SugarCRM REST API set_relationship between Quote and ProductBundles - php

I am using SugarCRM Pro 6.5.5
I need to create a Quote and add Products to it with the REST API. All bundles > Prof the set_entry's work, and the set_relationship for ProductBoducts works fine. But, set_relationship for Quotes > ProductBundles does not work.
Here is my input for ProductBundles > Products: this works fine
{
"session":"5qklti658f0ooou135vt8fkbi4",
"module":"ProductBundles",
"module_id":"50b71673-b555-9d68-04c9-508ef9582f47",
"link_field_name":"products",
"related_ids":[
"a9615ab1-cd89-1549-f9b8-508f00c6fa84"
]
}
Here is my input for Quotes > ProductBundles: this does not work
{
"session":"jqodi1pu8u2l8basca1hhcbt27",
"module":"Quotes",
"module_id":"bc01a88a-35c9-25ed-dfac-508ef206a264",
"link_field_name":"product_bundles",
"related_ids":[
"50b71673-b555-9d68-04c9-508ef9582f47"
]
}
BUT it still returns:
{
"created":1,
"failed":0,
"deleted":0
}
But, no record is created in the product_bundle_quote table.
I have dug into the Sugar code a little, and found something interesting.
In service/core/SoapHelperWebService.php on line 735, is this:
$mod->$link_field_name->add($related_ids, $name_value_pair);
Which calls the add method in file data/Relationships/M2MRelationship.php on line 118. Interestingly, $lhsLinkName is NULL, which causes the method to return false. Here is a snippet:
public function add($lhs, $rhs, $additionalFields = array())
{
$lhsLinkName = $this->lhsLink;
$rhsLinkName = $this->rhsLink;
if (empty($lhs->$lhsLinkName) && !$lhs->load_relationship($lhsLinkName))
{
$lhsClass = get_class($lhs);
$GLOBALS['log']->fatal("could not load LHS $lhsLinkName in $lhsClass");
return false;
}
if (empty($rhs->$rhsLinkName) && !$rhs->load_relationship($rhsLinkName))
{
$rhsClass = get_class($rhs);
$GLOBALS['log']->fatal("could not load RHS $rhsLinkName in $rhsClass");
return false;
}
It returns FALSE in the first if() block, which means the record is never created.
Also, this shows up in my log:
[2139][1][FATAL] could not load LHS in ProductBundle
So yeah, I'm stuck here. I tried searching for everything I could, including the error, but I haven't found anything helpful.

This is a Sugar bug, which will be fixed in the 6.7 release. In the meantime, check out this forums post for the code fix.
http://forums.sugarcrm.com/f6/create-quote-line-items-web-service-api-83183/

This does turn out to be a Sugar Bug, but not the one in the other answer. It's actually related to this bug: Bug 32064. They were able to provide me with a custom module that I could upload and activate to address the issue before release 6.7 when this is slated to be addressed officially. You'll probably want to contact SugarCRM directly for this workaround, depending on your time frame.

I was having the same issue and in order to resolve that i added the below mentioned code in product bundles vardef and it's start working fine.
'quotes' =>
array (
'name' => 'quotes',
'type' => 'link',
'vname'=>'LBL_PRODUCT_BUNDLES',
'relationship' => 'product_bundle_quote',
'source'=>'non-db',
),
Thanks!

You have to link the quote to products too.
make sure in your quote vardef there's the following:
'products' =>
array (
'name' => 'products',
'type' => 'link',
'relationship' => 'quote_products',
'vname' => 'LBL_PRODUCTS',
'source'=>'non-db',
),
and in your webservice, set the relationship between quote and products
{
"session":$session_id,
"module":"Quotes",
"module_id":$quote_id,
"link_field_name":"products",
"related_ids":[
$product_id
]
}
It works for me.

Related

creating a variable shortcode based on user input that returns (prechosen) variables Wordpress

I need to update values in all posts of a wordpress installation on a regular base. I was gooing to use shortcodes to insert the request into the wordpress post. Then use a custom functions.php that holds all the variables that need to be updated from time to time.
I got it working. Somehow but not the way I intended to use it. I'm a total beginner. Please consider this when answering my questions.
I want to have a function that reads what comes after honda_ and displays the correct value in wordpress without having to create a separate shortcode for each variable.
When entering [honda_link] wordpress should display the value from honda_link. When entering [honda_longlink] the value from honda_longlink variable should get displayed. I don't want to create a shortcode for each value.
I came up with this as a working solution...
// Honda
function honda() {
$honda_link = 'www.honda.com';
$honda_longlink = 'http://www.honda.com';
$honda_free = 'Free';
$honda_new = '23.688 $';
$honda_mileage = '00';
return $honda_link;
}
add_shortcode('neu', 'honda_link');
I tried some approaches by using an array but it ultimately failed all the time. I also tried it with if statements but wasn't able to get the right value displayed.
Someone willing to help a noob? I think I need to see a working example in order to understand it. The code snippets I have been looking at (that do something similiar but not the same I want to achieve) did confuse me more than they helped me.
I came up with this / Which works in a way but... This isn't very comfortable to use.
add_shortcode('HONDA','HONDA_TEST');
function HONDA_TEST($atts = array(), $content = null, $tag){
shortcode_atts(array(
'var1' => 'default var1',
'var2' => false,
'var3' => false,
'var4' => false
), $atts);
if ($atts['var2'])
return 'honda2';
else if ($atts['var3'])
return 'honda3';
else if ($atts['var4'])
return 'honda4';
else
return 'honda1';
}
So now when using:
[HONDA var1="novalue"][/HONDA]
[HONDA var2="novalue"][/HONDA]
[HONDA var3="novalue"][/HONDA]
[HONDA var4="novalue"][/HONDA]
it shows:
honda1
honda2
honda3
honda4
and so on.
Is there a better way to achieve the intended goal from post #1 ? Any way I could import the $variables from 1st post in bulk for example?
I don't have a working WP setup right now to test, but could you try this:
add_shortcode('HONDA','HONDA_TEST');
function HONDA_TEST($atts = array(), $content = null, $tag){
$atts = shortcode_atts(array(
'model' => 1,
), $atts);
$myHondas = [1 => 'honda1', 'honda2', 'honda3'];
return isset($myHondas[$atts['model']]) ? $myHondas[$atts['model']] : 'unknown model id';
}
And use it with [HONDA model="1"], [HONDA model="2"]

Preventing Form Validation Callback Issues Codeigniter

this is my form_validation.php
$config = array(
'buy' =>array(
array(
'field' => 'id',
'label' => 'Item',
'rules' => 'trim|required'
),
array(
'field' => 'qty',
'label' => 'Quantity',
'rules' => 'trim|greater_than[0]|callback_validate_qty'
)
)
);
My Controller to load form validation :
$this->load->library('form_validation');
Function validate_qty($qty)
$item_id = $this->input->post('id');
$total_item= $this->item_model->getTotalItem($item_id);
if ($qty >= $total_item) {
$this->form_validation->set_message('validate_qty', 'Cant Buy More than maximum stock!');
return FALSE;
}
this validation is work perfectly, but someone has got throught it, he buy more than maximum stock..
How to prevent this? I don't know how he does that, but it looks like the validation didn't work for him. When I tested, it works perfectly, I don't know what's wrong with it, maybe he uses software or anything? is there any solution? Thanks
i think your code is correct.
i have one doubt on that code.
check the variable you used in if condition is correct or not ($qty). becuase you not show the full code.i don't known how that variable comes.
Also change the code like if and else condition.
if ($qty >= $total_item) {
$this->form_validation->set_message('validate_qty', 'Cant Buy More than maximum stock!');
return false;
} else {
return true;
}
Assume if you have 30 stock in your hand and you want to get all 30 stocks out. But according to your if condition (if ($qty >= $total_item) {) it throws error.
Fix 1
if should be > (if ($qty > $total_item) {).
Fix 2
Typo - in your callback function you've used two underscores _. Get rid of one
callback__validate_qty
^^
Read more about codeigniter.com - Callbacks

Getresponse API 2 (Adding Custom fields and contacts using PHP)

Im new to coding and web development as it is and diving into the deep end with API's is a thing i wish i never had done! However being said i have progressed further than expected. I am now having problems when trying to add custom fields to the add contact feature. Im trying to get the code to add the hidden form input fields when the user hits my thankyou page. I dont want to use Getresponses own Form builder for my main page so it was better to use the API. I have the code running perfectly when it comes to just adding the contact however when i add the set_contact_customs the code does not execute and fails with the following error: (Request have return error: Array) So i understand its to do with the set_contact_customs array however im clueless as to what it is i have done wrong.. Any advice and help is greatly appreciated as i am still learning the basics so picking up on what you experts say is a great learning curve. Thanks.
--- Below is the working version without the set_contact_customs ----
<?php
// Add contact to selected campaign id
try{
$result_contact = $client->add_contact(
$api_key,
array (
'campaign' => 'My-Camp-ID',
'name' => $fullname,
'email' => $emailaddress
)
);
echo "<p style='color: blue; font-size:24px;'>No Errors, Contact and Custom Fields have been added...</p>";
}
catch (Exception $e) {
echo $e->getMessage();
}
?>
--- Here is the code that causes the problems (with set_contact_customs) ----
<?php
// Add contact to selected campaign id
try{
$result_contact = $client->add_contact(
$api_key,
array (
'campaign' => 'My-Camp-ID',
'name' => $fullname,
'email' => $emailaddress
)
);
$result_contact = $client->set_contact_customs(
$api_key,
array(
'Survey Type' => $surveytype,
'Survey Cost' => $surveycost
)
);
echo "<p style='color: blue; font-size:24px;'> Contact Added </p>";
}
catch (Exception $e) {
echo $e->getMessage();
}
?>
API 2 doesn't really exist: in GetResponse they say version "1.5.0 - this is last JSON/RPC version of our API", especially if you were speaking 10 months ago. Now they are preparing to beta-test v3. So I will assume you were speaking about 1.5 and answer about it (I'm not familiar with v3, maybe there it's different).
You must send contact id with set_contact_customs, and you didn't.
When it says, "request error: array", it doesn't relate to your array (even though the problem is in your array, because you don't send in it contact id), they are sending an array as a response with error messages.
I'd love to tell you, where to get the contact id in order to send it, but I'm looking for it myself now. :)
UPDATE:
Ok, I combined it from pieces all over the internet, and now here's the working format.
You don't need to add_contact and then update it, you can do it in one go, adding the 'customs' parameter to the add_contact call (GR say, that we shouldn't expect for the contact to be added immediately, so you might not even get whom to update, if you call that function right away).
The fields for add_contact are described here.
The 'customs' parameter should look differently. Instead of:
array(
'Survey Type' => $surveytype,
'Survey Cost' => $surveycost
)
it should be:
array(
array( 'name' => 'Survey Type', 'content' => $surveytype ),
array( 'name' => 'Survey Cost', 'content' => $surveycost )
)
By the way, from what I tested, - blessedly, you don't need to define in GR UI those custom fields first, whatever you send, will be added or updated (in their limits for the custom field names and values).
I got error, when tried to send one custom field with empty content, when calling add_contact. When I sent it with set_contact_customs, I didn't get any error; I wanted to see, if it would delete the field or field value - it didn't do a thing.
If you still wish to update the existing contact, here's how to send the contact id with the update call:
$result = $client->set_contact_customs(
$api_key, array(
'contact' => $contact_id,
'customs' => $custom_fields_array
)
);
To first find contact id, you should call get_contacts. And since it's been said (I haven't tested it), that in different campaigns contacts with the same email address have different contact id, you should pass both the campaign, and the email with it.
As you can see, campaign can be sent in 'campaigns' parameter (then campaign id, that you got for add_contact, should be used), or in 'get_campaigns' (then the campaign name or even prefix can be used).
Here's the call with campaign id, for your code:
$result = $client->get_contacts(
$api_key, array(
'campaigns' => array( 'My-Camp-ID' ),
'email' => array( 'EQUALS' => $emailaddress )
)
);
To retrieve contact id from get_contacts, do the same as recommended for retrieving campaign id:
$contact_id = array_pop( array_keys( $result ) );
if ( empty( $contact_id ) ) {
//still not ok
}
else {
//you can call set_contact_customs
}
In order for that error message to be more descriptive, instead of just 'Request have return error: Array', open your jsonRPCClient.php, which you most surely include in your file with these GR function calls, and look for the following line:
!is_null($response['error']) => 'Request have return error: ' . $response['error'],
and replace it with the following, at least:
!is_null($response['error']) => 'Request have returned error: ' . var_export($response['error'], true),
Now your code will use the beloved var_export function and if you make a mistake, you will see in your error log something like:
Request have returned error: array (
'message' => 'Invalid params',
'code' => -32602,
)
I dedicate this thorough answer to all those, who helped me endlessly here on StackOverflow, just giving their answers to someone else's questions, sometimes years ago. Thank you! Hopefully my answer will save someone time, efforts, and mood, too. :)

assign customer to multiple customer groups to magento

Hello I want to assign multiple groups to particular customer like "Rajat the customer" belogs to "Wholesale,retailer,electric". actually I saw the same thread on Multiple customer groups per customer but it is not helpful does there any update to make this change happen.
I am stuck what should I do because there aren't any extension available with the same functionality?
I found the solution,
First of all go to database and click on the eav_attribute and then search for group_id in the attribute code field and edit this record.
now Step 1:-
change frontend_input from select to multiselect.
step 2:-
change backend_type from static to varchar.
although it is not standard way but it worked for me. :)
PS. I'm using magento 1.7.0.2 community version.
Rajat Modi's solution worked pretty well for me thank you but doing this did break the display of the groups column on the customer grid if more than one is selected, plus broke the ability to filter customers by group.
To fix that, create this file to use as the renderer for customer groups: /app/code/local/Mage/Adminhtml/Block/Customer/Renderer/Group.php
<?php
class Mage_Adminhtml_Block_Customer_Renderer_Group
extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
public function render(Varien_Object $row)
{
$value = $row->getData($this->getColumn()->getIndex());
$groups = Mage::getModel('customer/group')->getCollection()
->addFieldToFilter('customer_group_id', array('in' => explode(',', $value)));
$groupNames = array();
foreach ($groups as $group)
{
$groupNames[] = $group->getCustomerGroupCode();
}
return implode(', ', $groupNames);
}
}
Then override /app/code/core/Mage/Adminhtml/Block/Customer/Grid.phpenter code here (copy it to /app/code/local/Mage/Adminhtml/Block/Customer/Grid.php)
In the _prepareColumns() function, change this (around line 95):
$this->addColumn('group', array(
'header' => Mage::helper('customer')->__('Group'),
'width' => '100',
'index' => 'group_id',
'type' => 'options',
'options' => $groups,
));
to this:
$this->addColumn('group_id', array(
'header' => Mage::helper('customer')->__('Group'),
'width' => '100',
'index' => 'group_id',
'type' => 'options',
'options' => $groups,
'renderer' => 'Mage_Adminhtml_Block_Customer_Renderer_Group',
'filter_condition_callback' => array($this, '_filterGroupCondition')
));
then it will use that class for rendering groups on the grid.
Also in the _prepareCollection() function, around line 52 find ->addAttributeToSelect('group_id') and add after: ->addAttributeToSelect('customer_group_id')
Having multiple groups per customer also seems to interfere with tiered pricing (where a product has a different price depending on customer group). To fix that on the frontend display...
Fix for customer group-based product pricing tiers when calculating on the front-end:
In /app/code/core/Mage/Catalog/Model/Product/Type/Price.php
Around line 138, FIND:
$customerGroup = $this->_getCustomerGroupId($product);
ADD AFTER:
$customerGroups = explode(',',$customerGroup);
FIND:
if ($groupPrice['cust_group'] == $customerGroup && $groupPrice['website_price'] < $matchedPrice) {
REPLACE WITH:
if (in_array($groupPrice['cust_group'],$customerGroups) && $groupPrice['website_price'] < $matchedPrice) {
Do the same thing in /app/code/core/Mage/Bundle/Model/Product/Price.php if you use bundles.
I do not yet have a fix for displaying the customer group tier price when creating an order or reorder from the backend dashboard - they just show up the standard product prices.
Finally, when figuring all this out we did have some instances where mgnt_catalog_product_entity_group_price got emptied and I'm still not sure why it happened, so do make sure to take backups. For that table I restored it from an SQL backup, but re-indexing things and maybe flushing the Magento cache is also often required when getting into this stuff.
When doing things such as searching for customers by group programmatically in your own scripts or modules you may have to take into account that it is now a multiselect for example by doing things like this:
$allowedGroups = array(
array(
"finset" => array(10)
),
array(
"finset" => array(42)
)
);
$collection = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('group_id', $allowedGroups);
Although I'm not sure that that piece of code will work right until all the customers have rows in the mgnt_customer_entity_varchar table which is where the new value for a customer's groups is stored when there are more than one group selected. Only a single group ID remains stored in mgnt_customer_entity as that field isn't varchar.
For that reason be aware that yes it can affect modules which extend or use the functionality of customer groups.

php sprintf not working as expected

$actions = array(
'EDIT' => sprintf('Edit',
'abf_cm',
'edit_course',
$item['course_id'],
'thickbox edit-box',
'edit_'.$item['course_id']
),
'DELETE' => sprintf('Delete','course_management','do_process','delete',$item['course_id']),
);
In doing so, the edit part is not being displayed.Am i doing anything wrong.
I also tried using the placeholders
'EDIT' => sprintf('Edit',
'abf_cm',
'edit_course',
$item['course_id'],
'thickbox edit-box',
'edit_'.$item['course_id']
),
but still no results. I also noticed that when i remove the class and id attributes in the earlier version, then it works fine.
Can you please give me a satisfactory explanation of this and tell me where am i doing wrong.
EDIT:
Im using this inside Wordpress for creating custom table using WP_List_Table class
function column_course_name($item ) {
//Build row actions
$actions = array(
'EDIT' => sprintf('Edit',
'abf_cm',
'edit_course',
$item['course_id'],
'thickbox edit-box',
'edit_'.$item['course_id']
),
'DELETE' => sprintf('Delete','book_management','do_process','delete',$item['course_id']),
);
//Return the title contents
return sprintf('%1$s%3$s',
/*$1%s*/ strlen($item['course_name'])>0?$item['course_name']:'<span style="color:silver">(No Name)</span>',
/*$2%s*/ $item['course_id'],
/*$3%s*/ $this->row_actions($actions) //row_actions is a method in this class
);
}
update:
Well, its strange to mention but the code works when i use a single class( ie when i delete the space between the two classes for the tag) .
Any thoughts?
Dipesh, maybe you have errors in the code around this snippet.
Try to check your code in isolation. I copied your code to the separate .php script with little set-up and checked $actions array with print_r, like this:
edit_array.php
<?php
$item = array();
$item['course_id'] = 1;
$actions = array(
'EDIT' => sprintf('Edit',
'abf_cm',
'edit_course',
$item['course_id'],
'thickbox edit-box',
'edit_'.$item['course_id']
),
'DELETE' => sprintf('Delete','course_management','do_process','delete',$item['course_id']),
);
print_r($actions);
I ran this script from console and got the following results:
$ php edit_array.php
Array
(
[EDIT] => Edit
[DELETE] => Delete
)
Generated link for $actions['EDIT'] is HTML valid, so one can safely conclude that your code itself is working fine, and error lies somewhere else.

Categories