SugarCRM SOAP API Examples
Thursday, April 24. 2008
Thanks! I’ve been looking for some good sugar soap examples. I look forward to seeing what else you come up with in the future.
Jeremy, thanks for dropping by – I will certainly be adding things as I find them. I don’t work with sugarcrm all the time but when I do, I’ll be posting :)
Thanks for this LornaJame. This just look like what we are looking for.
Cheers,
Nigel
Nigel: Glad to help – thanks for dropping by and commenting :)
thank you! would love to see more examples to help me grasp this concept… appreciate the insight :)
Darcy: Next time I am working with sugarcrm and SOAP, I’ll see what I can add to this. There is nothing like a working code example when you’re just getting to grips with something, I know!
Wow! Thank you for this sample. It’s exactly what I’ve been looking for. It’ a shame that Sugar doesn’t have simple, clear examples like this.
Josh: I’m very happy to hear these examples helped – once I’d managed to get mine working it seemed important to share! I know sugarcrm are doing more with their services though so watch for better things coming from them in the future
I’m just commenting ‘cause you told me to, not ‘cause I’m brilliant or anything.
You often have to fully qualify fields for Sugar SOAPiness to work, e.g.
$query = " username_c like ‘$keyword‘ or first_name like ‘$keyword‘ "; //fails unhelpfully saying "Looks like we got no XML document.. "
$query = " username_c like ‘$keyword‘ or `contacts`.first_name like ‘$keyword‘ "; //works!
mwic: If only brilliant people were allowed to comment, it would be much quieter around here!! Thanks for adding your findings, hopefully they’ll be helpful to the next person who comes here looking for info.
your examples are great, and i’ve got my soap connection working remotely with my server here in house—that is, i get a response from my query, so i know it’s making some connection to sugar, but i’m getting an invalid session ID error. it seems this is caused by not logging in or something… any thoughts? i didn’t see an authentication example using regular soap… all of sugar’s use nusoap… boo…
thanks!
Hi Dean, came across this post last night and had your problem. I lazily copy/pasted the above code and dropped a var_dump in to inspect the results of the SOAP call. You need to log into sugar first before you can make these calls, and you can do so with:
$session_id = $client->login(array(
‘user_name’=>‘bob’,
‘password’=>‘password’
),FILE)->id;
Then (assuming your credentials are correct) you have a $session_id to pass into other calls.
I’m writing more examples for the community based off of what you have here.
Check out
http://deanhaddock.com/mercury/2009/04/10/sugarcrm-soap-examples/
Hey , i am trying to pass , queries via browser , to soap server , what changes do i need to make in server?
Ritesh: to consume a soap service, you will need a soap client of some kind. Until the new sugarcrm version is out with rest services, a browser can’t talk to the services. Hope that helps
If you get this error:
Fatal error: Cannot use object of type stdClass as array
when trying to read the result as an array, use this function.
function objectToArray( $object )
{
if( !is_object( $object ) && !is_array( $object ) )
{
return $object;
}
if( is_object( $object ) )
{
$object = get_object_vars( $object );
}
return array_map( ‘objectToArray’, $object );
}
this will convert the object returned by soap api to an array.
No need to convert an object to an array to use it, just use the correct syntax.
E.g. echo $result->name
Great blog!
I’m trying to update a custom value on an existing Account in sugar but without progress. This is what I try:
set_entry($sess, ‘Accounts’, array( array("name" => ‘id’,"value" => $TheId), array("name" => ‘customfield’,"value" => $updatedvalue) ));
Could you please help!
Thank you!
Mike
Mike: do you get an error message or does the field just not update? I’m not sure if I’ve tried custom fields with the services; can you update a "normal" field?
Thanks for your answer!
Yes I can update normal fields and one of my custom fields… Well, maybe it’s better to take a look at the command retrieving the data first. It looks like this:
$result = $this->proxy->get_entry_list( $this->sess, ‘Accounts’, $query, $orderby, 0, array( ‘openx_c’, ‘customfield_c’, ‘id’, ‘name’ ), $maxnum, false );
I’m trying to retrieve two custom fields: customfield_c and openx_c. The mystery is that I can retrieve customfield_c, but not openx_c.
Any ideas?
Regards,
Mike
I don’t know if this is still monitored, but I could really use some help – this stuff is driving me NUTS! I am not getting back any info and I know that there are 3 records that would match. Also, when I put in information – it’s not entering the email address. Here’s my code:
require_once(‘inc/nusoap/nusoap.php’);
class SugarSoap{ var $proxy; var $sess;
function SugarSoap($soap_url,$login=true){
$soapclient = new soapclient($soap_url,true);
$this->proxy = $soapclient->getProxy();
if($login) $this->login();
}
function login(){
$params = array(
‘user_name’ => ‘uname’,
‘password’ => md5(‘password’),
‘version’ => ‘.01’
);
$result = $this->proxy->login($params,‘MyApp’);
$this->sess= $result[‘error’][‘number’]==0 ? $result[‘id’] : null;
return $this->sess;
}
function getContacts($query=’‘,$maxnum=5,$orderby=’ last_name asc’){
$soap=new SugarSoap(‘http://agame.sugarondemand.com/soap.php?wsdl’); // we automatically log in
$result=$soap->getContacts(’ email1 like "%"’,5," last_name desc");
print_r($result);
if($result[‘result_count’]>0){
foreach($result[‘entry_list’] as $record){
$array= $soap->nameValuePairToSimpleArray($record[‘name_value_list’]);
echo $array[‘first_name’] . " " . $array[‘last_name’] . " – " . $array[‘email1’]. "";
}
} else {
echo "No contacts found";
}
$result=$soap->createUser(‘email@gmail.com’,‘Lastname’,‘Firstname’,‘Employer’,‘Somewhere’,‘Campaign’);
sandi—
check out some of my examples at http://systemsconsciousness.com/?s=sugarcrm+soap
the one titled ‘sugarcrm soap examples’ has some code that will hopefully help you.
dean
This is great! Sandi: thanks for your comment. Dean: thanks for your very helpful reply! I don’t have any specific advice other than if one field doesn’t save, double- and triple-check your field names for that field. For the missing records, start will a full list and add criteria one at a time, until you see which one causes your records to disappear. Good luck :)
Hi People !
Thanks a LOT LORNAJANE !
To get the session ! take this PHP NATIVE SOAP
http://www.sugarcrm.com/forums/showpost.php?p=112331&postcount=5



Since SugarCRM only works with PHP5 in non- WSDL mode, I had to create all my calls from scratch. The SugarCRM documentation is somewhat minimal, as I may have mentioned, and I found I was mostly reading the WSDL to figure out how to format my SOAP calls.
Tracked: Apr 24, 16:34
Sugar CRM Dev Links
Tracked: Nov 17, 01:50