Nothing But .Net with Force.com by Sara Morgan In the previous post of this Nothing But .NET series, I walked you through the various ways you can query Salesforce records using the new Force.com Toolkit for .NET. In this post, I will show you what you need to know to implement CRUD-like operations to create, update and delete Salesforce object records using the toolkit.

Creating the ForceClient

After a user has been authenticated, using either of the supported methods, a valid Instance URL, Access Token and API Version will need to be stored somewhere in the application. These values will be used when instantiating the ForceClient object. No matter what operation you want to perform, you start by generating a new instance of the ForceClient. The code to instantiate this object may look like the following:
ForceClient client = new ForceClient(instanceUrl, accessToken, apiVersion);

Creating Records

Creating new records is similar to querying them. You can either use a strongly typed or dynamically typed instance to define your record structure. For a strongly typed instance, you will need to create a class definition to represent the sObject you are working with. For example, if I want to insert a new Account record, I first need a class definition for Account, such as the following:

I can then insert a new record using code like this:

Make sure the object name you specify as the first parameter matches the API name that Salesforce has. If you are adding a record for a custom object named New_Object, then the API name should be something like, New_Object__c. And don’t forget to add code that checks to see whether the ID value returned is null. If it is, then you want to display a message to the user or perform some particular functionality.

If you want to go the dynamic route, you will need to use the ExpandoObject class which was made available with .NET 4. This allows you to dynamically assign properties at runtime and not specify class definitions for each record you create. For example, I could create a new Account record using the following code:

The obvious drawback to this method is that if I accidently type in one of the property names incorrectly, I will not find this out until the code actually executes. But as long as you put in proper exception handling and alternative code, this can be a very powerful alternative for working with records.

Updating Records

When you update a record, you will need to pass in a valid object name, record ID, and either a strongly typed or dynamically typed object. It also works with anonymous types as well. Once the update is complete, you will be returned a SuccessResponse result that is defined in the Salesforce.Common.Models namespace. For example, to perform an update of the Account record using an anonymous type and check whether the update was a success, you could do something like this:

You can use UpsertExternalAsync to either insert a new record or update an existing one based on the value of a specified external id field. While the record ID is not required here, the external ID is. The method accepts four parameters: a string value for the object name, a string value for the external field name, a string value for the actual external ID, and finally an object to be upserted. For example, to upsert a record and check whether the operation was successful you could use code such as the following:

Deleting Records

You can delete a record based on a specific record id and return a bool response to determine whether the operation was successful. For example, to delete a single record, you could use code such as the following:

The new Force.com Toolkit is very flexible and great for performing CRUD operations. It offers both strongly and dynamically typed alternatives for dealing with records. One of the biggest drawbacks is that if you want to work with strongly typed objects, you have to define those classes yourself.

But, I will give you a bit of a teaser here and tell you that if you tune in for the next post in this series, you will find out about a new technology that should make working with the Force.com Toolkit a little easier. But, that is all I will tell you for now. You will just have to wait until next week to find out more. In the mean time, you can check out more sample code on the GitHub website.

About the Author

Our guest blogger, Sara Morgan, is an independent Software Developer and long-time .NET programmer who has recently been wooed into the world of Force.com development. She recently filmed her first introductory course covering Visualforce for Lynda.com.

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS