DEV Community

Rachel Soderberg
Rachel Soderberg

Posted on

Creating a Record Using C# .NET and the Salesforce REST API

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.

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.

Creating A Record

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;
  }
Enter fullscreen mode Exit fullscreen mode

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;
  }
Enter fullscreen mode Exit fullscreen mode

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 or LinkedIn and say hello!

Top comments (3)

Collapse
 
cmuntada profile image
cmuntada

Have you been able to insert PersonalAccounts?
I don't know if it can be achieved in one single call or I do need to first create an Account and then a Contact. Being able to do both things in one call would be great. Any suggestion?

Collapse
 
sabirsheikh007 profile image
sabirsheikh007

How to insert multiple records

Collapse
 
asif110 profile image
asif110

Do you have the complete VS solution somewhere?