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"/>
</info> <publications>
<artifact type="zip"/>
</publications> <dependencies>
<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>
</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"
}
/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": {
"mailStore" : "myMailStore", "receiverURL" : "http://localhost:8097/zero/receiver/mail", "config" : {
# HTTP or HTTPS protocol configuration can be placed here
}
}
}]
- /config/handlers += [{
- "events" : "POST", "handler" : "zero.mail.receiver.MailReceiver.class", "conditions" : "/request/path =~ /zero/receiver/mail(/.*)?"
}]
- /config/handlers += [{
- "events" : "mailMessage", "handler" : "zero.mail.receiver.flow.FlowMailMessageHandler.class", "conditions" : "(/event/mailStore == myMailStore)", "instanceData" : { "view" : "myMailMessageFlow.flow" }
}]
- /config/connection/destinations += {
- "http://sugarservice/crm/*" : {
-
- "handlers" : [{
"class" : "sugar.SugarLoginHandler", "config" : {
"username" : "will", "password" : "will", "version" : "0.1", "application" : "soapleadcapture"
}
}, {
"name" : "restToSoap", "config" : {
"endpointAddress" : "http://localhost:8073/soap.php", "SOAPVersion" : "1.1", "r2sMapping" : [{
"RESTOperation" : "PUT", "URLMatch" : "/crm/{recordType}/{objectId}", "SOAPBodyTemplate" : "updateRecord.gt", "SOAPAction" : "http://localhost:8073/soap.php/set_entry"
}, {
"RESTOperation" : "POST", "URLMatch" : "/crm/{recordType}", "SOAPBodyTemplate" : "createRecord.gt", "SOAPAction" : "http://localhost:8073/soap.php/set_entry"
}, {
"RESTOperation" : "GET", "URLMatch" : "/crm/{recordType}/{objectId}", "SOAPBodyTemplate" : "fetchRecord.gt", "SOAPAction" : "http://localhost:8073/soap.php/get_entry"
}, {
"RESTOperation" : "GET", "URLMatch" : "/crm/{recordType}", "SOAPBodyTemplate" : "fetchRecords.gt", "SOAPAction" : "http://localhost:8073/soap.php/get_entry_list"
}
]
}
}]
}, "http://sugarservice/sugarLogin" : {
- "handlers" : [
{ "name" : "restToSoap", "config" : {
"endpointAddress" : "http://localhost:8073/soap.php", "SOAPVersion" : "1.1", "r2sMapping" : [{
"RESTOperation" : "GET", "SOAPBodyTemplate" : "login.gt", "SOAPAction" : "http://localhost:8073/soap.php/login"
}]
}
}]
}
}
- /config/activity/metadata/sugarUpdate = {
- "attributes" : [{"name":"module"},{"name":"id"}], "inputs" : [{"name": "updateData"}]
}
- /config/activity/metadata/sugarCreate = {
- "attributes" : [{"name":"module"}], "inputs" : [{"name": "createData"}]
}
- /config/activity/metadata/sugarRetrieve = {
- "attributes" : [{"name":"module"},{"name":"id"}]
}
- /config/activity/metadata/sugarFind = {
- "attributes" : [{"name":"module"},{"name":"searchString"}]
}
Here is the contents of app/assemble/activities/sugarCreate.php
<?php include_once "java_imports.php"; class SugarCreate {
- 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);
}
} ?>
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) {
$returnedObjects=$data["get_entry_listResponse"]["return"]["entry_list"]["item"];
- } else {
- $returnedObjects=array($data["get_entry_listResponse"]["return"]["entry_list"]["item"]);
} foreach ($returnedObjects as $returnedObject) {
$sugarObject=array(); $fields=$returnedObject["name_value_list"]["item"]; foreach ($fields as $fieldStruct) {
$fieldName=$fieldStruct["name"]["content"]; $fieldValue=$fieldStruct["value"]["content"]; $sugarObject[$fieldName]=$fieldValue;
} $sugarObjectList[]=$sugarObject;
} zput ("/event/result",$sugarObjectList);
}
} ?>
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());
}
} ?>
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) {
$fieldName=$fieldStruct["name"]["content"]; $fieldValue=$fieldStruct["value"]["content"]; $sugarObjectProperties[$fieldName]=$fieldValue;
} zput ("/event/result",$sugarObjectProperties);
}
} ?>
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">
<control source="receiveMailMessage"/>
</sugarFind> <sugarCreate name="CreateNote" module="Notes">
<input name="createData" value="${['name':'Note added by email','parent_type':'Accounts','parent_id':FindAccount[0].id,'description':receiveMailMessage]}"/>
</sugarCreate>
</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>
</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>
</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:
