Create Custom Activities in Flow to interact with SugarCRM

http://www.projectzero.org/blog/index.php/2009/04/22/create-custom-activities-in-flow-to-interact-with-sugarcrm/

Posted by nicholsr on April 22nd, 2009. Other posts by nicholsr Create Custom Activities in Flow to interact with SugarCRM.

This cookbook entry presents the source code behind this video: Build Situational Applications usng Custom Flow Activities

Here is the contents of config/ivy.xml

<ivy-module version="1.3">
<info module="SugarActivities" organisation="zero" revision="1.0.0">
<license name="type of license" url="http://license.page"/> <ivyauthor name="author name" url="http://authors.home.page"/> <description homepage="http://module.description.page"/>

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 19)

Definition list ends without a blank line; unexpected unindent.

</info> <publications>

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 21)

Unexpected indentation.
<artifact type="zip"/>

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 22)

Block quote ends without a blank line; unexpected unindent.

</publications> <dependencies>

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 24)

Unexpected indentation.
<dependency org="zero" rev="[1.0.0.0,2.0.0.0[" name="zero.mail.kicker"/> <dependency org="zero" rev="[1.0.0.0,2.0.0.0[" name="zero.mail.receiver"/> <dependency org="zero" name="zero.core" rev="[1.0.0.0, 2.0.0.0["/> <dependency org="zero" name="zero.php" rev="[1.0.0.0, 2.0.0.0["/> <dependency org="zero" rev="[1.0.0.0, 2.0.0.0[" name="zero.assemble.flow"/> <dependency org="zero" name="zero.connection.soap" rev="[1.0.0.0, 2.0.0.0["/> <dependency org="zero" name="zero.php.connection" rev="[1.0.0.0, 2.0.0.0["/> </dependencies>

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 32)

Definition list ends without a blank line; unexpected unindent.

</ivy-module>

Here is the contents of config/zero.config

# HTTP port (default is 8080) /config/http/port = 8097

# Runtime mode (default is "production") /config/runtime/mode="development"

/config/zso/immediateStart=true

/config/mail/stores/myMailStore = {
"hostname" : "localhost", "userid" : "dondemo@localhost.com", "password" : "smash"

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 48)

Definition list ends without a blank line; unexpected unindent.

}

/config/zso/immediateStart=true

/config/timer/tasks/myMailKicker = { "delay" : 8 } # polling every 8 seconds

/config/handlers += [{

"events" : "timer", "handler" : "zero.mail.kicker.MailKicker.class", "conditions" : "/event/_taskName =~ myMailKicker", "instanceData": {

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 59)

Unexpected indentation.

"mailStore" : "myMailStore", "receiverURL" : "http://localhost:8097/zero/receiver/mail", "config" : {

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 62)

Unexpected indentation.
# HTTP or HTTPS protocol configuration can be placed here

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 63)

Block quote ends without a blank line; unexpected unindent.

}

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 64)

Block quote ends without a blank line; unexpected unindent.

}

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 65)

Definition list ends without a blank line; unexpected unindent.

}]

/config/handlers += [{
"events" : "POST", "handler" : "zero.mail.receiver.MailReceiver.class", "conditions" : "/request/path =~ /zero/receiver/mail(/.*)?"

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 71)

Definition list ends without a blank line; unexpected unindent.

}]

/config/handlers += [{
"events" : "mailMessage", "handler" : "zero.mail.receiver.flow.FlowMailMessageHandler.class", "conditions" : "(/event/mailStore == myMailStore)", "instanceData" : { "view" : "myMailMessageFlow.flow" }

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 78)

Definition list ends without a blank line; unexpected unindent.

}]

/config/connection/destinations += {
"http://sugarservice/crm/*" : {

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 124); backlink

Inline emphasis start-string without end-string.
"handlers" : [{

"class" : "sugar.SugarLoginHandler", "config" : {

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 87)

Unexpected indentation.
"username" : "will", "password" : "will", "version" : "0.1", "application" : "soapleadcapture"

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 91)

Block quote ends without a blank line; unexpected unindent.

}

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 92)

Block quote ends without a blank line; unexpected unindent.

}, {

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 94)

Unexpected indentation.

"name" : "restToSoap", "config" : {

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 96)

Unexpected indentation.

"endpointAddress" : "http://localhost:8073/soap.php", "SOAPVersion" : "1.1", "r2sMapping" : [{

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 99)

Unexpected indentation.
"RESTOperation" : "PUT", "URLMatch" : "/crm/{recordType}/{objectId}", "SOAPBodyTemplate" : "updateRecord.gt", "SOAPAction" : "http://localhost:8073/soap.php/set_entry"

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 103)

Block quote ends without a blank line; unexpected unindent.

}, {

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 105)

Unexpected indentation.
"RESTOperation" : "POST", "URLMatch" : "/crm/{recordType}", "SOAPBodyTemplate" : "createRecord.gt", "SOAPAction" : "http://localhost:8073/soap.php/set_entry"

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 109)

