Salesforce DML Operation Using DeveloperForce Toolkit for .Net

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.

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.

salesforce dml developer force toolkit

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.

salesforce dml developer force toolkit

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=
"3MVG9ZL0ppGP5UrCWG8fRnEL5LOxZ2cd0L6O
zGT41JUCt5J4XcALvU_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?

salesforce dml developer force toolkit

Enter 1 to view the Salesforce Lead records

salesforce dml developer force toolkit

Enter 2 to create a new lead in Salesforce.

salesforce dml developer force toolkit

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.

salesforce dml developer force toolkit

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.

salesforce dml developer force toolkit

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

salesforce dml developer force toolkit

Enter 4 to delete an existing Lead record in Salesforce.

Image

Conclusion:

With the help of REST API, we can perform DML operations, so that data cans sync. between .NET Console Application and Salesforce, and vice versa. This functionality will support all the Standard and Custom objects in Salesforce.com.

Reference Link:

https://developer.salesforce.com/page/

Force.com_for_ASP.NET_Developers

https://developer.salesforce.com/page/

Integrating_Force.com_with_Microsoft_.NET

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.