Sage Accpac Community
Accpac Support Community
Search in


Over the last 7 years, Rob has been asked by the SageCRM Community hundreds if not thousands of questions on how do you do this inside of SageCRM? Rob has noticed a very distinctive pattern develop over these last 6 years as the respective members of the SageCRM Community have joined and advanced their knowledge of SageCRM. The questions have gone from basic to advanced/techie/nerdy as each member’s knowledge and implementation experience has grown. The intent of this blog is to help you as a member of the SageCRM Community to move your SageCRM knowledge to the next level - what ever that level might be.

How to WRITE a SageCRM SQL View for NON-PROGRAMMERS

 Overview:

The purpose of this article is to provide instruction on how to write a SQL View for use within SageCRM.  This is designed for SageCRM Certified Consultants who are not programmers or are just learning how to write SQL Statements for SageCRM and do not know Transact SQL.  A PDF has been created to proivde the following details for off line use and for viewing the individual images referenced below.  To download the PDF click here.

Assumptions:

During this discussion I will be presenting how to use SQL 2005 Express Management Studio to write a SQL Statement and then apply that code within SageCRM.  Even though I am using SQL 2005 Express for this example, the logic and process would be similar on SQL 2000 or full SQL 2005.

Perquisites:

SQL 2005 Express must be installed before SageCRM is installed.  OR SQL 2005 Express will be installed if SQL is not found during the installation routine.

HOWEVER, to complete this exercise an additional tool must be downloaded from Microsoft’s website.  Navigate to www.microsoft.com .  Then search for “SQL 2005 Express Management Studio”.  Once found download and install this free utility.  We will be using this tool for this whitepaper.

Instructions:

First start Microsoft SQL Server Management Studio Express.  Login as SA or use Windows Authentication.  For this exercise either way is fine.  In the print screen below I have clicked on the connect button.

On the right half of the screen Navigate to <SQL_Server_Instance>| Databases | <CRM_Database> | Views, in the print screen below it can be seen that I have navigated to TTAYLORVM2 | Databases | CMR61FULL | Views.

Once the SageCRM Views are visible, right click in a blank area on the right hand side of the screen and select New View.

Now let us start writing a view by selecting two tables, Company and Person, as shown in the
following print screen.  Note that the Company and Person tables have already been added.  By simply scrolling through the list of tables and clicking on the Add button. 

Once both tables are added click close on the Add Table screen. 

Note in the following print screen (under Scenario 1) that I checked on both tables the option of
*(All Columns).  This allows for the results to show ALL the columns within both tables.  If only certain fields are needed, then those fields should be selected.  This area brings up several areas that from a non-programmer perspective can be a little much to grasp and are out of the scope of what this document was attended for.  From the perspective of the beginning coder, start writing views by using the
option of *(All Columns). 

Now let’s join the two tables together.  We first have to decide what data we want.  The following scenarios discuss examples of how to present this data.

Scenario 1:

What if I want all companies and their respective Primary Contact, which two fields should we link together? 

In the print screen above I joined from Comp_PrimaryPersonID to Pers_PersonID.  This brought back all companies and their Primary contact. 
 
Scenario 2:

What if the customer wants all company records even when the company does not have a primary contact associated to it? 

In the previous scenario, I joined the two fields together in a way that I only get a Company when it has a primary contact defined.  If I change the relationship to a LEFT OUTER JOIN, I get all company records even if there is not a related person, as shown in the following print screen.

I achieve these results by simply right clicking over the join between the two tables and selecting the option “Select All Rows from Company”, as shown in the following print screen.  Once the join is changed, click the explanation point button to refresh the results shown at the bottom of the following print screen.

Notice that the total record count at the bottom of the above print screen has jumped from the previous total of 1302 to 1319. 

 
The first row of the record set in the print screen above shows in the second column (field name Comp_PrimaryPersonID) has a NULL value within it.

Scenario 3:

What about if I want ALL contacts at each company, what two fields would we link together?

I first remove the existing join between Comp_PrimaryPersonID and Pers_PersonID.  Now we will add a join between Comp_CompanyId and Pers_CompanyId.  I also went ahead and did a LEFT OUTER JOIN by right clicking over the join between the two tables and selecting the option “Select All Rows from Company”. 

The above results show where Every Company exists with OR without a Person Record Associated to it.

Scenario 4:

Taking the previous scenario one step further what if the customer wants ALL companies with or without a person record AND all persons even if they are not associated to a company?

In order to achieve this scenario we will need to right clicking over the join between the two tables and selecting the option “Select All Rows from Person”, as shown in the above print screen.

The above results show where Every Company exist with OR without a Person Record Associated to it and where Every Person Exist regardless of Company relationship.  So as a result I get ALL companies and ALL people regardless of a relationship existing or not existing.  In the above print screen we can see that our total count increased by one from 1926 to 1927.  Which translates to ONE person inside of SageCRM is NOT linked to a Company.  Or in SageCRM terminology we have ONE person that is an Individual.

