Skip to content

dbpullman/SalesforceSharper

Repository files navigation

SalesforceSharper

SalesforceSharper is a light-weight, easy to use asynchronous REST client for Salesforce written in .NET.

Installing

To add SalesforceSharper to your project, use the Nuget package on Nuget.org

https://www.nuget.org/packages/SalesforceSharper/

Usage

To use SalesforceSharper, you must authenticate before making any other method calls:

Note: Your password is a combination of your user password and security token

var auth = new UsernamePasswordAuthenticator("<ConsumerKey>",
                "<ConsumerSecret>",
                "<Username>",
                "<Password><SecurityToken>");

var authInfo = await auth.Authenticate();

var client = new SalesforceClient(authInfo.InstanceUrl, authInfo.AccessToken);

After authenticated, you can begin to use the client:

Query

The query method will return 2000 records matching a query. To query more than 2000 records, us the QueryAll<>() method (example below)

var accounts = await client.Query<Account>("SELECT Id, Name FROM Account");

Query All

The query all functionality will query all records (or the specified records in the LIMIT clause) and return more than 2000 records.

var accounts = await client.QueryAll<Account>("SELECT Id, Name FROM Account");

Create

To create a record:

var acct = new Account()
{
  Name = "Test Account"
};

var createdAccount = await client.Create<Account>(acct);

or

var createdAccount = await client.Create("Account", acct);

Update

To update a record:

var updatedAccount = new Account()
{
  Name = "Updated Test Account"
};

var updated = await client.Update<Account>("<recordId>", updatedAccount);

or

var updated = await client.Update("Account", "<recordId>", updatedAccount);

Delete

To delete a record:

var deleted = await client.Delete<Account>("<recordId>");

or

var deleted = await client.Delete("Account", "<recordId>");

Attribute Usage

There is a SalesforceObjectAttribute that can be used to name a class to match the object name in Salesforce. This is used in the Update, Create, and Delete methods to determine the object name and URL:

[SalesforceObject("Custom_Object__c")]
public class CustomObject
{
  public string Id { get; set; }
  public string Name { get; set; }
}

var customObj = new CustomeObject()
{
  Id = "1234567891011",
  Name = "Custom Object Name"
};

var created = await client.Create<CustomObject>(customObj);

The above method call would result in the following URL being used to make the API call /services/data/v45.0/Custom_Object__c

This allows you to name your objects what you want on the client side and helps keep the client side code cleaner and more readable.

Releases

No releases published

Packages

No packages published

Languages