Login / Register  search  syndication  about

          Kris Halsrud's Blog

Kris Halsrud on development and Integration with CRM and Development

Distributing SQL Views to Remote SalesLogix Databases

There have been a couple occurrences where a client has required SQL views be used within their SalesLogix system to retrieve complex data queries.  Normally this is fairly straight forward for a network only SalesLogix implementation, however in some cases these clients have also had remote databases that also need these views.  How can these be distributed?

If you have worked with SalesLogix and creating remote databases, you will know that SalesLogix does not create SQL views as part of the standard database creation.  If you have implemented SQL views in your database that the remotes must also use, you are tasked with creating these views. 

Now there are several ways of handling this, you could simply create a script that you run on the SalesLogix remote system when attaching the database, however you need to remember to do this each time a new SalesLogix remote database is created.  Also running the scripts is often problematic, in that you don't have access to the system to run the scripts yourself, or because of the type of SQL on the remote computer you do not have access to tools to run scripts.  While there is ISQL, you sill need to perform manual steps after every database installations.

Here is one approach we had taken in the past, while it probably could be improved upon, it should give some ideas about how you can automate distribution of SQL views.

This approach utilizes standard SalesLogix scripts to check for the existence of the required views, and if not found, creates them automatically.

First, on the OnOpen event of the form that utilizes the views we add a method, "FXCheckViews" to check on the existence of the views.  While you could place this in several places, including the System Logon event, putting it on the OnOpen event only causes the script to be run if the user goes to the screen that needs the views.

Sub MainAXFormOpen(Sender, ID)

   FXCheckViews
   application.BasicFunctions.ProcessWindowMessages

   'Other code goes here

End Sub

The FXCheckViews routine is defined as follows:

Sub FXCheckViews
dim objrs

    Set objrs = GetNewRecordset()
        objrs.Open "select id from sysobjects where name = 'FXLastActivity' and type='V'"
        if(objrs.eof or objrs.bof) then ExecuteNativeSQL("CREATE view sysdba.FXLastActivity as select convert(varchar(10),max(startdate),101) activitydate, contactid from activity with(NOLOCK) group by contactid")
    Set objrs = nothing

End Sub

The FXCheckViews uses a couple of custom routines, "GetNewRecordSet" and "ExecuteNativeSQL".  I will discuss those below.  The routine will check to see if the view exists in the SQL system catalog.  If it does not exist then it runs a T-SQL create view command to insert the view into the database.

"GetNewRecordSet" returns a RecordSet object and is defined as:

Function GetNewRecordset()
    Set GetNewRecordset = CreateObject("ADODB.Recordset")
    Set GetNewRecordset.ActiveConnection = GetNativeConnection()
    GetNewRecordset.CursorLocation = 3 'adUseClient
    GetNewRecordset.CursorType = 3 'adOpenStatic
    GetNewRecordset.LockType = 3 'adLockOptimistic
End Function

Notice the custom call to "GetNativeConnection", this is another custom routine I will discuss in a little bit.  Also, notice that the RecordSet's properties are set using the numeric value rather than the VB Constant. (This is important as on Vista or server 2008 systems the constants are no longer inherently supported).

Now if you refer back to the FXCheckViews routine you will see it uses "ExecuteNativeSQL".  This function executes a SQL statement against the database using a native SQL connection (bypassing the SalesLogix OLEDB Provider).  This is because the SalesLogix OLEDB Provider does play nice with database alteration commands like CREATES, DROPS, ALTER, etc.  So to get around this restriction we use a native SQL connection.  Here is the sample "ExecuteNativeSQL", which accepts an input of the SQL statement to be executed:

Function ExecuteNativeSQL(ByVal SQL)
Dim objConn
Dim lRecs

    On Error Resume Next

    lRecs = -1
    Set objConn = GetNativeConnection
    objConn.Execute SQL, lRecs, 128 'adExecuteNoRecords

    If Err.Number <> 0 Then RaiseError "executing SQL statement"
    On Error Goto 0

    Set objConn = Nothing
    ExecuteNativeSQL = lRecs
