If you use SugarCRM, one thing that you’ll inevitably need in this time of integrating web services, platforms and devices is the ability to send data from a web form into SugarCRM. You can use SOAP for this, and it works well, for the most part. But you’ll find there are very few good examples out there on the web to really clarify and demystify a clean SugarCRM/SOAP implementation.
The best example I have found is here: http://www.lornajane.net/posts/2008/SugarCRM-SOAP-API-Examples. LornaJane’s examples were an excellent start, and I thought I’d go ahead and provide you some more details. The upside of these examples is that you won’t need to install nusoap in order for them to work. If you like what you see, and you want to see more, please consider visiting one of our sponsors. Each click makes us a few cents.
Setup: LAMP web server running the latest version of WordPress with tons of plugins and customization. I also use cForms. It’s not important that you are using WordPress or that you have cForms installed. I just happen to like cForms, and I think it’s a fun challenge to integrate all these different systems. If you do use WP and cForms, just follow the instructions in the cForms documentation for customizing my-functions.php in your plugins/cforms-custom directory. The only real difference will be that you will use $POSTdata['varname'] instead of the regular $_POST['varname']. You’ll also need to use stripslashes() and pay attention to the variable names that cForms passes. I won’t be going into the specific details of cForms-to-SugarCRM here, but you should be able to infer how to implement that from this explanation.
This example uses a simple web form POST calling a PHP document as an action. For simplicity, let’s assume you have a web form with the following fields: first_name, last_name, email_address, phone_number and message.
When you submit the form, it calls “form-action.php”. You will implement the examples here in this PHP file.
Getting Started
To get started, let’s just get a basic example going. Copy and paste the following code in your form-action.php document. If you want to look at the SugarCRM documentation for these SOAP calls, go here: http://sugarcrm.openapp.org/2006/04/24/using-sugarcrm-soap/ Part of the reason I’m writing this post is because they don’t provide any examples for these calls. Once you get the hang of it though, that link will be quite helpful to you.
<?php
// set up options array
$options = array(
“location” => ‘http://your.website.here/sugarcrm/soap.php’,
“uri” => ‘http://www.sugarcrm.com/sugarcrm’,
“trace” => 1
);//user authentication array
$user_auth = array(
“user_name” => ‘YOUR_SUGAR_USERNAME’,
“password” => MD5(’YOUR_SUGAR_PASSWORD’),
“version” => ‘.01′
);// connect to soap server
$client = new SoapClient(NULL, $options);// Login to SugarCRM
$response = $client->login($user_auth,’test’);$session_id = $response->id;
// look what modules sugar exposes
$response = $client->get_available_modules($session_id);// look in more detail at the fields in a module
//$response = $client->get_module_fields($session_id, ‘Accounts’);var_dump($response);
?>
So what’s going on here? Your options array is storing some variables that you’ll use a few lines later in the SoapClient call. The options array is where you specify the connection parameters to the server that has your SugarCRM installation. Naturally, you’ll want to make sure you enable port 80 access through your router/firewall because that’s the default port for HTTP and also SOAP. The next array of interest is $user_auth, which is where you specify your user name, password and SOAP version. After that you pass the connection options into the SoapClient() call and then log in via the login() call. The ‘test’ parameter is a required application name, which can be most anything you want to call it. Once you pass the authentication, the $session_id variable grabs the session id for your current SOAP connection. You’ll need this with most, if not all, calls that you make from this point forward.
The next few lines of code are the fun part. The “$response = $client->get_available_modules($session_id);” line is grabbing all of the modules in your SugarCRM installation. This is very useful, for instance, when you want to know if the particular module you are attempting to write to is exposed. It’s also helpful when you’re trying to debug your SOAP calls and you’ve used the wrong module name, and so on. Below that is another example call that, if you uncomment the line, will return all of the fields that are exposed in a specific module, in this example–Accounts. Finally, var_dump($response) echos out whatever your $response variable has collected above.
Play around with it and get a feel for how it works. Notice that any custom modules you create show up in the list of available modules. Notice also that some modules, like Users, don’t return any available fields at all.
Inserting a Record
Now that you’ve made your first SOAP call to SugarCRM, you’ll want to try to insert a record or two into a module. This, like the previous example, is pretty simple and straight forward, but there are a few quirks like anything else in SugarCRM.
Let’s take the previous example to the next level. Under your $session_id variable declaration, add this line: “$user_id = $client->get_user_id($session_id);” You’ll need this eventually to assign a given record to a user. In this case we’re going to use the authenticated user’s id. Now the fun part! Remove or comment out the get_available_modules and get_module_fields lines and replace them with this:
$response = $client->set_entry($session_id, ‘Leads’, array(
array(”name” => ‘first_name’,”value” => $_POST['first_name']),
array(”name” => ‘last_name’,”value” => $_POST['last_name']),
array(”name” => ‘email1′,”value” => $_POST['email_address']),
array(”name” => ‘work_phone’,”value” => $_POST['phone_number']),
array(”name” => ‘description’,”value” => $_POST['message']),
array(”name” => ‘assigned_user_id’,”value” => $user_id)
));
Here, we’re inserting the data passed from our form into the Leads module in SugarCRM. One thing you may notice instantly is that some of the variable names for the SugarCRM module have different names than the POSTed variables from our form. The email address, for instance, gets inserted into the email1 field for SugarCRM. This is because SugarCRM has its own way of dealing with multiple (primary, secondary, etc.) email addresses. The $_POST['message'] variable is being inserted into the description field in the Leads module for simplicity because SugarCRM includes a description field in this module by default. The last line inserts the authenticated user’s id into the record and associates your new Lead with this user. This can be really helpful, say, if you have a team of people using SugarCRM to correspond with clients, and each person deals with a different type of incomming Lead. Your form-action.php file should now look something like this:
<?php
// set up options array
$options = array(
“location” => ‘http://your.website.here/sugarcrm/soap.php’,
“uri” => ‘http://www.sugarcrm.com/sugarcrm’,
“trace” => 1
);//user authentication array
$user_auth = array(
“user_name” => ‘YOUR_SUGAR_USERNAME’,
“password” => MD5(’YOUR_SUGAR_PASSWORD’),
“version” => ‘.01′
);// connect to soap server
$client = new SoapClient(NULL, $options);// Login to SugarCRM
$response = $client->login($user_auth,’test’);$session_id = $response->id;
$user_id = $client->get_user_id($session_id);// write our form data to the Leads module
$response = $client->set_entry($session_id, ‘Leads’, array(
array(”name” => ‘first_name’,”value” => $_POST['first_name']),
array(”name” => ‘last_name’,”value” => $_POST['last_name']),
array(”name” => ‘email1′,”value” => $_POST['email_address']),
array(”name” => ‘work_phone’,”value” => $_POST['phone_number']),
array(”name” => ‘description’,”value” => $_POST['message']),
array(”name” => ‘assigned_user_id’,”value” => $user_id)
));var_dump($response);
?>
When you execute this code, you get something that looks like this:
object(stdClass)#61 (2) { ["id"]=> string(36) “7e084ce3-8ccb-1a73-4e82-49df645ff06a” ["error"]=> object(stdClass)#58 (3) { ["number"]=> string(1) “0″ ["name"]=> string(8) “No Error” ["description"]=> string(8) “No Error” } }
Believe it or not, this means it worked. More evidence that it worked will be when the record pops up in your SugarCRM Leads module and you scream eureka and everyone in your office turns around to stare at you. When you’re ready to publish your form to a live website, just comment out the var_dump line and add something like, echo “Your form worked, and I’m awesome.”;.
Relating Two Records in Different Modules
This example was a little tricky to figure out, but ultimately it’s pretty simple. You will inevitably need to relate a record with another record, say, for example, if you’re using the ITHelpDesk module created by the good folks at StoryCorps. You could easily create a web form for your users to enter a support request on your company’s intranet site and have it go straight into the ITHelpDesk module. You’d just need to backtrack through these examples a bit to get the right module and field names, and then tweak the examples provided herein to write the appropriate data to the appropriate places. Then you could append the following bit of code to associate, say, the Support record with the Inventory record as its entered into SugarCRM.
But for simplicity sake, we’re just going to create an Account record and a Contact record and relate them instead of creating a Lead. Start by adding another field to your form, called organization_name. Then refer back to the previous example and edit the Leads insert example to insert into the Contacts table instead. Copy and paste the Contacts $response insert lines and modify it to add a record to the Accounts table. For this part, just add the organization_name variable to the Accounts module, which is stored as the “name” field in SugarCRM. If you stop at this point, you’ll just have a new account and a new contact in SugarCRM, but they won’t be related. They also won’t be related if you just put the organization_name in as the associated account in your Contacts $response insert. Don’t waste your juice trying to do this. Instead, you’ll need this little bit of code, which you’ll insert after your two $response inserts:
$relationship = array(
‘module1′ => ‘Contacts’,
‘module1_id’ => “$contact_id”,
‘module2′ => ‘Accounts’,
‘module2_id’ => “$account_id”,
);// call set_relationship()
$response = $client->set_relationship($session_id, $relationship);
Pretty simple, right? This won’t work without the $contact_id and $account_id variables though, so you’ll need to call $contact_id = $response->id and $account_id = $response->id after each of your $response inserts. The final result will look something like this:
<?php
// set up options array
$options = array(
“location” => ‘http://your.website.here/sugarcrm/soap.php’,
“uri” => ‘http://www.sugarcrm.com/sugarcrm’,
“trace” => 1
);//user authentication array
$user_auth = array(
“user_name” => ‘YOUR_SUGAR_USERNAME’,
“password” => MD5(’YOUR_SUGAR_PASSWORD’),
“version” => ‘.01′
);// connect to soap server
$client = new SoapClient(NULL, $options);// Login to SugarCRM
$response = $client->login($user_auth,’test’);$session_id = $response->id;
$user_id = $client->get_user_id($session_id);// write our form data to the Contacts module
$response = $client->set_entry($session_id, ‘Contacts’, array(
array(”name” => ‘first_name’,”value” => $_POST['first_name']),
array(”name” => ‘last_name’,”value” => $_POST['last_name']),
array(”name” => ‘email1′,”value” => $_POST['email_address']),
array(”name” => ‘work_phone’,”value” => $_POST['phone_number']),
array(”name” => ‘description’,”value” => $_POST['message']),
array(”name” => ‘assigned_user_id’,”value” => $user_id)
));$contact_id = $response=>id;
// write our form data to the Accounts module
$response = $client->set_entry($session_id, ‘Contacts’, array(
array(”name” => ‘name’,”value” => $_POST['organization_name']),
array(”name” => ‘assigned_user_id’,”value” => $user_id)
));$account_id = $response=>id;
$relationship = array(
‘module1′ => ‘Contacts’,
‘module1_id’ => “$contact_id”,
‘module2′ => ‘Accounts’,
‘module2_id’ => “$account_id”,
);// call set_relationship()
$response = $client->set_relationship($session_id, $relationship);//var_dump($response);
?>
Now if neither of us has made any blunders you should have a working web-to-SugarCRM form that creates a related Account record and a Contact record, both of which are assigned to the authenticated user. Clearly this is very useful technology because it allows your team to respond to web inquiries directly from SugarCRM. With creative use of such modules as the Google Calendar connector (which works, but you can’t uninstall it after you run a Repair or upgrade), you can do some pretty creative stuff to cut down on extraneous software and procedures.
Long live integration! And good luck!
27 ResponsesLeave a comment ?
Fucking brilliant! Thanks mate. Saved my ass.
Hey, thanks, Fred! Happy to help.
Hi there, great article, but that went straight over my head! I am not a dev (as you may have guessed). However i am considering using Sugar for my new online business. I havent had the integration into sugar specified as yet, and I am afraid the dev is gonna rip my heart out. So I was thinking if there is already a plug in or something similar developed where the website would automatically insert the information into the CRM? ie Contact info, Order information, payment details etc…
Thank you very much m8
This tutorial came very much useful for me to build an integration module for OSCommerce2.2 and SugarCRM 5.2. I haven’t finished it yet.
(Going to check the part-2 tomorrow which might possibly help me do the rest of this project
)
Hi Tanish,
Thanks for the kind words. Hopefully pt2 will be helpful, as well. It’s mostly about integration with Google Charts API.
Are you happy with OSCommerce? I’ve never used it, but we have a project that it may be a good fit for…
Cheers,
Dean
can you help me with salesforce? we dont use sugerCRM
Hi Ben–I could probably figure it out. What’s up?
Dean that would be awesome, can you email me directly for assistance? We’re currently using salesforce and it would be nice if once someone sumbits a form using cforms that salesforce would capture the info. ben@mcelroyfilms.com
I’m looking for a dev or designer here who can create a custom theme to match our website look and feel. We’d like to change the positioning of the layout/navigation and colors buttons, etc…. Please let me know if this is the correct forum to be posting in. I had posted this before but only got one reply. Is it hard to create customized themes for sugar It seems like I can’t find hardly anyone to do it.
Thanks
I would recommend posting an ad on CraigsList to find a webby who can edit CSS. That’s the biggest chunk of what you’ll need to do to create a theme in Sugar. For the most part, creating Sugar themes is pretty easy. You may also want to check out this link: http://www.sugarthemeclub.org/
Yes this will work. Just remember when you modify one of the stock modules to include a new field, the new field will be affixed with a _c on the end.
I’m looking for a dev or designer here who can create a custom theme to match our website look and feel. We’d like to change the positioning of the layout/navigation and colors buttons, etc…. Please let me know if this is the correct forum to be posting in. I had posted this before but only got one reply. Is it hard to create customized themes for sugar It seems like I can’t find hardly anyone to do it.
Thanks
Great post, I’ve been trying to fix this for weeks.
Thanks loads!
Yes–you can modify or build a module and access the fields via SOAP. Just keep in mind that if you modify one of the built-in modules your field name will have _c appended to it. For instance, if you added a field “birthday” it would be referenced as birthday_c. In modules you build from scratch however this would not be the case.
Very good article. I’ve seen many examples using SugarCRM’s SOAP interface, but none described in this detail. When it comes down to it, there are two things that you need to do – construct your SOAP call, and parse the data. That’s it. All the other tutorials, demos, examples, what have you, don’t explain this.
Thanks again for taking the time to write this. I’m going to start hacking on this right now, actually.
Great post,
I just have a question:
Example: I’m inserting a Lead to the Leads module, but I need to insert some other fields like referring website, and some other fields. Do I have to use the Studio in Sugar, to edit the Leads module, and add these fields, and then use SOAP to fill these fields, would it work?
[...] been a while since I wrote my last tutorial on SugarCRM, and I think it’s time for an update. In my previous set of examples, I discussed [...]
Thanks for your great example! Especially showing me the “email1″ parameter.
Hi,
thank you for these examples.
I’m trying to create a new document and I was wondering how to attach the file.
I notice a method called “set_document_revision”. Do you know how to use it?
Thank you
Hey thanks …..grt work !!
Ooops, hit the send button too fast…
Anyway:
$soapClient->call('set_relationship', $soapArgs);
Note the ’set_relationship_value’ =>, otherwise i’ll get an “Module Does Not Exist” error from Sugar :-/
I think your problem might be nusoap. Check out this post
http://www.sugarcrm.com/forums/showthread.php?t=33242.
I suppose the examples for “set_relationship” wont work as described, at least not for Sugar 5.2.0f w/ PHP 5.1.6 and Nusoap (Sugars version).
I had to construct the SOAP paramters like this:
$soapArgs = array(
'session' => $soapSessionId,
'set_relationship_value' => array(
'module1' => 'Calls',
'module1_id' => $callRecord['sweet']['id'],
'module2' => 'Contacts',
'module2_id' => $assoSugarObject['values']['id'],
)
);
never mind. I’m using PHP 5.1.6 and the inbuilt soapclient class isn’t available in this version … I guess I’ll have to use nusoap…
where is the SoapClient class located?
Thanks for the clear explanation, this was thruly helpfull.
Do you mind sharing a bit more?
Do you know by any chance how to verify if a field is unique before saving it in Sugar via Soap?
We are breaking our heads trying to solve this :S
Thanks a lot for any help you can give us on the subject
YOU ROCK!
thanks so much for the tips & walkthru