Integration between SugarCRM and Perl using SOAP

I’ve been developing SugarCRM solutions for a while now at InsightfulCRM (Fonality Pty Ltd) and I never really got into interacting through SOAP with languages other than PHP until today.

SugarCRM

There are some tutorials and documentation on SugarCRM Wiki website about it, but not really for Perl, that is the programming language I’ve chosen today.

I’m not a “rockstar” with Perl but let’s figure this out :)

First of all why Perl?
The main reason is: you get Perl with your Linux box, without mucking around too much, in any Linux distribution and doesn’t really need too much software around it. It is multi platform and it has his own package manager (CPAN) to install other components.
This is all that’s needed to get started!

So let’s start.

First of all, let’s define what exactly we want to achieve through SOAP.

1) We want Perl to log in to our SugarCRM instance.
2) We want to create a new Case in Sugar
3) We want to create a note with an attachment (file stored on the PC where the script is executed) linked to our new Case

The software I will write, will be running from command line, and to be safe, we will add another step before 1), to check the hostname where our SugarCRM resides is currently active, just by pinging it.

We will need to install some software that doesn’t really come out of the box.
Usually SOAP Lite is not installed by default, so we will need to run

perl -MCPAN -e 'install SOAP::Lite'

Now we can start the real coding.

Step 0, write a few lines to check if the host is alive (works only if executed by root):

#!/usr/bin/perl
use Net::Ping;
my $p = Net::Ping->new("icmp");
if ($p->ping('our.crm.com', 0.5))
{
print 'Alive\n';
} else {
print 'Dead\n';
}

Now we have to use the SugarCRM SOAP login function. It requires the username and the password passed in as parameters, and it returns the session id that we will use for other SOAP calls.

Let’s write the login part using the necessary libraries.

use SOAP::Lite;
use Data::Dumper;
# For encoding notes attachment content we will need this
use MIME::Base64;
# Let's define some variables...
my $soap_domain = 'http://www.sugarcrm.com/sugarcrm';
my $soap_url = 'http://our.crm.com/soap.php';
my $sugarcrm_username = 'username';
# MD5 of the password
my $sugarcrm_md5_password = '5f4dcc3b5aa765d61d8327deb882cf99';
# Instantiate SOAP
my $soap = SOAP::Lite->uri($soap_domain)->proxy($soap_url);
# Hash with credentials
my $user_auth = {'user_name' => $sugarcrm_username, 'password' => $sugarcrm_md5_password};
# Execute the login
my $result = $soap->login(SOAP::Data->value($user_auth));
# We can eventually print out the result so that we understand the data structure for debug (yeah that's what I did all the time)
# print(Dumper($result->result));
# And finally get the session id
my $session_id = $result->result->{'id'};
# Now we can get the Sugar user id of the logged in user (always useful for assigning records to the right person)
$result = $soap->get_user_id(SOAP::Data->value($session_id));
my $sugarcrm_id = $result->result;

Now we have to complete point 2, creating the Case. For this we use the general purpose function ‘set_entry’.
We just need to pass the session id previously generated, the module name, and an array of name and values for each field we want to fill in.

# Ok let's create the Case
$result = $soap->set_entry(
SOAP::Data->value($session_id),
SOAP::Data->value('Cases'),
SOAP::Data->value(
[
{'name' => 'name', 'value' => 'Enrico Test'},
{'name' => 'status', 'value' => 'Assigned'},
{'name' => 'assigned_user_id', 'value' => $sugarcrm_id},
{'name' => 'description', 'value' => 'Enrico Test'}
]
)
);
my $case_id = $result->result->{'id'};

Now we retrieve the record just created to use the case_number property. We can use the get_entry function.
It needs the session, the module name, the case id and an array of fields we want back in the response.

$result = $soap->get_entry(
SOAP::Data->value($session_id),
SOAP::Data->value('Cases'),
SOAP::Data->value($case_id),
SOAP::Data->value(
['case_number']
)
);
# Hash, use ->{'key'}, array use ['0'] (Yeah this needed a bit of debugging before I got it right!)my $case_number = $result->result->{'entry_list'}['0']->{'name_value_list'}['0']->{'value'};

It’s time for the step 3. Create a function to easily store multiple files.

a) Create a Note with set_entry
b) Read the file
c) Create an attachment for the note just created with set_note_attachment (This function can only link a file to an existing note…)

Here is the code:

sub create_note($$$$$)
{
my($soap_connection, $session_id, $filename, $display_filename, $case_id) = @_;
# Check if the file exists
if(-e $filename)
{
# Create note
$result = $soap_connection->set_entry(
SOAP::Data->value($session_id),
SOAP::Data->value('Notes'),
SOAP::Data->value(
[
{'name' => 'name', 'value' => $display_filename},
{'name' => 'parent_id', 'value' => $case_id},
{'name' => 'parent_type', 'value' => 'Cases'}
]
)
);
$note_id = $result->result->{'id'};
# Read our file
open(DAT, $filename) || die("Could not open file!");
@data=;
close(DAT);
# Convert array of lines to a string
$data = join('', @data);
# Attach the file (The content needs to be encoded in base 64)
my $resp = $soap_connection->set_note_attachment(
SOAP::Data->value($session_id),
SOAP::Data->value(
{
'id' => $note_id,
'file' => encode_base64($data),
'filename' => $display_filename
}
)
);
}
}

Cool! Now we have completed all three pieces that we needed, and I’m sure that I’ve covered a bit of everything on how to integrate your Perl program with SugarCRM.

If you are interested on having a free trial SugarCRM system and purchasing it in Australia, you can always send me an email and/or go to InsightfulCRM website.

Thank you,

Enrico

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.