public async Task Delete_Account_IsSuccess() { var account = new Account { Name = "New Account", Description = "New Account Description" }; var successResponse = await _client.CreateAsync("Account", account); var success = await _client.DeleteAsync("Account", successResponse.Id); Assert.IsTrue(success); }
public async void Delete_Account_IsSuccess() { var account = new Account { Name = "New Account", Description = "New Account Description" }; var id = await _client.CreateAsync("Account", account); var success = await _client.DeleteAsync("Account", id); Assert.IsTrue(success); }
private bool HandleExpiredRequest(ForceClient forceClient, FileRequest fileRequest) { // Delete expired file requests bool isExpired = false; if (!string.IsNullOrEmpty(fileRequest.FileUrl__c)) { System.Uri url = new System.Uri(fileRequest.FileUrl__c); string queryString = System.Net.WebUtility.UrlDecode(url.Query); string[] queryParts = queryString.Split('&'); foreach (string queryPart in queryParts) { if (queryPart.StartsWith("se=")) { System.DateTime endDateTime = System.DateTime.Parse(queryPart.Replace("se=", "")); if (endDateTime < System.DateTime.Now) { // Delete SalesForce record isExpired = true; bool isSuccess = forceClient.DeleteAsync("FileRequest__c", fileRequest.Id).Result; } break; } } } return(isExpired); }
/// <summary> /// Deleting provided opportunity /// </summary> /// <param name="client">Force client instance</param> /// <param name="opportunityId">Opportunity Id</param> /// <returns></returns> public async Task <SalesForceResponse> DeleteOpportunity(ForceClient client, string opportunityId) { var res = await client.DeleteAsync(Constants.Opportunity, opportunityId); return(new SalesForceResponse { IsSuccess = res, Details = res ? Constants.MsgOpportunityDeleteSuccess : Constants.MsgOpportunityDeleteFailed, }); }
public async Task <SalesForceModels.SalesForceResponseModel> DeleteLead(ForceClient client, string leadId) { var res = await client.DeleteAsync("Lead", leadId); return(new SalesForceModels.SalesForceResponseModel { IsSuccess = res, Details = res ? "Lead succesfully deleted." : "Problem while deleting lead from SFDC.Please refresh the page and try again.", }); }
public async Task <SalesForceModels.SalesForceResponseModel> DeleteOpportunity(ForceClient client, string opportunityId) { var res = await client.DeleteAsync("Opportunity", opportunityId); return(new SalesForceModels.SalesForceResponseModel { IsSuccess = res, Details = res ? "Opportunity succesfully deleted." : "Problem while deleting opportunity from SFDC.Please refresh the page and try again.", }); }
public async Task <ActionResult> LeadDelete(string id) { // TODO: Resolve Edit failures with ID // Ensure the user is authenticated if (!SalesforceService.IsUserLoggedIn()) { // Should include a state argument to the URI so that when the dance is done, we can redirect to the page that we were requesting string myUri = SalesforceOAuthRedirectHandler.AuthorizationUri.ToString() + "&state=" + this.Url.RequestContext.HttpContext.Request.RawUrl; return(this.Redirect(myUri)); } // Initialize the Force client SalesforceService service = new SalesforceService(); ForceClient client = service.GetForceClient(); var success = await client.DeleteAsync("Lead", id); return(RedirectToAction("LeadList")); }
private static async Task RunSample() { var auth = new AuthenticationClient(); // Authenticate with Salesforce Console.WriteLine("Authenticating with Salesforce"); var url = IsSandboxUser.Equals("true", StringComparison.CurrentCultureIgnoreCase) ? "https://test.salesforce.com/services/oauth2/token" : "https://login.salesforce.com/services/oauth2/token"; await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password, url); Console.WriteLine("Connected to Salesforce"); var client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion); string sql = "SELECT Id, CaseNumber, Current_Version__c, Priority, Go_Live_Critical__c, Case.Account.name, " + "Case.Owner.name, Origin, Patch_Number__c, Subject, OwnerId, Type, Description, CreatedDate, " + "Case.createdBy.name, status, bzid__c, product__c, Customer__r.name, " + "( SELECT CommentBody, CaseComment.createdBy.name, CaseComment.lastModifiedBy.name, CreatedDate, LastModifiedDate FROM CaseComments ORDER BY CreatedDate DESC NULLS LAST LIMIT 1 ) " + "FROM Case WHERE Status ='Eng New' AND Case.product__c='Accela ACA' AND Case.Owner.name='Engineering'"; //const string qry = "SELECT ID, Name FROM Account"; var accts = new List<Account>(); var results = await client.QueryAsync<Account>(sql); var totalSize = results.TotalSize; Console.WriteLine("Queried " + totalSize + " records."); accts.AddRange(results.Records); var nextRecordsUrl = results.NextRecordsUrl; if (!string.IsNullOrEmpty(nextRecordsUrl)) { Console.WriteLine("Found nextRecordsUrl."); while (true) { var continuationResults = await client.QueryContinuationAsync<Account>(nextRecordsUrl); totalSize = continuationResults.TotalSize; Console.WriteLine("Queried an additional " + totalSize + " records."); accts.AddRange(continuationResults.Records); if (string.IsNullOrEmpty(continuationResults.NextRecordsUrl)) break; //pass nextRecordsUrl back to client.QueryAsync to request next set of records nextRecordsUrl = continuationResults.NextRecordsUrl; } } Console.WriteLine("Retrieved accounts = " + accts.Count() + ", expected size = " + totalSize); // Create a sample record Console.WriteLine("Creating test record."); var account = new Account { Name = "Test Account" }; account.Id = await client.CreateAsync(Account.SObjectTypeName, account); if (account.Id == null) { Console.WriteLine("Failed to create test record."); return; } Console.WriteLine("Successfully created test record."); // Update the sample record // Shows that annonymous types can be used as well Console.WriteLine("Updating test record."); var success = await client.UpdateAsync(Account.SObjectTypeName, account.Id, new { Name = "Test Update" }); if (!string.IsNullOrEmpty(success.Errors.ToString())) { Console.WriteLine("Failed to update test record!"); return; } Console.WriteLine("Successfully updated the record."); // Retrieve the sample record // How to retrieve a single record if the id is known Console.WriteLine("Retrieving the record by ID."); account = await client.QueryByIdAsync<Account>(Account.SObjectTypeName, account.Id); if (account == null) { Console.WriteLine("Failed to retrieve the record by ID!"); return; } Console.WriteLine("Retrieved the record by ID."); // Query for record by name Console.WriteLine("Querying the record by name."); var accounts = await client.QueryAsync<Account>("SELECT ID, Name FROM Account WHERE Name = '" + account.Name + "'"); account = accounts.Records.FirstOrDefault(); if (account == null) { Console.WriteLine("Failed to retrieve account by query!"); return; } Console.WriteLine("Retrieved the record by name."); // Delete account Console.WriteLine("Deleting the record by ID."); var deleted = await client.DeleteAsync(Account.SObjectTypeName, account.Id); if (!deleted) { Console.WriteLine("Failed to delete the record by ID!"); return; } Console.WriteLine("Deleted the record by ID."); // Selecting multiple accounts into a dynamic Console.WriteLine("Querying multiple records."); var dynamicAccounts = await client.QueryAsync<dynamic>("SELECT ID, Name FROM Account LIMIT 10"); foreach (dynamic acct in dynamicAccounts.Records) { Console.WriteLine("Account - " + acct.Name); } // Creating parent - child records using a Dynamic Console.WriteLine("Creating a parent record (Account)"); dynamic a = new ExpandoObject(); a.Name = "Account from .Net Toolkit"; a.Id = await client.CreateAsync("Account", a); if (a.Id == null) { Console.WriteLine("Failed to create parent record."); return; } Console.WriteLine("Creating a child record (Contact)"); dynamic c = new ExpandoObject(); c.FirstName = "Joe"; c.LastName = "Blow"; c.AccountId = a.Id; c.Id = await client.CreateAsync("Contact", c); if (c.Id == null) { Console.WriteLine("Failed to create child record."); return; } Console.WriteLine("Deleting parent and child"); // Delete account (also deletes contact) Console.WriteLine("Deleting the Account by Id."); deleted = await client.DeleteAsync(Account.SObjectTypeName, a.Id); if (!deleted) { Console.WriteLine("Failed to delete the record by ID!"); return; } Console.WriteLine("Deleted the Account and Contact."); }
public async Task <bool> Delete(string id, string sfObject) { var succesResponse = await _client.DeleteAsync(sfObject, id); return(succesResponse); }
private static async Task RunSample() { var auth = new AuthenticationClient(); // Authenticate with Salesforce Console.WriteLine("Authenticating with Salesforce"); await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password); Console.WriteLine("Connected to Salesforce"); var client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion); // Create a sample record Console.WriteLine("Creating test record."); var account = new Account { Name = "Test Account" }; account.Id = await client.CreateAsync(Account.SObjectTypeName, account); if (account.Id == null) { Console.WriteLine("Failed to create test record."); return; } Console.WriteLine("Successfully created test record."); // Update the sample record // Shows that annonymous types can be used as well Console.WriteLine("Updating test record."); var success = await client.UpdateAsync(Account.SObjectTypeName, account.Id, new { Name = "Test Update" }); if (!success) { Console.WriteLine("Failed to update test record!"); return; } Console.WriteLine("Successfully updated the record."); // Retrieve the sample record // How to retrieve a single record if the id is known Console.WriteLine("Retrieving the record by ID."); account = await client.QueryByIdAsync<Account>(Account.SObjectTypeName, account.Id); if (account == null) { Console.WriteLine("Failed to retrieve the record by ID!"); return; } Console.WriteLine("Retrieved the record by ID."); // Query for record by name Console.WriteLine("Querying the record by name."); var accounts = await client.QueryAsync<Account>("SELECT ID, Name FROM Account WHERE Name = '" + account.Name + "'"); account = accounts.records.FirstOrDefault(); if (account == null) { Console.WriteLine("Failed to retrieve account by query!"); return; } Console.WriteLine("Retrieved the record by name."); // Delete account Console.WriteLine("Deleting the record by ID."); success = await client.DeleteAsync(Account.SObjectTypeName, account.Id); if (!success) { Console.WriteLine("Failed to delete the record by ID!"); return; } Console.WriteLine("Deleted the record by ID."); // Selecting multiple accounts into a dynamic Console.WriteLine("Querying multiple records."); var dynamicAccounts = await client.QueryAsync<dynamic>("SELECT ID, Name FROM Account LIMIT 10"); foreach (dynamic acct in dynamicAccounts.records) { Console.WriteLine("Account - " + acct.Name); } // Creating parent - child records using a Dynamic Console.WriteLine("Creating a parent record (Account)"); dynamic a = new ExpandoObject(); a.Name = "Account from .Net Toolkit"; a.Id = await client.CreateAsync("Account", a); if (a.Id == null) { Console.WriteLine("Failed to create parent record."); return; } Console.WriteLine("Creating a child record (Contact)"); dynamic c = new ExpandoObject(); c.FirstName = "Joe"; c.LastName = "Blow"; c.AccountId = a.Id; c.Id = await client.CreateAsync("Contact", c); if (c.Id == null) { Console.WriteLine("Failed to create child record."); return; } Console.WriteLine("Press Enter to delete parent and child and continue"); Console.Read(); // Delete account (also deletes contact) Console.WriteLine("Deleting the Account by Id."); success = await client.DeleteAsync(Account.SObjectTypeName, a.Id); if (!success) { Console.WriteLine("Failed to delete the record by ID!"); return; } Console.WriteLine("Deleted the Account and Contact."); }
public async Task <bool> DeleteObject(string id, string objectName) { bool response = await _client.DeleteAsync(objectName, id); return(response); }
private static async Task RunSample() { try { var auth = new AuthenticationClient(); // Authenticate with Salesforce Console.WriteLine("Authenticating with Salesforce..."); var url = IsSandboxUser.Equals("true", StringComparison.CurrentCultureIgnoreCase) ? "https://login.salesforce.com/services/oauth2/token " : "https://test.salesforce.com/services/oauth2/token"; await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password, url); Console.WriteLine("Connected to Salesforce."); var client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion); // Retrieve all accounts Console.WriteLine("Get Accounts"); // Initialization PhoneBook phoneBook = new PhoneBook(); // Create Query var addPhoneData = new PhoneBook { Email__c = "*****@*****.**", FirstName__c = "John", LastName__c = "Doe", Phone__c = 9876543210 }; var result = await client.CreateAsync(PhoneBook.SObjectTypeName, addPhoneData); if (result.Id == null) { Console.WriteLine("Cannot insert data to Phonebook!"); return; } Console.WriteLine("Successfully added the record."); // Update Query var success = await client.UpdateAsync(PhoneBook.SObjectTypeName, result.Id, new { FirstName__c = "Tim" }); if (!string.IsNullOrEmpty(success.Errors.ToString())) { Console.WriteLine("Failed to update test record!"); return; } Console.WriteLine("Successfully updated the record."); // Delete Query Console.WriteLine("Deleting the record by ID."); var deleted = await client.DeleteAsync(PhoneBook.SObjectTypeName, result.Id); if (!deleted) { Console.WriteLine("Failed to delete the record by ID!"); return; } Console.WriteLine("Successfully deleted the record."); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } }
private static async Task RunSample() { var auth = new AuthenticationClient(); // Authenticate with Salesforce Console.WriteLine("Authenticating with Salesforce"); var url = IsSandboxUser.Equals("true", StringComparison.CurrentCultureIgnoreCase) ? "https://test.salesforce.com/services/oauth2/token" : "https://login.salesforce.com/services/oauth2/token"; await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password, url); Console.WriteLine("Connected to Salesforce"); var client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion); // retrieve all accounts Console.WriteLine("Get Accounts"); const string qry = "SELECT ID, Name FROM Account"; var accts = new List<Account>(); var results = await client.QueryAsync<Account>(qry); var totalSize = results.TotalSize; Console.WriteLine("Queried " + totalSize + " records."); accts.AddRange(results.Records); var nextRecordsUrl = results.NextRecordsUrl; if (!string.IsNullOrEmpty(nextRecordsUrl)) { Console.WriteLine("Found nextRecordsUrl."); while (true) { var continuationResults = await client.QueryContinuationAsync<Account>(nextRecordsUrl); totalSize = continuationResults.TotalSize; Console.WriteLine("Queried an additional " + totalSize + " records."); accts.AddRange(continuationResults.Records); if (string.IsNullOrEmpty(continuationResults.NextRecordsUrl)) break; //pass nextRecordsUrl back to client.QueryAsync to request next set of records nextRecordsUrl = continuationResults.NextRecordsUrl; } } Console.WriteLine("Retrieved accounts = " + accts.Count() + ", expected size = " + totalSize); // Create a sample record Console.WriteLine("Creating test record."); var account = new Account { Name = "Test Account" }; var response = await client.CreateAsync(Account.SObjectTypeName, account); if (!string.Equals(response.Success, "true", StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("Failed to create test record."); return; } Console.WriteLine("Successfully created test record."); // Update the sample record // Shows that annonymous types can be used as well Console.WriteLine("Updating test record."); var success = await client.UpdateAsync(Account.SObjectTypeName, account.Id, new { Name = "Test Update" }); if (!string.IsNullOrEmpty(success.Errors.ToString())) { Console.WriteLine("Failed to update test record!"); return; } Console.WriteLine("Successfully updated the record."); // Retrieve the sample record // How to retrieve a single record if the id is known Console.WriteLine("Retrieving the record by ID."); account = await client.QueryByIdAsync<Account>(Account.SObjectTypeName, account.Id); if (account == null) { Console.WriteLine("Failed to retrieve the record by ID!"); return; } Console.WriteLine("Retrieved the record by ID."); // Query for record by name Console.WriteLine("Querying the record by name."); var accounts = await client.QueryAsync<Account>("SELECT ID, Name FROM Account WHERE Name = '" + account.Name + "'"); account = accounts.Records.FirstOrDefault(); if (account == null) { Console.WriteLine("Failed to retrieve account by query!"); return; } Console.WriteLine("Retrieved the record by name."); // Delete account Console.WriteLine("Deleting the record by ID."); var deleted = await client.DeleteAsync(Account.SObjectTypeName, account.Id); if (!deleted) { Console.WriteLine("Failed to delete the record by ID!"); return; } Console.WriteLine("Deleted the record by ID."); // Selecting multiple accounts into a dynamic Console.WriteLine("Querying multiple records."); var dynamicAccounts = await client.QueryAsync<dynamic>("SELECT ID, Name FROM Account LIMIT 10"); foreach (dynamic acct in dynamicAccounts.Records) { Console.WriteLine("Account - " + acct.Name); } // Creating parent - child records using a Dynamic Console.WriteLine("Creating a parent record (Account)"); dynamic a = new ExpandoObject(); a.Name = "Account from .Net Toolkit"; a.Id = await client.CreateAsync("Account", a); if (a.Id == null) { Console.WriteLine("Failed to create parent record."); return; } Console.WriteLine("Creating a child record (Contact)"); dynamic c = new ExpandoObject(); c.FirstName = "Joe"; c.LastName = "Blow"; c.AccountId = a.Id; c.Id = await client.CreateAsync("Contact", c); if (c.Id == null) { Console.WriteLine("Failed to create child record."); return; } Console.WriteLine("Deleting parent and child"); // Delete account (also deletes contact) Console.WriteLine("Deleting the Account by Id."); deleted = await client.DeleteAsync(Account.SObjectTypeName, a.Id); if (!deleted) { Console.WriteLine("Failed to delete the record by ID!"); return; } Console.WriteLine("Deleted the Account and Contact."); }
private static async Task RunSample() { var auth = new AuthenticationClient(); // Authenticate with Salesforce Console.WriteLine("Authenticating with Salesforce"); await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password); Console.WriteLine("Connected to Salesforce"); var client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion); // Create a sample record Console.WriteLine("Creating test record."); var account = new Account { Name = "Test Account" }; account.Id = await client.CreateAsync(Account.SObjectTypeName, account); if (account.Id == null) { Console.WriteLine("Failed to create test record."); return; } Console.WriteLine("Successfully created test record."); // Update the sample record // Shows that annonymous types can be used as well Console.WriteLine("Updating test record."); var success = await client.UpdateAsync(Account.SObjectTypeName, account.Id, new { Name = "Test Update" }); if (!success) { Console.WriteLine("Failed to update test record!"); return; } Console.WriteLine("Successfully updated the record."); // Retrieve the sample record // How to retrieve a single record if the id is known Console.WriteLine("Retrieving the record by ID."); account = await client.QueryByIdAsync<Account>(Account.SObjectTypeName, account.Id); if (account == null) { Console.WriteLine("Failed to retrieve the record by ID!"); return; } Console.WriteLine("Retrieved the record by ID."); // Query for record by name Console.WriteLine("Querying the record by name."); var accounts = await client.QueryAsync<Account>("SELECT ID, Name FROM Account WHERE Name = '" + account.Name + "'"); account = accounts.FirstOrDefault(); if (account == null) { Console.WriteLine("Failed to retrieve account by query!"); return; } Console.WriteLine("Retrieved the record by name."); // Delete account Console.WriteLine("Deleting the record by ID."); success = await client.DeleteAsync(Account.SObjectTypeName, account.Id); if (!success) { Console.WriteLine("Failed to delete the record by ID!"); return; } Console.WriteLine("Deleted the record by ID."); }
/// <summary> /// Deletes attachment from Salesforce once the attachment has been added to Sharepoint and Image link put on Contact record /// </summary> private static async Task DeleteSalesforceAttachmentAsync(ForceClient client, string attachmentId) { await client.DeleteAsync("Attachment", attachmentId); }
private static async Task RunSample() { var auth = new AuthenticationClient(); // Authenticate with Salesforce Console.WriteLine("Authenticating with Salesforce"); var url = IsSandboxUser.Equals("true", StringComparison.CurrentCultureIgnoreCase) ? "https://test.salesforce.com/services/oauth2/token" : "https://login.salesforce.com/services/oauth2/token"; await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password, url); Console.WriteLine("Connected to Salesforce"); var client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion); // retrieve all accounts Console.WriteLine("Get Accounts"); const string qry = "SELECT ID, Name FROM Account"; var accts = new List <Account>(); var results = await client.QueryAsync <Account>(qry); var totalSize = results.TotalSize; Console.WriteLine("Queried " + totalSize + " records."); accts.AddRange(results.Records); var nextRecordsUrl = results.NextRecordsUrl; if (!string.IsNullOrEmpty(nextRecordsUrl)) { Console.WriteLine("Found nextRecordsUrl."); while (true) { var continuationResults = await client.QueryContinuationAsync <Account>(nextRecordsUrl); totalSize = continuationResults.TotalSize; Console.WriteLine("Queried an additional " + totalSize + " records."); accts.AddRange(continuationResults.Records); if (string.IsNullOrEmpty(continuationResults.NextRecordsUrl)) { break; } //pass nextRecordsUrl back to client.QueryAsync to request next set of records nextRecordsUrl = continuationResults.NextRecordsUrl; } } Console.WriteLine("Retrieved accounts = " + accts.Count() + ", expected size = " + totalSize); // Create a sample record Console.WriteLine("Creating test record."); var account = new Account { Name = "Test Account" }; var createAccountResponse = await client.CreateAsync(Account.SObjectTypeName, account); account.Id = createAccountResponse.Id; if (account.Id == null) { Console.WriteLine("Failed to create test record."); return; } Console.WriteLine("Successfully created test record."); // Update the sample record // Shows that annonymous types can be used as well Console.WriteLine("Updating test record."); var success = await client.UpdateAsync(Account.SObjectTypeName, account.Id, new { Name = "Test Update" }); if (!string.IsNullOrEmpty(success.Errors.ToString())) { Console.WriteLine("Failed to update test record!"); return; } Console.WriteLine("Successfully updated the record."); // Retrieve the sample record // How to retrieve a single record if the id is known Console.WriteLine("Retrieving the record by ID."); account = await client.QueryByIdAsync <Account>(Account.SObjectTypeName, account.Id); if (account == null) { Console.WriteLine("Failed to retrieve the record by ID!"); return; } Console.WriteLine("Retrieved the record by ID."); // Query for record by name Console.WriteLine("Querying the record by name."); var accounts = await client.QueryAsync <Account>("SELECT ID, Name FROM Account WHERE Name = '" + account.Name + "'"); account = accounts.Records.FirstOrDefault(); if (account == null) { Console.WriteLine("Failed to retrieve account by query!"); return; } Console.WriteLine("Retrieved the record by name."); // Delete account Console.WriteLine("Deleting the record by ID."); var deleted = await client.DeleteAsync(Account.SObjectTypeName, account.Id); if (!deleted) { Console.WriteLine("Failed to delete the record by ID!"); return; } Console.WriteLine("Deleted the record by ID."); // Selecting multiple accounts into a dynamic Console.WriteLine("Querying multiple records."); var dynamicAccounts = await client.QueryAsync <dynamic>("SELECT ID, Name FROM Account LIMIT 10"); foreach (dynamic acct in dynamicAccounts.Records) { Console.WriteLine("Account - " + acct.Name); } // Creating parent - child records using a Dynamic Console.WriteLine("Creating a parent record (Account)"); dynamic a = new ExpandoObject(); a.Name = "Account from .Net Toolkit"; var createParentAccountResponse = await client.CreateAsync("Account", a); a.Id = createParentAccountResponse.Id; if (a.Id == null) { Console.WriteLine("Failed to create parent record."); return; } Console.WriteLine("Creating a child record (Contact)"); dynamic c = new ExpandoObject(); c.FirstName = "Joe"; c.LastName = "Blow"; c.AccountId = a.Id; var createContactResponse = await client.CreateAsync("Contact", c); c.Id = createContactResponse.Id; if (c.Id == null) { Console.WriteLine("Failed to create child record."); return; } Console.WriteLine("Deleting parent and child"); // Delete account (also deletes contact) Console.WriteLine("Deleting the Account by Id."); deleted = await client.DeleteAsync(Account.SObjectTypeName, a.Id); if (!deleted) { Console.WriteLine("Failed to delete the record by ID!"); return; } Console.WriteLine("Deleted the Account and Contact."); }
private static async Task RunSample() { var auth = new AuthenticationClient(); // Authenticate with Salesforce Console.WriteLine("Authenticating with Salesforce"); await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password); Console.WriteLine("Connected to Salesforce"); var client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion); // Create a sample record Console.WriteLine("Creating test record."); var account = new Account { Name = "Test Account" }; account.Id = await client.CreateAsync(Account.SObjectTypeName, account); if (account.Id == null) { Console.WriteLine("Failed to create test record."); return; } Console.WriteLine("Successfully created test record."); // Update the sample record // Shows that annonymous types can be used as well Console.WriteLine("Updating test record."); var success = await client.UpdateAsync(Account.SObjectTypeName, account.Id, new { Name = "Test Update" }); if (!success) { Console.WriteLine("Failed to update test record!"); return; } Console.WriteLine("Successfully updated the record."); // Retrieve the sample record // How to retrieve a single record if the id is known Console.WriteLine("Retrieving the record by ID."); account = await client.QueryByIdAsync <Account>(Account.SObjectTypeName, account.Id); if (account == null) { Console.WriteLine("Failed to retrieve the record by ID!"); return; } Console.WriteLine("Retrieved the record by ID."); // Query for record by name Console.WriteLine("Querying the record by name."); var accounts = await client.QueryAsync <Account>("SELECT ID, Name FROM Account WHERE Name = '" + account.Name + "'"); account = accounts.records.FirstOrDefault(); if (account == null) { Console.WriteLine("Failed to retrieve account by query!"); return; } Console.WriteLine("Retrieved the record by name."); // Delete account Console.WriteLine("Deleting the record by ID."); success = await client.DeleteAsync(Account.SObjectTypeName, account.Id); if (!success) { Console.WriteLine("Failed to delete the record by ID!"); return; } Console.WriteLine("Deleted the record by ID."); // Selecting multiple accounts into a dynamic Console.WriteLine("Querying multiple records."); var dynamicAccounts = await client.QueryAsync <dynamic>("SELECT ID, Name FROM Account LIMIT 10"); foreach (dynamic acct in dynamicAccounts.records) { Console.WriteLine("Account - " + acct.Name); } // Creating parent - child records using a Dynamic Console.WriteLine("Creating a parent record (Account)"); dynamic a = new ExpandoObject(); a.Name = "Account from .Net Toolkit"; a.Id = await client.CreateAsync("Account", a); if (a.Id == null) { Console.WriteLine("Failed to create parent record."); return; } Console.WriteLine("Creating a child record (Contact)"); dynamic c = new ExpandoObject(); c.FirstName = "Joe"; c.LastName = "Blow"; c.AccountId = a.Id; c.Id = await client.CreateAsync("Contact", c); if (c.Id == null) { Console.WriteLine("Failed to create child record."); return; } Console.WriteLine("Press Enter to delete parent and child and continue"); Console.Read(); // Delete account (also deletes contact) Console.WriteLine("Deleting the Account by Id."); success = await client.DeleteAsync(Account.SObjectTypeName, a.Id); if (!success) { Console.WriteLine("Failed to delete the record by ID!"); return; } Console.WriteLine("Deleted the Account and Contact."); }