Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web.Services.Protocols;
- using Walkthrough.sforce;
- namespace Walkthrough
- {
- class QuickstartApiSample
- {
- private SforceService binding;
- [STAThread]
- static void Main(string[] args)
- {
- QuickstartApiSample sample = new QuickstartApiSample();
- sample.run();
- }
- public void run()
- {
- if (login())
- {
- describeGlobalSample();
- describeSObjectsSample();
- querySample();
- logout();
- }
- }
- private bool login()
- {
- Console.Write("Enter username: ");
- string username = Console.ReadLine();
- Console.Write("Enter password: ");
- string password = Console.ReadLine();
- binding = new SforceService();
- LoginResult lr;
- try
- {
- Console.WriteLine("\nLogging in...\n");
- lr = binding.login(username, password);
- }
- catch (SoapException e)
- {
- Console.WriteLine(e.Code);
- Console.WriteLine("An unexpected error has occurred: " + e.Message);
- Console.WriteLine(e.StackTrace);
- return false;
- }
- if (lr.passwordExpired)
- {
- Console.WriteLine("An error has occurred. Your password has expired.");
- return false;
- }
- String authEndPoint = binding.Url;
- binding.Url = lr.serverUrl;
- binding.SessionHeaderValue = new SessionHeader();
- binding.SessionHeaderValue.sessionId = lr.sessionId;
- printUserInfo(lr, authEndPoint);
- return true;
- }
- private void printUserInfo(LoginResult lr, String authEP)
- {
- try
- {
- GetUserInfoResult userInfo = lr.userInfo;
- Console.WriteLine("\nLogging in ...\n");
- Console.WriteLine("UserID: " + userInfo.userId);
- Console.WriteLine("User Full Name: " +
- userInfo.userFullName);
- Console.WriteLine("User Email: " +
- userInfo.userEmail);
- Console.WriteLine();
- Console.WriteLine("SessionID: " +
- lr.sessionId);
- Console.WriteLine("Auth End Point: " +
- authEP);
- Console.WriteLine("Service End Point: " +
- lr.serverUrl);
- Console.WriteLine();
- }
- catch (SoapException e)
- {
- Console.WriteLine("An unexpected error has occurred: " + e.Message +
- " Stack trace: " + e.StackTrace);
- }
- }
- private void logout()
- {
- try
- {
- binding.logout();
- Console.WriteLine("Logged out.");
- }
- catch (SoapException e)
- {
- Console.WriteLine(e.Code);
- Console.WriteLine("An unexpected error has occurred: " + e.Message);
- Console.WriteLine(e.StackTrace);
- }
- }
- private void describeGlobalSample()
- {
- try
- {
- DescribeGlobalResult dgr = binding.describeGlobal();
- Console.WriteLine("\nDescribe Global Results:\n");
- for (int i = 0; i < dgr.sobjects.Length; i++)
- {
- Console.WriteLine(dgr.sobjects[i].name);
- }
- }
- catch (SoapException e)
- {
- Console.WriteLine("An exception has occurred: " + e.Message +
- "\nStack trace: " + e.StackTrace);
- }
- }
- private void describeSObjectsSample()
- {
- Console.Write("\nType the name of the object to " +
- "describe (try Account): ");
- string objectType = Console.ReadLine();
- try
- {
- DescribeSObjectResult[] dsrArray = binding.describeSObjects(new string[] { objectType });
- DescribeSObjectResult dsr = dsrArray[0];
- Console.WriteLine("\n\nObject Name: " + dsr.name);
- if (dsr.custom) Console.WriteLine("Custom Object");
- if (dsr.label != null) Console.WriteLine("Label: " + dsr.label);
- if (dsr.createable) Console.WriteLine("Createable");
- if (dsr.deletable) Console.WriteLine("Deleteable");
- if (dsr.queryable) Console.WriteLine("Queryable");
- if (dsr.replicateable) Console.WriteLine("Replicateable");
- if (dsr.retrieveable) Console.WriteLine("Retrieveable");
- if (dsr.searchable) Console.WriteLine("Searchable");
- if (dsr.undeletable) Console.WriteLine("Undeleteable");
- if (dsr.updateable) Console.WriteLine("Updateable");
- Console.WriteLine("Number of fields: " + dsr.fields.Length);
- for (int i = 0; i < dsr.fields.Length; i++)
- {
- Field field = dsr.fields[i];
- Console.WriteLine("Field name: " + field.name);
- Console.WriteLine("\tField Label: " + field.label);
- if (field.nameField)
- Console.WriteLine("\tThis is a name field.");
- if (field.restrictedPicklist)
- Console.WriteLine("This is a RESTRICTED picklist field.");
- Console.WriteLine("\tType is: " + field.type.ToString());
- if (field.length > 0)
- Console.WriteLine("\tLength: " + field.length);
- if (field.scale > 0)
- Console.WriteLine("\tScale: " + field.scale);
- if (field.precision > 0)
- Console.WriteLine("\tPrecision: " + field.precision);
- if (field.digits > 0)
- Console.WriteLine("\tDigits: " + field.digits);
- if (field.custom)
- Console.WriteLine("\tThis is a custom field.");
- if (field.nillable) Console.WriteLine("\tCan be nulled.");
- if (field.createable) Console.WriteLine("\tCreateable");
- if (field.filterable) Console.WriteLine("\tFilterable");
- if (field.updateable) Console.WriteLine("\tUpdateable");
- if (field.type.Equals(fieldType.picklist))
- {
- Console.WriteLine("\tPicklist Values");
- for (int j = 0; j < field.picklistValues.Length; j++)
- Console.WriteLine("\t\t" + field.picklistValues[j].value);
- }
- if (field.type.Equals(fieldType.reference))
- {
- Console.WriteLine("\tCan reference these objects:");
- for (int j = 0; j < field.referenceTo.Length; j++)
- Console.WriteLine("\t\t" + field.referenceTo[j]);
- }
- Console.WriteLine("");
- }
- }
- catch (SoapException e)
- {
- Console.WriteLine("An exception has occurred: " + e.Message +
- "\nStack trace: " + e.StackTrace);
- }
- Console.WriteLine("Press ENTER to continue...");
- Console.ReadLine();
- }
- private void querySample()
- {
- String soqlQuery = "SELECT FirstName, LastName FROM Contact";
- try
- {
- QueryResult qr = binding.query(soqlQuery);
- bool done = false;
- if (qr.size > 0)
- {
- Console.WriteLine("Logged-in user can see "
- + qr.records.Length + " contact records.");
- while (!done)
- {
- Console.WriteLine("");
- sObject[] records = qr.records;
- for (int i = 0; i < records.Length; i++)
- {
- Contact con = (Contact)records[i];
- string fName = con.FirstName;
- string lName = con.LastName;
- if (fName == null)
- Console.WriteLine("Connect " + (i + 1) + ": " + lName);
- else
- Console.WriteLine("Connect " + (i + 1) + ": " + fName
- + " " + lName);
- }
- if (qr.done)
- {
- done = true;
- }
- else
- {
- qr = binding.queryMore(qr.queryLocator);
- }
- }
- }
- else
- {
- Console.WriteLine("No records found.");
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("\nFailed to execute query succesfully," +
- "error message was: \n{0}", ex.Message);
- }
- Console.WriteLine("\nPress ENTER to continue...");
- Console.ReadLine();
- }
- }
- }
Add Comment
Please, Sign In to add comment