0

Is there a step by step example , preferably in c# , which shows how I can move over half a millions records from sql table to a Salesforce entity, namely Account using the Rest Bulk Api ? I have setup a connected app , and have the tokens , can login and perform a select quey , but I would like to be able to perform a create of these records by selecting from the sql table.

Program.cs:

public class Program
{
    private static SalesforceClient CreateClient()
    {
        return new SalesforceClient
        {
            Username = ConfigurationManager.AppSettings["username"],
            Password = ConfigurationManager.AppSettings["password"],
            Token = ConfigurationManager.AppSettings["token"],
            ClientId = ConfigurationManager.AppSettings["clientId"],
            ClientSecret = ConfigurationManager.AppSettings["clientSecret"]
        };
    }

    static void Main(string[] args)
    {
        var client = CreateClient();

        if (args.Length > 0)
        {
            client.Login();
            Console.WriteLine(client.Query(args[0]));
        }
        else
        {
            client.Login();
            Console.WriteLine(client.Describe("Account"));
            Console.WriteLine(client.Describe("Contact"));
            Console.WriteLine(client.QueryEndpoints());
            Console.WriteLine(client.Query("SELECT Name from Contact")); 

           // can i create here since i have the instance ?                
        }
        Console.ReadLine();
    }
}

SalesforceClient.cs

public class SalesforceClient
{
    private const string LOGIN_ENDPOINT = 
    "https://login.salesforce.com/services/oauth2/token";        
    private const string API_ENDPOINT = "/services/data/v25.0/";


    public string Username { get; set; }
    public string Password { get; set; }
    public string Token { get; set; }
    public string ClientId { get; set; }
    public string ClientSecret { get; set; }
    public string AuthToken { get; set; }
    public string InstanceUrl { get; set; }

    static SalesforceClient()
    {            
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
    }
    
    public void Login()
    {
        String jsonResponse;
        using (var client = new HttpClient())
        {
            var request = new FormUrlEncodedContent(new Dictionary<string, string>
                {
                    {"grant_type", "password"},
                    {"client_id", ClientId},
                    {"client_secret", ClientSecret},
                    {"username", Username},
                    {"password", Password}                        
                }
            );
            request.Headers.Add("X-PrettyPrint", "1");                
            var response = client.PostAsync(LOGIN_ENDPOINT, request).Result;
            jsonResponse = response.Content.ReadAsStringAsync().Result;
        }
        Console.WriteLine($"Response: {jsonResponse}");
        var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonResponse);
        AuthToken = values["access_token"];
        InstanceUrl = values["instance_url"];
        
    }

    public string QueryEndpoints()
    {
        using (var client = new HttpClient())
        {
            string restQuery = InstanceUrl + API_ENDPOINT;
            var request = new HttpRequestMessage(HttpMethod.Get, restQuery);
            request.Headers.Add("Authorization", "Bearer " + AuthToken);
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            request.Headers.Add("X-PrettyPrint", "1");
            var response = client.SendAsync(request).Result;
            return response.Content.ReadAsStringAsync().Result;
        }
    }

    public string Describe(string sObject)
    {
        using (var client = new HttpClient())
        {
            string restQuery = InstanceUrl + API_ENDPOINT + "sobjects/" + sObject;
            var request = new HttpRequestMessage(HttpMethod.Get, restQuery);
            request.Headers.Add("Authorization", "Bearer " + AuthToken);
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            request.Headers.Add("X-PrettyPrint", "1");
            var response = client.SendAsync(request).Result;
            return response.Content.ReadAsStringAsync().Result;
        }
    }

    public string Query(string soqlQuery)
    {
        using (var client = new HttpClient())
        {
            string restRequest = InstanceUrl + API_ENDPOINT + "query/?q=" + soqlQuery;
            var request = new HttpRequestMessage(HttpMethod.Get, restRequest);
            request.Headers.Add("Authorization", "Bearer " + AuthToken);
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            request.Headers.Add("X-PrettyPrint", "1");
            var response = client.SendAsync(request).Result;
            return response.Content.ReadAsStringAsync().Result;
        }
    }
}

thanks

5
  • 1
    Does this answer your question? Can I use SalesforceDX to load data into sandboxes?
    – identigral
    Oct 12, 2020 at 6:27
  • 1
    Export to CSV and then use sfdx force:data:bulk:upsert to upsert. See referenced answer for details.
    – identigral
    Oct 12, 2020 at 6:28
  • I have written a c# app that allows me to login and perform selects... I was looking at extending the app to allow create, update , delete of entities in Salesforce by setting up a rest request
    – Debbie
    Oct 12, 2020 at 6:31
  • What have you tried and where are you stuck? Post your attempt (C# code)
    – identigral
    Oct 12, 2020 at 8:04
  • If i post code here , for some reason it doesnt format correctly. I will post in my question above.
    – Debbie
    Oct 12, 2020 at 8:36

0

You must log in to answer this question.

Browse other questions tagged .