Creating Custom Web Services In SugarCRM

I have been using web services to access Sugar for sometime now, for purposes of integration and for accessing data from a portal. While it is reasonably well documented, there are restrictions, and last week I cam across a requirement I just couldn’t meet with the standard web services methods. It was time to explore custom web services.

When I did, I suddenly realised that it was very simple to implement, and allowed me to push much of the business logic back into the server where it belongs. I could create a new method that had all sorts of complex logic,and expose the results via web services.

Now I did think that this might restrict the ability of the client software; any time it needs to access a Sugar instance, that instance needs the custom web service installed. By zipping up the custom web service as a loadable module (a topic I’ll discuss in the future as it is a very easy and powerful way to deploy any sort of customisation) this objection was quickly overcome. In any cases I would normally have access to the server when doing this type of work.

So, I have included a trivial method here, just to prove that I can. I think you will see that just about anything can go into such a method, allowing the web service to provide complex functionality to its client.

Before tackling that, lets see how the standard web service works. We will look at the version 4.1REST option from Sugar PRO 6.5.13.

service/v4_1/rest.php

You can try this in a browser directly, as follows (your url will be slightly different)

http://localhost/sugarPro6.5.13/service/v4_1/rest.php

This will show you documentation of the standard Sugar methods you can call, and their parameters.

Custom Web Service

Now our custom web service is to reside at custom/service so that it its upgrade safe. Lets look at the code, and compare it with the standard Sugar version above.

You will see we just extend the standard classes and create our own. These are below (MySugarRestService.php and MySugarRestServiceImp.php, though we could do without the first)

and MySugarRestServiceImpl.php below. You can see how it extends the Sugar class and adds a new method (getContactDetails).

Note that the method just returns hard coded values.

Accessing The Web Service

To prove that it works you need some a client. The following is very basic code to act as a client calling our new method. In a later post I will show more examples of client code, but for now this is enough to get started.

When you run that client you get the following response.

Which are just the hard coded value set in our server method.

I think that is enough to get started. I haven’t shown the login process, or how values can be retrieved from Sugar. All will have to wait for a new post.

Please contact me if you have any questions.

 

 

One thought on “Creating Custom Web Services In SugarCRM

  1. Hello, Greg Ambrose.
    Thanks for your great post.
    I’m completely new in SugarCRM. At the moment I’m trying to populate dropdowns through web services.
    I can already fill those values, but when I want to show that value in listview or put it in a pdf, instead of the value it only shows the position of the array and not the value.
    Can you help me? I really appreciate if you can, because I’m trying to solve this problem a few days ago and can not find the solution.
    I’m customizing a community edition 6.5.5.
    Thanks in advance,
    Marta Henriques

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">