Summary Scenarios:

So based on the results from the above scenarios, it is important to understand that a simple join completely alters the result set shown for a simple view.

Applying view created to SageCRM:

Now that we have a working view lets now add that view into SageCRM.  Lets use the view created from Scenario 3 above.  In order to do this we have to reset the joins to match the values for Scenario 3.  In the following print screen I have done so.  At this point we also need to select the code that was actually created by the selections made.

Now that the code is actually selected, right click and select copy as shown in the following print screen.

Once the code is stored in the computers memory we need to launch and login to SageCRM as a user with Administrator rights.

Within SageCRM navigate to Administration | Customization | <Entity to store view under> |
Views Tab and select the New View button.  In our example our view is pulling all companies and all their related persons so we will navigate to Administration | Customization | Company | Views Tab.

To read up on all the definitions for the fields shown on the above screen see chapter titled “Views” within the  “System Administrator Guide.pdf”.  This chapter goes into great detail of what all of these fields are used for.
For our purposes we are going to keep this very basic.  The following values need to be typed in before pasting the code into the View Script area.

  • View Name: vCompanyAllPersons
  • Description: This view shows all Company records with related Person records.
  • Reports View: Check this box
  • Groups View: Check this box (Optional NOT required)
  • Translation: Company Records and All Persons

At this point the screen should match the following print screen.

On the previous print screen you will see a high lighted area of the View Script, at this point in the
process please remove the highlighted area ONLY, as shown in the following print screen.

Now in the print screen above you can see that the two lines of script have been removed.  At this point paste the SQL Code that we copied from SQL Server Management Studio into the View Script area below the “AS” line shown in the previous print screen.  See the following print screen for the final results.

At this point click on save and the view should then be available within the list of views for the Company Entity, as shown in the following print screen.

Where can this view be used.  Remember we checked the Reports View check box earlier before
saving the view. 

Navigate to Main Menu | Reports | General Report Category. 

Once there click the New Report button. 

At this point the following screen with the newly created View as an option within the “Source View” selection list of values.

Why does the above print screen show in the selection list “Company Records and All Persons” instead of the views actual name “vCompanyAllPersons”? 

The value shown in the selection list is the Translation we provided when we were creating the view.

Once the view is selected now all the available fields specified earlier are available for creating a report.  See the following print screen.

To learn more about writing reports see the Report Writing chapter within the
“System Administrator Guide.pdf”.

Summary:

The purpose of this whitepaper is to provide instruction on how to write a SQL View for use within SageCRM.  This is designed for SageCRM Certified Consultants who are not programmers or are just learning how to write SQL Statements for SageCRM and do not know Transact SQL.

Published Mar 09 2009, 04:18 PM by Rob

Comments

 

SageCRM - The Next Level said:

This was a recent question I got from a business partner. When you create a derived field in a SageCRM

April 16, 2009 3:42 PM
 

Hints, Tips and Tricks said:

The following is a survey of all the different articles that have discussed creating and editing views

July 1, 2009 10:38 AM

About Rob

Rob Lawson, a Field Sales Engineer III for Sage Business Solutions division is considered to be one of the true experts on the SageCRM product. Mr. Lawson has been with Sage for over 7 years and is a Sage Accpac Certified Consultant in SageCRM, Sage Accpac ERP, Sage Accpac ERP Project and Job Costing. He is also a Sage Accpac Certified Product Trainer, Sage Accpac Certified Sales Consultant, Sage SalesLogix Certified Engineer, and a Certified Pervasive Professional. He has a background in both Accounting and Information Systems and received a BBA in Accounting from Georgia State University. Currently, Mr. Lawson provides pre-sales technical assistance to customers and partners for SageCRM, Sage Accpac ERP, and Sage AccpacOnline. He also works closely with the development teams of SageCRM and Sage Accpac ERP to help bring the front line experience of the pre-sales team from the customer and partner perspective into view. While in previous positions within Sage he has provided pre-sales assistance on SageCRM.com, SalesLogix, SalesLogix Web Client, and Sage Mobile. Additionally, he regularly publishes articles for the SageCRM Community which can be found on the Accpac BLOG Community site and the SageCRM Development Partner Program (DPP) BLOG Community site. He also continues to teach the SageCRM Business Partner Community via public webinars, pre-recorded “videos on demand”, and classroom courses. Prior to joining Sage, Mr. Lawson has worked as a project manager, staff accountant and consultant for public accounting firms for eight years. This included work in areas such as sales, business analysis, implementations, customization, report writing, web site development, software and hardware support, and database conversions. In his free time Mr. Lawson enjoys playing golf, reading, and working on computers.