Block quote ends without a blank line; unexpected unindent.

}, {

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 111)

Unexpected indentation.
"RESTOperation" : "GET", "URLMatch" : "/crm/{recordType}/{objectId}", "SOAPBodyTemplate" : "fetchRecord.gt", "SOAPAction" : "http://localhost:8073/soap.php/get_entry"

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 115)

Block quote ends without a blank line; unexpected unindent.

}, {

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 117)

Unexpected indentation.
"RESTOperation" : "GET", "URLMatch" : "/crm/{recordType}", "SOAPBodyTemplate" : "fetchRecords.gt", "SOAPAction" : "http://localhost:8073/soap.php/get_entry_list"

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 121)

Block quote ends without a blank line; unexpected unindent.

}

]

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 124)

Block quote ends without a blank line; unexpected unindent.

}

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 125)

Block quote ends without a blank line; unexpected unindent.

}]

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 126)

Definition list ends without a blank line; unexpected unindent.

}, "http://sugarservice/sugarLogin" : {

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 128)

Unexpected indentation.
"handlers" : [

{ "name" : "restToSoap", "config" : {

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 132)

Unexpected indentation.

"endpointAddress" : "http://localhost:8073/soap.php", "SOAPVersion" : "1.1", "r2sMapping" : [{

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 135)

Unexpected indentation.
"RESTOperation" : "GET", "SOAPBodyTemplate" : "login.gt", "SOAPAction" : "http://localhost:8073/soap.php/login"

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 138)

Block quote ends without a blank line; unexpected unindent.

}]

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 139)

Block quote ends without a blank line; unexpected unindent.

}

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 140)

Definition list ends without a blank line; unexpected unindent.

}]

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 141)

Block quote ends without a blank line; unexpected unindent.

}

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 142)

Definition list ends without a blank line; unexpected unindent.

}

/config/activity/metadata/sugarUpdate = {
"attributes" : [{"name":"module"},{"name":"id"}], "inputs" : [{"name": "updateData"}]

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 147)

Definition list ends without a blank line; unexpected unindent.

}

/config/activity/metadata/sugarCreate = {
"attributes" : [{"name":"module"}], "inputs" : [{"name": "createData"}]

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 152)

Definition list ends without a blank line; unexpected unindent.

}

/config/activity/metadata/sugarRetrieve = {
"attributes" : [{"name":"module"},{"name":"id"}]

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 156)

Definition list ends without a blank line; unexpected unindent.

}

/config/activity/metadata/sugarFind = {
"attributes" : [{"name":"module"},{"name":"searchString"}]

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 160)

Definition list ends without a blank line; unexpected unindent.

}

Here is the contents of app/assemble/activities/sugarCreate.php

<?php include_once "java_imports.php"; class SugarCreate {

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 167)

Unexpected indentation.
function onSugarCreateActivity() {
$createData = zget ("/event/inputs#0"); $type = zget ("/event/attributes#module"); $contents=new ArrayList(); $contents->add('application/json'); $result=Connection::doPOST("http://sugarservice/crm/$type", array('Content-Type' =>$contents ),json_encode($createData) ); $responseBody=$result->getResponseBodyAsString(); $data=json_decode($responseBody); $id=$data["set_entryResponse"]["return"]["id"]["content"]; zput ("/event/result", $id);

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 177)

Definition list ends without a blank line; unexpected unindent.

}

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 178)

Block quote ends without a blank line; unexpected unindent.

} ?>

Here is the contents of app/assemble/activities/sugarFind.php

<?php

include_once "java_imports.php";

class SugarFind {
function onSugarFindActivity() {

$searchString = zget ("/event/attributes#searchString"); $urlEncodedSS=rawurlencode($searchString); $type = zget ("/event/attributes#module"); $result=Connection::doGET("http://sugarservice/crm/$type?query=$urlEncodedSS"); $sugarObjectList=new Java("java.util.ArrayList"); //work around bug 7834

$responseBody=$result->getResponseBodyAsString(); $data=json_decode($responseBody); if ($data["get_entry_listResponse"]["return"]["result_count"]["content"]>1) {

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 199)

Unexpected indentation.
$returnedObjects=$data["get_entry_listResponse"]["return"]["entry_list"]["item"];

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 200)

Block quote ends without a blank line; unexpected unindent.
} else {
$returnedObjects=array($data["get_entry_listResponse"]["return"]["entry_list"]["item"]);

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 202)

Definition list ends without a blank line; unexpected unindent.

} foreach ($returnedObjects as $returnedObject) {

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 204)

Unexpected indentation.

$sugarObject=array(); $fields=$returnedObject["name_value_list"]["item"]; foreach ($fields as $fieldStruct) {

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 207)

