Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 KB | None | 0 0
  1. namespace ForceConnect
  2. {
  3. class Program
  4. {
  5. private static readonly string SecurityToken = ConfigurationManager.AppSettings["SecurityToken"];
  6. private static readonly string ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"];
  7. private static readonly string ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"];
  8. private static readonly string Username = ConfigurationManager.AppSettings["Username"];
  9. private static readonly string Password = ConfigurationManager.AppSettings["Password"] + SecurityToken;
  10. private static readonly string IsSandboxUser = ConfigurationManager.AppSettings["IsSandboxUser"];
  11.  
  12. static void Main()
  13. {
  14. try
  15. {
  16. var task = RunSample();
  17. task.Wait();
  18. }
  19. catch (Exception e)
  20. {
  21. Console.WriteLine(e.Message);
  22. Console.WriteLine(e.StackTrace);
  23.  
  24. var innerException = e.InnerException;
  25. while (innerException != null)
  26. {
  27. Console.WriteLine(innerException.Message);
  28. Console.WriteLine(innerException.StackTrace);
  29.  
  30. innerException = innerException.InnerException;
  31. Console.Read();
  32. }
  33. }
  34. }
  35.  
  36. private static async Task RunSample()
  37. {
  38. var auth = new AuthenticationClient();
  39.  
  40. // Authenticate with Salesforce
  41. Console.WriteLine("Authenticating with Salesforce");
  42. var url = IsSandboxUser.Equals("true", StringComparison.CurrentCultureIgnoreCase)
  43. ? "https://test.salesforce.com/services/oauth2/token"
  44. : "https://login.salesforce.com/services/oauth2/token";
  45.  
  46. await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password+SecurityToken,url);
  47. Console.WriteLine("Connected to Salesforce");
  48.  
  49. var client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion);
  50.  
  51. // retrieve all accounts
  52. Console.WriteLine("Get Accounts");
  53.  
  54. const string qry = "SELECT ID, Name FROM Account";
  55. var accts = new List<Account>();
  56. var results = await client.QueryAsync<Account>(qry);
  57. var totalSize = results.TotalSize;
  58.  
  59. Console.WriteLine("Queried " + totalSize + " records.");
  60.  
  61. accts.AddRange(results.Records);
  62. var nextRecordsUrl = results.NextRecordsUrl;
  63.  
  64. if (!string.IsNullOrEmpty(nextRecordsUrl))
  65. {
  66. Console.WriteLine("Found nextRecordsUrl.");
  67.  
  68. while (true)
  69. {
  70. var continuationResults = await client.QueryContinuationAsync<Account>(nextRecordsUrl);
  71. totalSize = continuationResults.TotalSize;
  72. Console.WriteLine("Queried an additional " + totalSize + " records.");
  73.  
  74. accts.AddRange(continuationResults.Records);
  75. if (string.IsNullOrEmpty(continuationResults.NextRecordsUrl)) break;
  76.  
  77. //pass nextRecordsUrl back to client.QueryAsync to request next set of records
  78. nextRecordsUrl = continuationResults.NextRecordsUrl;
  79. }
  80. }
  81. Console.WriteLine("Retrieved accounts = " + accts.Count() + ", expected size = " + totalSize);
  82.  
  83. // Create a sample record
  84. Console.WriteLine("Creating test record.");
  85. var account = new Account { Name = "Test Account" };
  86. //account.Id = await client.CreateAsync(Account.SObjectTypeName, account);
  87. var response = await client.CreateAsync("Account", account);
  88. string Id = response.Id;
  89.  
  90. if (account.Id == null)
  91. {
  92. Console.WriteLine("Failed to create test record.");
  93. return;
  94. }
  95.  
  96. Console.WriteLine("Successfully created test record.");
  97.  
  98. // Update the sample record
  99. // Shows that annonymous types can be used as well
  100. Console.WriteLine("Updating test record.");
  101. var success = await client.UpdateAsync(Account.SObjectTypeName, account.Id, new { Name = "Test Update" });
  102. if (!string.IsNullOrEmpty(success.Errors.ToString()))
  103. {
  104. Console.WriteLine("Failed to update test record!");
  105. return;
  106. }
  107.  
  108. Console.WriteLine("Successfully updated the record.");
  109.  
  110. // Retrieve the sample record
  111. // How to retrieve a single record if the id is known
  112. Console.WriteLine("Retrieving the record by ID.");
  113. account = await client.QueryByIdAsync<Account>(Account.SObjectTypeName, account.Id);
  114. if (account == null)
  115. {
  116. Console.WriteLine("Failed to retrieve the record by ID!");
  117. return;
  118. }
  119.  
  120. Console.WriteLine("Retrieved the record by ID.");
  121.  
  122. // Query for record by name
  123. Console.WriteLine("Querying the record by name.");
  124. var accounts = await client.QueryAsync<Account>("SELECT ID, Name FROM Account WHERE Name = '" + account.Name + "'");
  125. account = accounts.Records.FirstOrDefault();
  126. if (account == null)
  127. {
  128. Console.WriteLine("Failed to retrieve account by query!");
  129. return;
  130. }
  131.  
  132. Console.WriteLine("Retrieved the record by name.");
  133.  
  134. // Delete account
  135. Console.WriteLine("Deleting the record by ID.");
  136. var deleted = await client.DeleteAsync(Account.SObjectTypeName, account.Id);
  137. if (!deleted)
  138. {
  139. Console.WriteLine("Failed to delete the record by ID!");
  140. return;
  141. }
  142. Console.WriteLine("Deleted the record by ID.");
  143.  
  144. // Selecting multiple accounts into a dynamic
  145. Console.WriteLine("Querying multiple records.");
  146. var dynamicAccounts = await client.QueryAsync<dynamic>("SELECT ID, Name FROM Account LIMIT 10");
  147. foreach (dynamic acct in dynamicAccounts.Records)
  148. {
  149. Console.WriteLine("Account - " + acct.Name);
  150. }
  151.  
  152. // Creating parent - child records using a Dynamic
  153. Console.WriteLine("Creating a parent record (Account)");
  154. dynamic a = new ExpandoObject();
  155. a.Name = "Account from .Net Toolkit";
  156. a.Id = await client.CreateAsync("Account", a);
  157. if (a.Id == null)
  158. {
  159. Console.WriteLine("Failed to create parent record.");
  160. return;
  161. }
  162.  
  163. Console.WriteLine("Creating a child record (Contact)");
  164. dynamic c = new ExpandoObject();
  165. c.FirstName = "Joe";
  166. c.LastName = "Blow";
  167. c.AccountId = a.Id;
  168. c.Id = await client.CreateAsync("Contact", c);
  169. if (c.Id == null)
  170. {
  171. Console.WriteLine("Failed to create child record.");
  172. return;
  173. }
  174.  
  175. Console.WriteLine("Deleting parent and child");
  176.  
  177. // Delete account (also deletes contact)
  178. Console.WriteLine("Deleting the Account by Id.");
  179. deleted = await client.DeleteAsync(Account.SObjectTypeName, a.Id);
  180. if (!deleted)
  181. {
  182. Console.WriteLine("Failed to delete the record by ID!");
  183. return;
  184. }
  185. Console.WriteLine("Deleted the Account and Contact.");
  186.  
  187. }
  188.  
  189. private class Account
  190. {
  191. public const String SObjectTypeName = "Account";
  192.  
  193. public String Id { get; set; }
  194. public String Name { get; set; }
  195. }
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement