You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Salesforce DML Operation Using DeveloperForce Toolkit for .Net


Introduction: 

If we want to do the basic operations like Create, Update, Delete and View records from Salesforce in .NET Console Application, it can be achieved by using REST API and DeveloperForce.Force in Visual Studio.  Below are the steps used to implement the above scenario:

Step1: 

If we want to access the Salesforce Lead object from a .NET Application, you must first install the Develperforce.force(REST API) in NuGet Packages.

Image

Step 2: 

Create connected apps by following the below steps in Salesforce:

1) Type Apps in Quick Find Box

2) Click on Apps

3) Create connected apps and provide necessary information

4) After the connected has been created, a Client ID and Client Secret will be generated in the detail page of this app.

Image

Step 3: 

Next, generate a Security Token based as explained below:

1) Click on the user icon located at the right top corner and select my Settings Option.

2) Expand the Personal menu, and select the Reset my Security Token.

3) This security token will be sent to your email address.

Step 4: 

By using your own salesforce credential, we can establish the connection between the .NET Console Application and Salesforce.

Step 5: 

Below is the code/logic that covers all the functionalities explained in this article.

class Program 

    { 

        private static string _clientID="3MVG9ZL0ppGP5UrCWG8fRnEL5LOxZ2cd0L6OzGT41JUCt5J4XcALvU_21XO"; 

        private static string _clientSecretctID = "8702660451118025048"; 

        private static string _username = "demo@demo.com"; 

        private static string _securityToken = "U4RFCORGcwBU5hP7AeKazL9x1"; 

        private static string _password = "Salesforce123"+_securityToken; 

        static void Main(string[] args) 

        { 

            var temp=""; 

            Guid newId = Guid.NewGuid(); 

            var auth = new Salesforce.Common.AuthenticationClient(); 

            auth.UsernamePasswordAsync(_clientID, _clientSecretctID, _username, _password).Wait(); 



            var client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion); 

            while (true) 

            { 

                Console.WriteLine("\n\t========== Menu ===========\n"); 

                Console.WriteLine("         Press 1 for View Lead       "); 

                Console.WriteLine("         Press 2 for Add New Lead"); 

                Console.WriteLine("         Press 3 for Update Lead"); 

                Console.WriteLine("         Press 4 for Delete Lead"); 

                Console.WriteLine("         Press 5 for Exit"); 

                Console.WriteLine("\n\t============================"); 



                Console.Write("\nEnter your option:\t"); 

                int opt = Convert.ToInt32(Console.ReadLine()); 



                switch (opt) 

                { 

                    case 1: 

                        Task<QueryResult<dynamic>> result = client.QueryAsync<dynamic> 

("SELECT Id,Name,Company,Status FROM Lead where status !='Closed - Converted'"); 

                        result.Wait(); 



                        var contacts = result.Result.Records; 

                        Console.ResetColor(); 

                        Console.BackgroundColor = ConsoleColor.Blue; 

                        Console.ForegroundColor = ConsoleColor.White; 

                        Console.WriteLine("{0}{1}{2}", "Name".PadRight(30), "Company".PadRight(40), "Status".PadRight(40)); 



                        Console.BackgroundColor = ConsoleColor.White; 

                        Console.ForegroundColor = ConsoleColor.Blue; 



                        foreach (var item in contacts) 

                        { 

                            Console.WriteLine("{0}{1}{2}", ((string)(item.Name)).PadRight(30), ((string)(item.Company)).PadRight(40), ((string)(item.Status)).PadRight(40)); 

                        } 

                        Console.ResetColor(); 

                        break; 

                    case 2: 

                        Console.Write("\nPlease Enter your Name:\t"); 

                        string name = Console.ReadLine(); 

                        Console.Write("\nPlease Enter your Company:\t"); 

                        string company = Console.ReadLine(); 



                        var newcontect = new { LastName = name, Company = company, Status = "Working - Contacted", ExternalLeadID__c = newId}; 

                        var createTaskResult = client.CreateAsync("Lead", newcontect); 

                        var clientid = createTaskResult.Result; 

                        temp = clientid.Id; 

                        if (temp!=null) 

                        { 

                            Console.WriteLine( "\nSuccessfully  Created Lead : "+ newcontect.LastName); 

                        } 



                        break; 

                    case 3: 

                        var contactResult = client.QueryAsync<dynamic> 

(String.Format("select LastName,Company,Status from Lead where id ='{0}'", temp.ToString())); 

                        var foundconduct = contactResult.Result.Records.FirstOrDefault(); 

                        Console.Write("\nPlease Enter your Name:\t"); 

                        string changedName = Console.ReadLine(); 

                        foundconduct.LastName = changedName; 

                        var updateSucess = client.UpdateAsync("Lead", temp.ToString(), foundconduct); 

                        updateSucess.Wait(); 

                        if (temp != null) 

                        { 

                            Console.WriteLine("\nSuccessfully  Update Lead: " + foundconduct.LastName); 

                        } 

                        break; 

                    case 4: 

                        var deleteSuccess = client.DeleteAsync("Lead", temp.ToString()); 

                        deleteSuccess.Wait(); 

                        if (deleteSuccess.Result) 

                        { 

                            Console.WriteLine("\nSuccessfully  Deleted Lead"); 

                        } 

                        else 

                        { 

                            Console.WriteLine("Fail"); 

                        } 

                        break; 

                    case 5: 

                        Environment.Exit(0); 

                        break; 

                    default: 

                        Console.WriteLine("Sorry, Invalid selection"); 

                        break; 

                } 

            } 

        } 

    } 

}

 

Lead Data Management through .NET Console Application

1) Run the .Net Console Application

2) Provide the option based on the menu

How to view lead record available in Salesforce?

Image

Enter 1 to view the Salesforce Lead records

Image

Enter 2 to create a new lead in Salesforce.

Image

After the Lead record is created through the .NET Console Application, it will automatically create the same record in Salesforce. The text highlighted in red shows the newly created lead from .NET Console Application.

Image

Enter 3 to update a Lead record –modify the lead name.

After updating the lead record, you can view the updated lead record by using option 1.

Image

The updated lead record using .NET Console Application will be automatically reflected in Salesforce.

Image

Enter 4 to delete an existing Lead record in Salesforce.