cYAn YeLLOW MAGenTA BLAcK PAnTOne 123 c BOOKs fOR PROfessIOnALs BY PROfessIOnALs® The eXPeRT’s VOIce® In OPen sOuRce Companion eBook Available The Definitive Guide to SugarCRM: Better Business Applications The Definitive Guide to Dear Reader, The Definitive Guide to S SugarCRM is the world's leading commercial open source CRM software, serv- ing more than 500,000 users all over the world. What most people don't realize u is how flexible SugarCRM is as a platform, enabling businesses of all sizes to g John Mertic custom build the CRM application they need. In this book, you'll learn all about SugarCRM SugarCRM from a developer's perspective. a This book brings to light the developer side of SugarCRM, an often rather r neglected topic. It provides something unique compared to all previous SugarCRM C books; the insight from being a part of very talented engineering team at SugarCRM which has put endless efforts into making SugarCRM one of the premier open R source web applications today. This insight will give you a deeper understanding how the internals of SugarCRM tick, and this book will be an authoritative resource M for developing new applications on top of the SugarCRM platform. The Definitive Guide to SugarCRM comes in three parts. Part 1 focuses on SugarCRM itself: the company, the community, the application, and the plat- Better Business Applications form. Here we’ll do a deep dive into everything the platform offers, from the MVC and Metadata frameworks to Web Services, and many pieces in between. Part 2 then takes this knowledge of the Sugar platform and builds upon it show- ing you how to customize the Sugar application out of the box. Part 3 takes that a step further, showing you how to build entirely new functionality on top of SugarCRM, and closes up with building an entire application on top of SugarCRM from start to finish. With this book, you’ll gain a better understanding of SugarCRM, and in doing so, you’ll be able to take SugarCRM and customize it to meet your organi- A hands-on guide to the #1 open source zation's needs. I truly hope you enjoy it. CRM platform John Mertic Companion eBook Software Engineer - SugarCRM S e l See last page for details T on $10 eBook version D Ti e T A l e R M John Mertic SOURCE CODE ONLINE e www.apress.com ISBN 978-1-4302-2439-6 rt 55999 ic US $59.99 Shelve in E-commerce User level: 9 781430 224396 Intermediate this print for content only—size & color not accurate spine = 0.625" 280 page count Download at WoweBook.Com The Definitive Guide to SugarCRM Better Business Applications ■ ■ ■ John Mertic i Download at WoweBook.Com The Definitive Guide to SugarCRM: Better Business Applications Copyright © 2009 by John Mertic ISBN-13 (pbk): 978-1-4302-2439-6 ISBN-13 (electronic): 978-1-4302-2440-2 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Steve Anglin Technical Reviewer: Matt Heitzenroder, Roger Smith, Collin Lee Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Copy Editor: Jim Markham Coordinating Editor: Katie Stence Compositor: MacPS, LLC Artist: April Milne Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders- [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5939, e-mail [email protected], or visit http://www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. ii Download at WoweBook.Com iii Download at WoweBook.Com Contents at a Glance ■Contents at a Glance............................................................................................iv(cid:1) ■Contents................................................................................................................v(cid:1) ■About the Author..................................................................................................xi(cid:1) ■About the Technical Reviewers...........................................................................xii(cid:1) ■Acknowledgments..............................................................................................xiii(cid:1) ■Introduction .......................................................................................................xiv Part 1: The SugarCRM Platform...............................................................................1(cid:1) ■Chapter 1: What Is SugarCRM?.............................................................................3(cid:1) ■Chapter 1: MVC Architecture...............................................................................11(cid:1) ■Chapter 1: Metadata Layer..................................................................................35(cid:1) ■Chapter 1: Web Services.....................................................................................55(cid:1) ■Chapter 1: More Platform Features.....................................................................77 Part 2: Customizing SugarCRM Out of the Box....................................................107(cid:1) ■Chapter 1: Easy Customizations Using Studio..................................................109(cid:1) ■Chapter 1: Workflows and Logic Hooks............................................................125(cid:1) ■Chapter 1: Overriding Existing Code.................................................................151 Part 3: Building New Functionality on Top of SugarCRM.....................................169(cid:1) ■Chapter 1: Using Module Builder to Build Custom Modules..............................171(cid:1) ■Chapter 1: Building a Custom Module Manually...............................................195(cid:1) ■Chapter 1: Designing a Complete Application...................................................219(cid:1) ■Index.................................................................................................................219(cid:1) iv Download at WoweBook.Com Contents ■Contents at a Glance............................................................................................iv(cid:1) ■Contents................................................................................................................v(cid:1) ■About the Author..................................................................................................xi(cid:1) ■About the Technical Reviewers...........................................................................xii(cid:1) ■Acknowledgments..............................................................................................xiii(cid:1) ■Introduction........................................................................................................xiv Part 1: The SugarCRM Platform................................................................................1(cid:1) ■Chapter 1: What Is SugarCRM?.............................................................................3(cid:1) The Company..................................................................................................................3(cid:1) Rapid Growth—Harnessing Open Source and SaaS..............................................................................4(cid:1) The Product.....................................................................................................................4(cid:1) The Community...............................................................................................................7(cid:1) Sugar Forums and Wiki..........................................................................................................................7(cid:1) SugarExchange and SugarForge............................................................................................................7(cid:1) Sugar Developer Zone............................................................................................................................8(cid:1) Getting SugarCRM..................................................................................................................................8(cid:1) Summary.........................................................................................................................9(cid:1) ■Chapter 2: MVC Architecture...............................................................................11(cid:1) What Is MVC?................................................................................................................11(cid:1) MVC the Sugar Way......................................................................................................12(cid:1) SugarApplication..................................................................................................................................13(cid:1) SugarController....................................................................................................................................15(cid:1) SugarView............................................................................................................................................18(cid:1) v Download at WoweBook.Com ■ CONTENTS Entrypoints....................................................................................................................21(cid:1) What About the Model?.................................................................................................22(cid:1) Bean Classes and SugarObjects..........................................................................................................22(cid:1) Vardefs.................................................................................................................................................24(cid:1) Fields....................................................................................................................................................24(cid:1) Indexes.................................................................................................................................................28(cid:1) Relationships.......................................................................................................................................29(cid:1) Database Layer....................................................................................................................................31(cid:1) Sugar Before MVC.........................................................................................................33(cid:1) Summary.......................................................................................................................33(cid:1) ■Chapter 3: Metadata Layer..................................................................................35(cid:1) DetailView and EditView..............................................................................................35(cid:1) How the DetailView and EditView Classes Work..........................................................38(cid:1) SugarFields..........................................................................................................................................40(cid:1) ListViews..............................................................................................................................................41(cid:1) Defining Search Fields.........................................................................................................................44(cid:1) Field Overlays”.....................................................................................................................................46(cid:1) Subpanels............................................................................................................................................48(cid:1) Summary.......................................................................................................................53(cid:1) ■Chapter 4: Web Services.....................................................................................55(cid:1) What Are Web Services?...............................................................................................55(cid:1) Connectors....................................................................................................................56(cid:1) Building the Connector Source............................................................................................................56(cid:1) Defining the Fields from the Web Service”...................................................................60(cid:1) Formatters...........................................................................................................................................61(cid:1) Localization..........................................................................................................................................63(cid:1) Pulling It Together................................................................................................................................63(cid:1) Web Services API..........................................................................................................64(cid:1) SOAP....................................................................................................................................................65(cid:1) Make a Connection..............................................................................................................................66(cid:1) Get Lists and Counts of Records..........................................................................................................67(cid:1) vi Download at WoweBook.Com ■ CONTENTS Add a New Record...............................................................................................................................68(cid:1) Saving and Retrieving Attachments.....................................................................................................70(cid:1) Relate Records.....................................................................................................................................71(cid:1) REST.....................................................................................................................................................72(cid:1) Logging in Using REST.........................................................................................................................73(cid:1) Custom Web Services...................................................................................................73(cid:1) Create a Custom Registry....................................................................................................................74(cid:1) Provide an Implementation Class.........................................................................................................74(cid:1) Add soap.php and rest.php Files..........................................................................................................75(cid:1) Summary.......................................................................................................................75(cid:1) ■Chapter 5: More Platform Features.....................................................................77(cid:1) User Management.........................................................................................................77(cid:1) ACL.......................................................................................................................................................79(cid:1) Password Management.......................................................................................................................81(cid:1) Tracking User Actions..........................................................................................................................82(cid:1) Team Management.......................................................................................................84(cid:1) Dashlets...............................................................................................................................................85(cid:1) ListView Dashlet..............................................................................................................................86(cid:1) Chart Dashlets.................................................................................................................................89(cid:1) Iframe Dashlets...............................................................................................................................92(cid:1) Custom Dashlets.............................................................................................................................93(cid:1) Sugar Feeds.........................................................................................................................................94(cid:1) Record Importing and Exporting..........................................................................................................98(cid:1) Themes..............................................................................................................................................102(cid:1) Logger................................................................................................................................................104(cid:1) Summary.....................................................................................................................106(cid:1) Part 2: Customizing SugarCRM Out of the Box....................................................107(cid:1) ■Chapter 6: Easy Customizations Using Studio ..................................................109(cid:1) Starting with Studio....................................................................................................109(cid:1) Adding New Fields to a Module.........................................................................................................111(cid:1) Customizing View Templates......................................................................................113(cid:1) vii Download at WoweBook.Com ■ CONTENTS Customizing Edit and Detail Views.....................................................................................................113(cid:1) Customizing ListViews.......................................................................................................................115(cid:1) Editing Labels.....................................................................................................................................117(cid:1) Relationships, Relationships, Relationships......................................................................................119(cid:1) Help! My Customizations Have Gone Awry........................................................................................121(cid:1) Summary.....................................................................................................................123(cid:1) ■Chapter 7: Workflows and Logic Hooks............................................................125(cid:1) What Are Logic Hooks?...............................................................................................125(cid:1) Creating Your First Logic Hook...........................................................................................................127(cid:1) Point and Click Logic Hooks with Workflows.....................................................................................131(cid:1) Creating the Workflow..................................................................................................................132(cid:1) Defining Workflow Alerts..............................................................................................................138(cid:1) Workflow Actions..........................................................................................................................141(cid:1) Managing the Sequence of Workflows.........................................................................................142(cid:1) Logic Hooks Pitfalls............................................................................................................................143(cid:1) Modifying the Bean Object............................................................................................................143(cid:1) Conflicting Logic Hooks.................................................................................................................147(cid:1) Logic Hook Being Nullified by the Existing Code...........................................................................148(cid:1) Logic Hook Weaknesses...............................................................................................................149(cid:1) Summary.....................................................................................................................150(cid:1) ■Chapter 8: Overriding Existing Code.................................................................151(cid:1) Safety Lies in the Custom Directory............................................................................151(cid:1) Customizations You Can Make...........................................................................................................153(cid:1) Altering View Actions....................................................................................................................153(cid:1) Changing the Metadata-Driven Views...........................................................................................156(cid:1) Adding New Custom Field Types..................................................................................................161(cid:1) Changing Language Strings..........................................................................................................164(cid:1) Tweak vardef Definitions..............................................................................................................165(cid:1) Custom Themes and Theme Customizations (Yes, They Are Different ).......................................166(cid:1) Summary.....................................................................................................................168(cid:1) viii Download at WoweBook.Com