SugarCRM REST API, how to get Contacts related to an Account - php

I am using SugarCRM Pro 6.5.5. I am trying to do some integration into another application, so I need to do some API calls from that other application. I am using the REST API v2. At this point, I need to get a Contact that is related to an Account via the account ID. I have tried both get_relationships() and get_entry_list(), but I can't get either of them to work.
Here's my input for get_relationships():
{"session":"eujfbfsfjgni98m0mivl6jm6r2","module_name":"Accounts","module_id":"c03d0649-0525-2f90-1206-50881e87d7dd","link_field_name":"contacts"}
I have tried many variations of this, and several other options, but nothing did what I wanted. This is the output from above:
stdClass Object
(
[entry_list] => Array
(
[0] => stdClass Object
(
[id] =>
[module_name] => Contacts
[name_value_list] => stdClass Object
(
[deleted] => stdClass Object
(
[name] => deleted
[value] => 0
)
[do_not_call] => stdClass Object
(
[name] => do_not_call
[value] => 0
)
)
)
)
[relationship_list] => Array
(
)
)
I don't see how this information could ever be useful.
Next up, I tried get_entry_list() with a query to select only the Contacts that had the given account ID. Here's my input for that:
{"session":"8mol33e7kdq6girugu30dnt074","module_name":"Contacts","query":"accounts.id = 'c03d0649-0525-2f90-1206-50881e87d7dd'"}
I am using accounts.id because I have read in similar questions, that is correct. I've tried many other things, like: contacts.account_id, account_id, etc. In every case, I get a MySQL error in the log stating the column is invalid.
SugarCRM: how to get all contacts for an account via REST API
This question is relevant, and seems to be the exact same problem. I tried adding a link_name_to_fields_array option to my input, but no matter what value I provide, it results in the following MySQL error in the log:
AND jt11.deleted=0 where (Array) AND contacts.deleted=0: MySQL error 1054: Unknown column 'Array' in 'where clause'
Dafuq?
Thanks.

Thanks to ychaouche, I figured it out.
I was able to use the get_relationships() method after all. I had tried before adding the related_fields to my input, but all that did was result in an empty result set. The answer? Set related_module_query to an empty string... and now the result set is not empty. Yep, my 3+ hours of furious Googleing last night is because of that.
So, here is some working code in case anyone else has this problem:
{
"session": "iov9pg9kvvl60librung1h5fh6",
"module_name": "Accounts",
"module_id": "c03d0649-0525-2f90-1206-50881e87d7dd",
"link_field_name": "contacts",
"related_module_query": "",
"related_fields": [
"first_name",
"last_name"
],
"deleted": false
}
This got me the following result set:
stdClass Object
(
[entry_list] => Array
(
[0] => stdClass Object
(
[id] =>
[module_name] => Contacts
[name_value_list] => stdClass Object
(
[first_name] => stdClass Object
(
[name] => first_name
[value] => John
)
[last_name] => stdClass Object
(
[name] => last_name
[value] => Smith
)
)
)
)
[relationship_list] => Array
(
)
)
You can of course specify different fields in related_fields and get different pieces of data.

Take a look at sugarcrm/tests/service/RESTAPI4Test.php. It's got plenty of examples on how to use the REST API. It's a common practice to look at test code for code examples on how to use a library/framework/API. Tests are in this sense a very good documentation.
Hope that helps.

Related

Codeigniter Query Doesnot Give Proper Answer

I have a web project that is under development. I want to select data from database by group having a condition. But the problem is CodeIgniter doesn't give me the actually expected result. For reference, I am sharing code below.
First query
$this->db->select('serial, wardno, COUNT(serial) as total');
$this->db->where('status','active');
$this->db->group_by('wardno');
$data['wards'] = $this->db->get('certi_charter_inherit')->result_object();
First query give doesnot give me the targated result. It generates result.
[wards] => Array
(
[0] => stdClass Object
(
[serial] => 6
[wardno] => 2
[total] => 2
)
)
This is not my expectation. But If I use another direct query then it works clearly.
Second Query
$data['wards'] = $this->db->query("SELECT serial, wardno, COUNT(serial) as 'total' from certi_trade WHERE status='active' GROUP by wardno")->result_object();
[wards] => Array
(
[0] => stdClass Object
(
[serial] => 3
[wardno] => 1
[total] => 2
)
[1] => stdClass Object
(
[serial] => 6
[wardno] => 2
[total] => 1
)
)
The second query generates the result like below. And it is correct. I have tested my phpmyadmin console. So I confused which one should be used. I like the first one.
Any help is highly appriciated.
Please change your table name here as per your second query :
$data['wards'] = $this->db->get('certi_trade')->result_object();

Facebook Webhooks API - How to get campaign from leadgen_id?

I'm using Facebooks Webhooks for lead generations. I successfully can fetch leadgen_id from the Facebooks callback.
So this is what Facebook returns for the leadgen field:
Array
(
[entry] => Array
(
[0] => Array
(
[changes] => Array
(
[0] => Array
(
[field] => leadgen
[value] => Array
(
[ad_id] => 0
[form_id] => 1109138149146076
[leadgen_id] => 1109157429144148
[created_time] => 1467887375
[page_id] => 152161181508654
[adgroup_id] => 0
)
)
)
[id] => 152161181508654
[time] => 1467887376
)
)
[object] => page
)
Is it possible to somehow get campaign ID from these values that Facebook returns?
There is no clue to campaign in leadgen itself and in the form that I fetched with leadgen_id and form_id.
I also tried to fetch all ad account campaigns with
/v2.6/<ad_account_id>/campaigns
endpoint and thought that I will see some connection there with form or leadgen, but nothing there as well.
So, I need to fetch it so that I can group leadgen forms in Facebook. With current implementation, unfortunately it is not possible - I guess Facebook didn't thought of that :(
Just in case someone will struggle with this as well:
We can grab ad id info from:
'https://graph.facebook.com/v2.6/<AD_ID>?access_token=<ACCESS_TOKEN>&fields=campaign_id';
And this will return us campaign id.
Reason why I didn't try this in first place, was because in initial response, Facebook returned ad_id as 0, but after some while I realised that this is probably because it was a test lead from https://developers.facebook.com/tools/lead-ads-testing

How to reformat this array without knowing the keys in advance

I'm querying an API and getting a response back with various countries. Here is the relevant array I'm working with and what it prints out.
print_r($apiResponse['response']['data'][0]['countries']);
prints this:
Array ( [US] => Array ( [id] => 840 [code] => US [name] => United States [regions] => Array ( ) ) [CA] => Array ( [id] => 124 [code] => CA [name] => Canada [regions] => Array ( ) ) )
I am looking to save an array of only the two character country codes from that data. The only thing is the key is unknown to me when I query it so I don't know how to access the [code] section of it to save it to my new array.
I want to end up being able to take whatever amount of countries the API sends back and save the two character codes in a format like this:
'country_codes' => array('US','CA','UK','AU')
Thanks for your help!
Use the array_keys() function. Here you have the documentation.

foursquare api hereNow list checked in people without being checked in yourself

I'm trying to get the list of users that are checked in a venue.
using following url (php):
$url = 'https://api.foursquare.com/v2/venues/5576e64f498e907e749435d4/herenow?client_id=' . $fsClientId
. '&oauth_token=' . $_SESSION['foursquareToken']->access_token . '&v=20130815';
If i'm checked in to the same venue I get a list of people who are checked in to the venue. If i'm not checked in there's an empty "items" object(I've json_decoded it to an object):
stdClass Object ( [meta] => stdClass Object ( [code] => 200 ) [notifications] => Array ( [0] => stdClass Object ( [type] => notificationTray [item] => stdClass Object ( [unreadCount] => 0 ) ) ) [response] => stdClass Object ( [hereNow] => stdClass Object ( [count] => 1 [items] => Array ( ) ) ) )
Is it possible to list people checked in to a venue without being checked in to it yourself?
I've read somewhere you have to be venue-owner for this being able to work. Is that correct?
How does one become venue-owner in that case?
Or is it truely not possible at all?
Thanks in advance.
MackDoms
Is it possible to list people checked in to a venue without being checked in to it yourself?
Yes, it is possible. You do not need to check in there. However, a list that you can see, the only friends of your friends or friends.
https://developer.foursquare.com/docs/venues/herenow
Returns a list of friends and friends-of-friends at the venue for authenticated requests. If the acting user is currently checked in to the venue, then a list of other users is also returned.

how to get to a specific part of this nested array/object

This is driving me crazy, I am trying to get to a specific part of this object and it is driving me crazy, here is the object contents:
XMLHandler Object
(
[doc:XMLHandler:private] => SimpleXMLElement Object
(
[#attributes] => Array
(
[state] => Live
)
[newsListItem] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[href] => http://api.contentplus.co.uk/6cb5ea15-d6b1-4c40-9db7-cb2a3315080b/news/800773226/
)
[id] => 800773226
[publishDate] => 2011-10-24T10:04:49
[lastModifiedDate] => 2011-10-24T11:20:40
[headline] => Relationships matter on social media
)
)
)
[format] => html
)
I want to get the value of [id] I am trying to access it like this:
echo $niList->doc->newsListItem[0]->id;
but this is giving me nothing, I know I am close (well I hope I am) but I just cant quite get it right, could anyone help please.
Thanks all.
Your object dump says
doc:XMLHandler:private
which means doc is a private property of XMLHandler. As such, you can only access it from within that object via $this. But you are trying to access it from outside the object when you do
echo $niList->newsListItem[0]->id;
This wont work. Add a method to that XMLHandler object that does what you want to do with that newslistitem id. Also see the chapter on Visibility in the PHP Manual:
http://docs.php.net/manual/en/language.oop5.visibility.php

Categories