Login / Register  search  syndication

          Ryan Farley's Blog

Ryan Farley on .NET Development with a focus on CRM Development for SalesLogix

Programatically Adding a New Record in SalesLogix Web

Something that is a common task in development, yet one that I get a lot of questions about, is how to programatically add a new record in SalesLogix Web using the entity model. This is an easy task, but unless you have some examples to learn from you might not know where to start. Let's take a look.

The EntityFactory is something you need to get familiar with. Get Reflector and dig around the Sage.Platform.dll to take a look. You'll use the EntityFactory to Create & Delete data. You can also use it to get a record by ID and to get a repository to query data in the entity model. Let's just take a look at using it to Create a new entity instance that we can save (which will add the new record to the database).

Scenario

Let's say we have an entity named Certification, which has a 1:M relationship to the Contact. We'll create a form under the contact where the user can choose a "certification" from a picklist and click an "add" button. We'll also put a DataGrid on the Form to display the certifications for the contact. When the "add" button is clicked we'll programatically create the certification record using the value the user selected in the picklist. Since the only selection to make is the certification, I like to do things like this instead of using a separate "add" form in a dialog that only contains a single control (the picklist). We've done away with the need to use an add dialog by just placing the picklist and add button controls right above the grid on the form itself.

The Code

The code that we'll execute on the Click of the add button would look as follows:

// make sure the user has selected a certification in the picklist
if (picklistCert.PickListValue != string.Empty)
{
// get a reference to the current Contact
Sage.Entity.Interfaces.IContact contact = this.BindingSource.Current as Sage.Entity.Interfaces.IContact;

// programatically create a Certification and save it.
// use the EntityFactory to create a new instance of the Certification entity
Sage.Entity.Interfaces.ICertification certification = Sage.Platform.EntityFactory.Create<Sage.Entity.Interfaces.ICertification>();
// now just set the properties & save
certification.Contact = contact;
certification.CertificationName = picklistCert.PickListValue;
certification.Save();

// add the certification to the contact's Certification collection.
// this is only needed to cause the grid bound to contact.Certifications to refresh
contact.Certifications.Add(certification);

picklistCert.PickListValue = string.Empty;
}
else
{
// if the user clicked the add button without first choosing a certification display a message
this.DialogService.ShowMessage("You must first select a certification to add. Please try again");
}

You can see that the only tricky part is knowing how to create the new entity instance using the EntityFactory (I've bolded that line in the code above). Once you have that it is just a matter of setting the needed properties and saving it. Much easier than using a SQL INSERT statement or even adding a new record to an updateable ADO Recordset like how you would do this in the LAN client.

What's This?
Bookmark and Share

About Ryan Farley

   Ryan Farley is the Director of Development for Customer FX Corporation and the creator of slxdeveloper.com.


Related Content
   Creating Pretty Prefixes and Suffixes in the SalesLogix web client
In the SalesLogix web client you may run across a reason you need to generate a “Pretty Key”. This is wh
Posted on Mar 17, 2010 by Kris Halsrud to Kris Halsrud's Blog
 
   SalesLogix Web Client- Working with Activities
In this webinar the user will learn to work with Activities from the Activities entity. This feature all
Posted on Mar 16, 2010 by Dale Richter to SalesLogix Training
 
   Adding an assembly reference to code files in the SalesLogix Application Architect
Often when adding custom code to SalesLogix you need to reference Assemblies that are not included by def
Posted on Mar 15, 2010 by Kris Halsrud to Kris Halsrud's Blog
 
   SalesLogix Web Client- Marketing Campaigns Part I [Video]
In this video webinar the Marketing Professional will learn how to create a New Marketing Campaign in Sal
Posted on Mar 15, 2010 by Dale Richter to SalesLogix Training
 
   Launching a report from a button in the SalesLogix web client
Starting in SalesLogix 7.5.1, Sage released an undocumented Reporting enhancement that allowed for intera
Posted on Mar 12, 2010 by Kris Halsrud to Kris Halsrud's Blog
 
Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add
All contents Copyright © 2010 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