End Function

Again, notice this function uses the "GetNativeConnection" routine.  Lets take a look at this, as this is key to getting around the SalesLogix OLEDB Provider.

Function GetNativeConnection()
Dim objRS
Dim connstring
Dim cn

    Set objRS = Application.GetNewConnection.Execute("slx_getNativeConnInfo()")
    connstring = objRS.Fields(0).Value & ";Password=" & SLXPW
    objRS.Close
    Set objRS = Nothing

    Set cn = CreateObject("ADODB.Connection")
        With cn
            .ConnectionString = connstring
            .Open
        End With

    If Err.Number <> 0 Then RaiseError "opening ADO connection object"
    On Error Goto 0

    Set GetNativeConnection = cn
    Set cn = nothing

End Function

Now within this function we can see it is creating a RecordSet that is populated with a SalesLogix built-in procedure, "slx_getNativeConnInfo()".  This procedure returns the underlying native SQL connection string that is used by the SalesLogix OLEDB provider.  As described in this post, the underlying connection string has the password parameter omitted from the returned string.  To get around that we append the provided connection string with the password, which is stored in a constant, "SLXPW":

Const SLXPW = "mypassword"

So there it is.  One approach to automatically distributing SQL views out to SalesLogix remote databases.  This approach does have a couple disadvantages:

  • You need to store the SQL password in the script.
  • You must hard-code the T-SQL create view statement in your code.

Aside from these issues, it is a pretty slick way of getting views out to users.  This could be made more elegant with more time.  Hopefully it might spur some more ideas.

What's This?
  
Bookmark and Share

About Kris Halsrud

   Kris Halsrud is a Senior Analyst / Developer for Customer FX Corporation.


Related Content
   What is the name of the Complete an Activity Form in SalesLogix?
I am trying to customize the "Complete an Activity". However, I am unable to find it. Can someo
Posted on May 16, 2012 by SalesLogix Support to SalesLogix Questions & Answers
 
   Is it possible to do a SalesLogix Mail Merge on Lookup Results without saving them as a Group?
Is there a way to do a mail merge based on lookup results. For example, let's say I do a look up by C
Posted on May 16, 2012 by SalesLogix Support to SalesLogix Questions & Answers
 
   How does the Record Changes property work on a datagrid?
Can anyone explain why the 'record changes' property check box on a datagrid (in my case Opportun
Posted on May 14, 2012 by SalesLogix Support to SalesLogix Questions & Answers
 
   How can I create seperate icons that can be used depending on which remote office they are at? Are there specific parameters that I can use in the Shortcut?
I have users that travel between offices that are running SalesLogix remote office and would like to crea
Posted on May 14, 2012 by SalesLogix Support to SalesLogix Questions & Answers
 
   What is wrong with my sql Statement?
When I try to execute the following sql in script i get the error failed to parse sql. However, if i r
Posted on May 11, 2012 by SalesLogix Support to SalesLogix Questions & Answers
 
Comments

 

Ryan Farley said:

Great post Kris.

May 13, 2009 1:13 PM
 

The Reporting Blog said:

The scenario is you have created a SQL view for use in a Crystal Report. Your end users are excited about

September 18, 2009 1:48 PM
 

The SalesLogix Product Blog said:

Advice for a SalesLogix Administrator. One of my co-workers recently wrote a blog about syncing SQL views

September 22, 2009 2:13 PM

Leave a Comment

(required)  
(optional)
(required)  
Add
All contents Copyright © 2012 Customer FX Corporation
Customer FX Corporation
2324 University Avenue West, Suite 115
Saint Paul, Minnesota 55114
Tel: 800.728.5783

  Follow @CustomerFX on twitter
Follow the best news, tips, and articles
  Subscribe to Customer FX on youtube
Watch SalesLogix tutorial videos from Customer FX
Login / Register