11/21/2024 7:50:51 PM
slxdeveloper.com
Now Live!
|
|
|
Using Code Templates in Saleslogix 6.x to Develop Class Objects |
|
Description: |
How many countless hours have you spent writing nearly the same code, over and over again. How often have you replicated nearly identical code to perform nearly the same operation to different part of the system? Well, those days may just be over! This document will describe how you can shave hours in not days of development time by utilizing code templates. This article will introduce you to a powerful tool called CodeSmith.
|
Category: |
SalesLogix VBScript Articles
|
Author: |
Scott Mercill
|
Submitted: |
7/22/2004
|
|
|
Stats: |
Article has been read 29340 times
|
Rating:
- 5.0 out of 5 by 3 users |
|
|
|
fiogf49gjkf0d
How many countless hours have you spent writing nearly the same code, over and over again. How often have you replicated nearly identical code to perform nearly the same operation to different part of the system? Well, those days may just be over! This document will describe how you can shave hours in not days of development time by utilizing code templates. This article will introduce you to a powerful tool called CodeSmith [1], by Scott Mercill and Brett Murphy.
CodeSmith is a template-based code generator that allows a developer to generate code for any ASCII-based language. CodeSmith can be used to generate SQL statements, html web pages, XML Files, C# or VB.NET Classes, as well as VBScript code, which Saleslogix 6.x uses. Anything that can be represented in ASCII CodeSmith can create. The code generated can be customized by the use of custom properties. Say you had a template that generates a SQL statement for a database table a custom property could be defined that would conditionally remove/add the database owner to the table.
CodeSmith has another benefit; it helps to enforce coding standards by automating code generation. Code generated by CodeSmith will have a consistent format and design methodology thus ensuring that you company’s standards are being met.
So take some time and download CodeSmith from http://www.ericjsmith.net/codesmith/ and go through the tutorial which can be found at http://www.ericjsmith.net/codesmith/tutorial.aspx.
The explorer version of CodeSmith is free. There is also a professional version that costs fewer than 100 dollars which provides an IDE to develop your own templates, although you can use notepad to develop your own templates.
For this article we created a CodeSmith template, SLXData.cst that builds a vbscript class file for use with Saleslogix. The SLXData.cst file is a proof of concept template (See Using Code Templates in Saleslogix 6.x to Develop Class Objects Sample Template or Related Articles section below). We make no claims on its usefulness in a production environment.
By now you should have download CodeSmith and gone though the tutorial and now have a basic understanding of CodeSmith and what it can do. For this article we will cover the operation of the Saleslogix Data Template (SLXData.cst) as well as some of its design attributes.
The SLXData.cst template will generate a vbscript class for a SalesLogix database table. Once the code is generated you then can easily paste it into Saleslogix’s Architect saving yourself considerable amount of time of you had to write the code from scratch.
The template has several interesting features, if the account or contact table is selected as its source table the template will add business logic to the class to generate the required address record a nice time saving feature. In addition if you select the account table the template will generate logic to Insert/update/delete the account summary record.
To find out more about creating VBScript classes in Saleslogix 6.x you should check out Ryan Farley’s article Using Classes in VBScript.
The First Thing that you need to do is launch the CodeSmith Explorer. Once the CodeSmith explorer is opened select open the SLXData.cst template. (See Figure 1 - CodeSmith Explorer Window)
Figure 1 - CodeSmith Explorer Window
A new window will open displaying the templates properties and the code generation window. (See Figure 2 - CodeSmith Code Generation Window)
Figure 2 - CodeSmith Code Generation Window
Let’s take a minute to look the generation window for the SLXData.cst template. As you can see on the left we have some user defined properties.
• PrimaryKey
o This property is required if the SourceTable does not have a primary key. Otherwise, you can leave this blank.
• SourceTable
o This is the table you wish to generate code against. Consult CodeSmith documentation about configuring a database.
• AddressClassName
o This property is only utilized if your SourceTable is either Account or Contact. What this property allows, is the ability to access the account or contact’s address directly. It is required that you execute the template against the address table for this to work properly.
• DeveloperCompany
o The name of the company you work for. Used for comments within the code.
• DeveloperName
o Your name. Used for comments within the code.
• IncludeTableOwner (True/False)
o Typically False, but set to True if you need to prefix database update statements with the table owner. (sysdba.)
• StrongTyped (True/False)
o Select True if you would like variables to be declared with a type. Set to False for VBScript.
By now you may have generated several sample classes for various Saleslogix table and noticed that the output varies depending on what source table is selected. So why don’t we take a look at how this is done. Go ahead and open up the template in notepad or if you have CodeSmith Studio you can use that as well.
If you remember from the last section we discussed the user defined properties at the top of the template you will notice several line that will look similar to the following line:
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
Category="Context" Description="Table that the object is based on." %>
If you went through the CodeSmith tutorial you will remember that the preceding line defines a user defined property list take some time and see how this property is used in the template.
If you generated the account class you will notice that all of the private member variables were generated at the top of the class:
Private pvACCOUNTID 'As String
Private pvTYPE 'As String
Private pvACCOUNT 'As String
Private pvDIVISION 'As String
Private pvSICCODE 'As String
Private pvPARENTID 'As String
Private pvDESCRIPTION 'As String
Private pvADDRESSID 'As String
Private pvSHIPPINGID 'As String
Private pvREGION 'As String
Private pvMAINPHONE 'As String
Private pvALTERNATEPHONE 'As String
You may be asking yourself how this was done. Well, the SourceTable property is of type SchemaExplorer.TableSchema which is a collection. That contains a listing of the table fields. So looking at the following code we can see how the private member variables are created.
<%Dim cs as ColumnSchema %>
<%Dim sQues as String %>
<%for each cs in SourceTable.Columns %>
Private <%=GetSpacedVariableName("pv" & GetColumnName(cs)) %> <%=CommentLine(StrongTyped) %>As <%=GetVariableDataType(cs)%>
<% Next %>
As you can see the code that generates the private member variables is a simple for each loop. The only tricky thing is that you just have to remember to wrap the generation code in the <% %> tags and if you are calling a function inside the <% %> you must include the = in the first tag <%= %>. If you are a Classic ASP developer this will seem like old times.
At this point spend some time reviewing the rest of the template to see how it works. In the next section will walk though how to use the template.
The First thing that you want to do is select your SourceTable by clicking the ellipsis button to the right of the field to display the Table Picker Window. (See Figure 3 - CodeSmith Table Picker Window). If this is the first time you have used CodeSmith you may receive an error. Its nothing to worry about you just need to define a data source. To define a data source select the ellipsis button to get the data source manager window. (See Figure 4 - CodeSmith Data Source Manager Window) Select Add to create a new data source and enter in a meaning full name, select the SQLSchemaProvider from Provider type and then enter in the connection string. (See Figure 5 - CodeSmith Data Source Window)
If you don’t know the format for the connection string look at the one provided (See Table 1 - Sample Connection String)
Data Source={ENTER SERVERNAME};database={ENTER DATABASE NAME}; uid=sysdba; pwd=masterkey;
Note: You should use "sysdba" as your uid for the connection string.
Table 1 - Sample Connection String
Figure 3 - CodeSmith Table Picker Window
Figure 4 - CodeSmith Data Source Manager Window
Figure 5 - CodeSmith Data Source Window
The vbscript class generated by CodeSmith will generate each table field as a class property. All properties will be prefixed with AIS_ as to prevent conflict with any VBScript reserved words. For instance, if you were to generate a class based on the account table the Type field would conflict with vbscript’s reserved word Type. So when accessing a field named ACCOUNT would be accessed in the following manner obj.AIS_ACCOUNT = "Ancala Information Services".
Note: During the design of the code template we chose to hard code the AIS_ prefix for properties instead of using a user defined property. We wanted a standard prefix for all properties. We felt that code consistency was more important than developer freedom.
There are also 5 methods and functions to assist with your data manipulation needs.
• Clear
o Reinitialize the class set all properties values to their initial state.
• Load (ID)
o One Parameter ID. Pass in the primary key value or the user defined key. This method will populate all properties with the corresponding database record. Returns a 1 if successful or a 0 if it failed.
• Add (CreateID)
o One Parameter CreateID. Pass in True/False. If True then will generate a new Saleslogix Table ID. If False will use the value stored in the primary id property. In the case of the Account Table it would be AIS_AccountID.
• Delete
o Deletes the current record from the database. Uses the value that is stored in the primary id property. In the case of the Account Table it would be AIS_AccountID. Returns a 0 for a failure and a 1 if successful.
• Update
o Requires a record to be loaded. Updates the SalesLogix database with any data changes. The template is smart in that it will only execute an update against any fields that were actually modified by you. Returns number of records affected (usually 0 or 1). If 0 then update failed.
Up until now we talked a lot about CodeSmith and building a vbscript class to use with Saleslogix, so now what? Well we need to create a new script file in Saleslogix and paste in our code. Once the code is saved we then can add it as an include script to another script or view. For more information on including scripts to a Saleslogix script please refer to Saleslogix documentation.
To demonstrate using the CodeSmith template, let’s assume that we used CodeSmith to generate a class based on the account table called ACCOUNTData, remember the SLXData.cst template requires account and contact table classes need to have an associated Address class we will call it ADDRESSData. See SLXData Template User Defined Properties for more information about the . All files have saved to Saleslogix and includes have be correctly defined.
Dim Acct 'Account object
Dim sNewID 'NewID to hold new Saleslogix ID
Dim s1 'Holds Return Value from input Box
Set Acct = New AccountData 'Create Our Object
'Calling the Clear Method to make sure all values are initialized
Acct.Clear
'Setting Properties
Acct.AIS_Seccodeid = "SYST00000001"
Acct.AIS_AccountManagerID = Application.BasicFunctions.CurrentUserID
s1 = InputBox("Enter Account Name:")
Acct.AIS_Account = s1
s1 = InputBox("Enter Type:")
Acct.AIS_Type = s1
s1 = InputBox("Enter Phone Number:")
Acct.AIS_MainPhone = s1
s1 = InputBox("Enter Status:")
Acct.AIS_Status = s1
s1 = InputBox("Enter Address1:")
Acct.SLXAddress.AIS_Address1 = s1
s1 = InputBox("Enter City:")
Acct.SLXAddress.AIS_City = s1
s1 = InputBox("Enter State:")
Acct.SLXAddress.AIS_State = s1
s1 = InputBox("Enter Zipcode:")
Acct.SLXAddress.AIS_PostalCode = s1
Acct.AIS_CreateDate = Now
Acct.AIS_ModifyDate = Now
Acct.AIS_CreateUser = application.BasicFunctions.CurrentUserID
Acct.AIS_ModifyUser = application.BasicFunctions.CurrentUserID
sNewID = Acct.Add(True)
If sNewID <> "" then
Application.BasicFunctions.SetCurrentAccountID sNewID
End If
Set Acct = Nothing
In the next example we will use the class object to insert a new note for an account.
Dim Acct 'Account Object
Set Acct = New AccountData 'Create Our Object
'Calling the Clear Method to make sure all values are initialized
Acct.Clear
'Populating new object with current Account record
Acct.Load Application.BasicFunctions.CurrentAccountID
sNotes = Inputbox("New Notes", "Account Notes", Acct.AIS_Notes)
'Adding a new note
Acct.AIS_NOTES = sNotes
'Updating the account object
Acct.Update
Set Acct = Nothing
'Refresh main view to see new note record.
Application.BasicFunctions.RefreshMainView
As you can see the class object reduces the amount of code that a developer would normally have to write, thus reducing coding errors.
We hope that two main points were discovered from this article. First that CodeSmith is a powerful tool in the development of code templates. Since we have been using CodeSmith here at Ancala Information Services we have found many uses for it. CodeSmith has been an invaluable tool it has reduced the amount of unit testing that we have to do on a project, allowing us to deliver a better solutions on time and under budget. Second using class objects in Saleslogix is a great time saver.
We feel that CodeSmith on the average saves us about 30% in development once you factor in the time it took you to develop the template in the first place. But over time the savings increase the longer you use the template. Its also can be looked at as manpower multiplier a suite of well designed templates will allow single developer to considerably more productive.
[1] Ancala Information Services has no affiliation with the author of CodeSmith or any of its related entities. We are just excided about this tool we wanted everyone to know about it. |
|
|
|
Rate This Article
|
you must log-in to rate articles. [login here] 
|
|
|
Please log in to rate article. |
|
|
Comments & Discussion
|
you must log-in to add comments. [login here]
|
|
|
- subject is missing.
- comment text is missing.
|
|
| Re: Using Code Templates in Saleslogix 6.x to Develop Class Objects Posted: 7/22/2004 1:03:46 AM | fiogf49gjkf0d Really awesome stuff Scott & Brett! Now this is the kind of stuff I love to see. Time to bring SLX development up-to-date and take advantage of great tools like CodeSmith.
Great job! (BTW, I am fixing the site so I can list multiple authors for an article. Don't want to leave you out Brett)
-Ryan | |
|
| Re: Using Code Templates in Saleslogix 6.x to Develop Class Objects Posted: 7/22/2004 10:52:15 AM | fiogf49gjkf0d I think this is great! I've also been trying to use some kind of code generation scheme to do my "dirty work" for me for some time.
I already have classes similar to what Scott generates using the template albeit untyped, meaning I access my fields as Acct.Fields("ACCOUNTMANAGERID") vs. Acct.AIS_AccountManagerID. Clearly, the latter is a better approach in most cases since knowing which fields are available compile time is better than finding out at run time. However, this is kind of moot since SLX environment is unable to take advantage of the latter approach since intellisense can't confirm our custom class members as it does the built-in objects. Also, even if you did make a mistake typing Acct.AIS_AccountManagerIT(note the typo), you won't know that you did this until you run the code.
ok so this is sounding like a feature request again but I would like the SLX guys to release info on their architect environment so that we can develop extensions. The architect is clearly lacking in many features. Even if they just released the spec on their plugin model, I'd be happy or an API to create/update a new plugin would be great because then I can have a code gen scheme generate all my classes as above and create/update vbscript plugins automatically.(I actually know that I can now but I want an official way to do this) Imagine this - every time you create/update tables, you can run the process and your classes will be ready for use right away!
In any case, kudos to you for the awesome stuff and bring us that much closer to our modern development! If only we had a way to use TDD with vbscript...
| |
|
| Re: Using Code Templates in Saleslogix 6.x to Develop Class Objects Posted: 7/22/2004 11:16:01 AM | fiogf49gjkf0d To comment on what Jiho Han stated I also do wish Saleslogix development would open up architect, but after working for saleslogix and knowing the hoops development has to go through to do anything. I don't see that happening. Also, to address the intellisense issue with custom class members is to use the VB 6 IDE. I know its a hack but I also use it as a source control solution. One thing I hate to loose code because someone overwrote my code.
| |
|
| Re: Using Code Templates in Saleslogix 6.x to Develop Class Objects Posted: 7/22/2004 3:52:20 PM | fiogf49gjkf0d Scott,
That's an interesting idea. Actually one of my co-worker uses a third party editor - textpad or something - but I didn't want to copy and paste constantly when there weren't enough values to switch. Hmm... you actually gave me another idea altogether but I just don't know how or if it's possible. I want to stay in VS.NET all the time if I can but I don't know whether I can extend intellisense to support vbscript(or saleslogix intrinsic objects). It might involve vs extensibility api etc. Hmm....
Too bad about the architect situation but I guess you would know if you were inside at one time. | |
|
| Re: Using Code Templates in Saleslogix 6.x to Develop Class Objects Posted: 7/22/2004 5:22:16 PM | fiogf49gjkf0d Jiho,
First of all, TextPad is great. I use it too. You can build clip libraries of commonly-used code so it just inserts entire blocks of a predefined code structure. Syntax highlighting and all etc.
Second, as far as getting intellisense in the VS IDE, there really is no need for extensibility, all you need is a class containing stubs for the SLX functions as static members. For example, if you wanted to program in the VS.NET IDE, you could add a class named "BasicFunctions" in the "Application" namespace (or just alias the Imports statement as Application - or you could create Application as a class with a static property your BasicFunctions class object to make things more accurate) with static (or shared in VB terms) members like this:
Public Shared Function EditActivity(ByVal ActivityID As String) As Boolean Return True End Function
Public Shared Property CurrentAccountID As String Get Return String.Empty End Get End Property
Get the idea? Then just add the class or reference the DLL containing the class and away you go, you now have intellisense for the SLX functions and objects. Sure it is tedious to create the stubs for all the functions, but you only need to do it once. For using the VB6 IDE it is even simpler. Just add a module and stub out the functions as public functions. Easy enough.
I'd go nuts if SLX opened up the Architect with a pluggable architecture. I'd be developing my own enhancements like crazy. If they'd just expose some APIs for dealing with plugin development I'd just write my own IDE altogether ;-) Wouldn't that be great (but like Scott, I don't see that happening, but maybe if we squeak enough?) | |
|
| Re: Using Code Templates in Saleslogix 6.x to Develop Class Objects Posted: 7/22/2004 5:27:04 PM | fiogf49gjkf0d Yo, that'd rule if SLX did that.
In any case, you wouldn't happen to have one of those stubbed out dlls lying around would you? :) Ahh.. I guess I got some mad typing to do... | |
|
| Re: Using Code Templates in Saleslogix 6.x to Develop Class Objects Posted: 7/23/2004 8:50:52 AM | fiogf49gjkf0d Check this out!
What you said inspired me and led me to this solution. Simply add a reference to SalesLogix.exe(since it's com) in VB6 IDE and everything is there:
Dim Application As SlxApplication strAccountID = Application.BasicFunctions.GetIDFor("ACCOUNT")
It should also be possible to do this using VS.NET. | |
|
| Re: Using Code Templates in Saleslogix 6.x to Develop Class Objects Posted: 7/23/2004 8:56:55 AM | fiogf49gjkf0d Jiho,
I am glad I inspired you to a to use VB6 IDE as your editor. I tried awhile ago to do the same with VS.NET but I ran into the issue that VB.NET syntax is slightly different then VB6/VBScript. If you figure out a way let me know.
-Scott | |
|
| Re: Using Code Templates in Saleslogix 6.x to Develop Class Objects Posted: 7/23/2004 9:40:12 AM | fiogf49gjkf0d Jiho,
Not a bad idea. That definately saves some time from creating the stubbed functions. I really don't develop much in the Architect, but I suppose that at least this gives you a better place to write out the code. However the differences between VB and VBScript could lead to some frustrations. It sure would be nice if they'd just open up the Architect to extending via custom DLLs or a plugin SDK of sorts.
-Ryan | |
|
|
|
|
|
Visit the slxdeveloper.com Community Forums!
Not finding the information you need here? Try the forums! Get help from others in the community, share your expertise, get what you need from the slxdeveloper.com community. Go to the forums...
|
|
|
|
|
|