Unexpected indentation.
$fieldName=$fieldStruct["name"]["content"]; $fieldValue=$fieldStruct["value"]["content"]; $sugarObject[$fieldName]=$fieldValue;

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 210)

Block quote ends without a blank line; unexpected unindent.

} $sugarObjectList[]=$sugarObject;

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 212)

Block quote ends without a blank line; unexpected unindent.

} zput ("/event/result",$sugarObjectList);

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 214)

Definition list ends without a blank line; unexpected unindent.

}

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 215)

Definition list ends without a blank line; unexpected unindent.

} ?>

Here is the contents of app/assemble/activities/sugarUpdate.php

<?php

include_once "java_imports.php";

class SugarUpdate {
function onSugarUpdateActivity() {
$updateData = zget ("/event/inputs#0"); $id = zget ("/event/attributes#id"); $type = zget ("/event/attributes#module"); $contents=new ArrayList(); $contents->add('application/json'); $result=Connection::doPUT("http://sugarservice/crm/$type/$id", array('Content-Type' =>$contents ),json_encode($updateData) ); zput ("/event/result", $result->getResponseBody());

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 233)

Definition list ends without a blank line; unexpected unindent.

}

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 234)

Definition list ends without a blank line; unexpected unindent.

} ?>

Here is the contents of app/assemble/activities/sugarRetrieve.php

<?php

include_once "java_imports.php";

class SugarRetrieve {
function onSugarRetrieveActivity() {

$id = zget ("/event/attributes#id"); $type = zget ("/event/attributes#module"); $result=Connection::doGET("http://sugarservice/crm/$type/$id"); $sugarObjectProperties= array(); $responseBody=$result->getResponseBodyAsString(); $data=json_decode($responseBody); $fields=$data["get_entryResponse"]["return"]["entry_list"]["item"]["name_value_list"]["item"]; foreach ($fields as $fieldStruct) {

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 253)

Unexpected indentation.
$fieldName=$fieldStruct["name"]["content"]; $fieldValue=$fieldStruct["value"]["content"]; $sugarObjectProperties[$fieldName]=$fieldValue;

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 256)

Block quote ends without a blank line; unexpected unindent.

} zput ("/event/result",$sugarObjectProperties);

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 258)

Definition list ends without a blank line; unexpected unindent.

}

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 259)

Definition list ends without a blank line; unexpected unindent.

} ?>

Here is the contents of app/scripts/java_imports.php

<?php java_import("zero.core.connection.Connection",NULL,false); java_import("java.util.ArrayList",NULL,false);

?>

Here is the contents of app/views/myMailMessageFlow.flow

<process name="splice-flow" persistPolicy="on">

<receiveMailMessage name="receiveMailMessage"/> <sugarFind name="FindAccount" searchString="accounts.name LIKE '%${request.mail.headers.Subject[0]}%'" module="Accounts">

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 275)

Unexpected indentation.
<control source="receiveMailMessage"/>

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 276)

Block quote ends without a blank line; unexpected unindent.

</sugarFind> <sugarCreate name="CreateNote" module="Notes">

System Message: ERROR/3 (data/sugarcrm-rest-to-soap.txt, line 278)

Unexpected indentation.
<input name="createData" value="${['name':'Note added by email','parent_type':'Accounts','parent_id':FindAccount[0].id,'description':receiveMailMessage]}"/>

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 279)

Block quote ends without a blank line; unexpected unindent.

</sugarCreate>

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 280)

Definition list ends without a blank line; unexpected unindent.

</process>

Here is the contents of app/views/fetchRecord.gt

<q0:get_entry soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:q0="http://www.sugarcrm.com/sugarcrm" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" > <session xsi:type="xsd:string"><%= zget("/connection/sugerSessionId") %></session> <module_name xsi:type="xsd:string"><%= r2s_getParam("recordType") %></module_name> <id xsi:type="xsd:string"><%= r2s_getParam("objectId") %></id>

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 292)

Definition list ends without a blank line; unexpected unindent.

</q0:get_entry>

Here is the contents of app/views/fetchRecords.gt

<q0:get_entry_list soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:q0="http://www.sugarcrm.com/sugarcrm" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" > <session xsi:type="xsd:string"><%= zget("/connection/sugerSessionId") %></session> <module_name xsi:type="xsd:string"><%= r2s_getParam("recordType") %></module_name> <query xsi:type="xsd:string"><%= r2s_getParam("query") %></query>

System Message: WARNING/2 (data/sugarcrm-rest-to-soap.txt, line 304)

Definition list ends without a blank line; unexpected unindent.

</q0:get_entry_list>

the files app/views/login.gt, app/views/updateRecord.gt, app/views/createRecord.gt ,java/sugar/SugarLoginHandler.java are as described in http://www.projectzero.org/blog/index.php/2009/02/13/use-the-rest-to-soap-layer-to-interact-with-sugarcrm/

I have placed the icons that live under public/tooling on the wiki here: