HomeArticles SugarCRM and Asterisk integration in Java ( II )
SugarCRM and Asterisk integration in Java ( II )
Opening a customer SugarCRM page in the browser on incoming calls
Introduction
Here comes the really interesting part, as this is what most readers have been asking for lately. The feature lots of people seem to be interested in is the possibility of , on an inbound asterisk call, opening/redirecting the browser to the specific account/contact SugarCRM page. Before we proceed you should first have a look at two of our previous articles:
In particular, we'll take the code developed in the first one and add some features, while the second one is useful to see how we're going to cope with SugarCRM authentication. As usual acknowledgments are due to the authors of AsteriskJava for the great job they are doing with their library.
The code
In the first article we've written two java classes: "PhoneNumberResolver", which is our main class and the listener for asterisk events, and "AccountFinder" which is in charge of querying SugarCRM for the Account owning the caller phone number for our inbound call. At the end of the article we were able to get a caller id from an asterisk event in the method "onManagerEvent" of our "PhoneNumberResolver" class, pass the caller id to the "searchAccountByNumber" method of our class "AccountFinder", obtaining back an hashtable with data eventually extracted from Sugarcrm and show them. What we are going to do now, is to create a new method "browserToSugarCRM" in our "AccountFinder" class. Again we'll pass it the caller id and it will again check if a corresponding Account exists in SugarCRM , and if so it will open our browser to the specific SugarCrm page. Here is our new method:
The method used here is explained in the second article cited above. We do a SOAP login to SugarCRM and get a session id (lines 3 and 4). then we use the session id to do a "seamless login"(line 5) which will allow us to be also logged in to the web interface.On line 6 we query SugarCRM to see if an Account with the given telephone number exists and, if so(line 9), we get the id of the extracted account (line 12). A little explanation for line 12; the account id is at index "0" of the array "nvl" because on line 7, when querying SugarCRM, we've put "id" as the first (that is index 0) element of the String array of the required fields. Now we have all we need to compose the URL to enter SugarCRM bypassing the login form and be redirected to the details page of the corresponding account record. On line 13 we create a 2 elements String array containing the path to our browser and the URL itself with variable "value" representing the account id on the database and the sessionID passed as parameter "MSID". This is what allows us to bypass the login page.
WARNING: the "MSID method" of remotely logging in hasn't been ported to version "5.0" of SugarCRM, while it seems it is present again on version "5.1" . For testing I'm using SugarCRM 4.2.1b, and the mechanism works properly.
On line 13 we launch the browser. This string is strictly related to the OS and browser you're using.In my case, if Firefox is already open, a new tab gets opened and I'm redirected to the account record sugarcrm page. To complete the modifications, we need to change the "onManageEvent" method of our "PhoneNumberResolver" class. Here is the code:
We've only changed the call to AccountFinder, this time calling our new "browserToSugarCRM" method. Done this we don't need anymore the method "showData", so here is the final version of "PhoneNumberResolver":
This article should answer a lot of email from readers interested in the exposed mechanism. I'll be writing more articles on Asterisk/SugarCRM and SugarCRM/CMS integration soon. Hasta la proxima.