Creating a Record Using C# .NET and the Salesforce REST API
Tags: csharp, salesforce, rest, dotnet
Published: 2020-08-25 17:28:08

---
title: Creating a Record Using C# .NET and the Salesforce REST API 
published: true
description: We use the application we have been building in this series to create a new Account in our Salesforce org.
tags: csharp, salesforce, rest, dotnet
series: Salesforce REST API Services
---

<i>This article is a continuation of the Salesforce REST API Services series. In this article we are operating under the assumption that you have already set up your Salesforce org and built an application that successfully authenticates with Salesforce and logs in. The steps in this tutorial will not work without the resulting AuthToken from your Salesforce org.</i>

In this tutorial we'll be creating a new Account record in our Salesforce org using our C# .NET application. Creating a record in an external application is incredibly useful for automated processing of data such as customer orders or shipments, allowing the application to perform the work of adding records that do not already exist.

<h1>Creating A Record</h1>
As with the other examples, we will encourage re-usability in our code with a CreateRecord() method and use our API Endpoint, Service URL, and AuthToken for our connection to the Salesforce org.

      private string CreateRecord(HttpClient client, string createMessage, string recordType)
      {
            HttpContent contentCreate = new StringContent(createMessage, Encoding.UTF8, "application/xml");
            string uri = $"{ServiceUrl}{ApiEndpoint}sobjects/{recordType}";

            HttpRequestMessage requestCreate = new HttpRequestMessage(HttpMethod.Post, uri);
            requestCreate.Headers.Add("Authorization", "Bearer " + AuthToken);
            requestCreate.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
            requestCreate.Content = contentCreate;

            HttpResponseMessage response = client.SendAsync(requestCreate).Result;
            return response.Content.ReadAsStringAsync().Result;
      }

Now that we have the framework to send a Create request to Salesforce, we can build a method to create an Account with the different pieces of customer data we need to populate. These fields and values will vary greatly depending on your needs, but I've given you a couple of fields that will typically be used. These values won't be hard coded as seen in the below example, they would typically be passed from your database, an order payload, or other method of customer data being passed to the application for processing. 

      private string CreateAccount(HttpClient client, Order order)
      {
            string companyName = "Bob's Builders";
            string phone = "123-456-7890";

            string createMessage = $"<root>" +
                $"<Name>{companyName}</Name>" +
                $"<Phone>{phone}</Phone>" +
                $"</root>";

            string result = CreateRecord(client, createMessage, "Account");

            XDocument doc = XDocument.Parse(result);
            string id = xmlHelper.RemoveTags(doc.Root.FirstNode.ToString());
            string success = xmlHelper.RemoveTags(doc.Root.LastNode.ToString());

            if (success != "true")
            {
                logger.SalesforceError("Create", "Account");
                return null;
            }

            logger.SalesforceSuccess("Create", "Account", id);
            return id;
      }

You may have noticed that the code in this tutorial looked very similar to updating and querying records in the previous tutorials. This leads me to a potential later project where I may look into building a single "SalesforceConnection" method that can perform all of these functions without all the very similar code.

For now though, this marks the end of this tutorial series. Let me know what other Salesforce topics you'd like to read or learn about!

--

If you'd like to catch up with me on social media, come find me over on [Twitter](https://twitter.com/RachSoderberg) or [LinkedIn](https://www.linkedin.com/in/rachelsoderberg/) and say hello!