Guest User

Untitled

a guest
Dec 6th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Services.Protocols;
  6. using Walkthrough.sforce;
  7.  
  8. namespace Walkthrough
  9. {
  10.  
  11.    class QuickstartApiSample
  12.    {
  13.       private SforceService binding;
  14.  
  15.       [STAThread]
  16.       static void Main(string[] args)
  17.       {
  18.          QuickstartApiSample sample = new QuickstartApiSample();
  19.          sample.run();
  20.       }
  21.  
  22.       public void run()
  23.       {
  24.    
  25.          if (login())
  26.          {
  27.    
  28.             describeGlobalSample();
  29.  
  30.    
  31.             describeSObjectsSample();
  32.  
  33.    
  34.             querySample();
  35.  
  36.    
  37.             logout();
  38.          }
  39.       }
  40.  
  41.       private bool login()
  42.       {
  43.          Console.Write("Enter username: ");
  44.          string username = Console.ReadLine();
  45.          Console.Write("Enter password: ");
  46.          string password = Console.ReadLine();
  47.  
  48.    
  49.          binding = new SforceService();
  50.  
  51.    
  52.          LoginResult lr;
  53.          try
  54.          {
  55.  
  56.             Console.WriteLine("\nLogging in...\n");
  57.             lr = binding.login(username, password);
  58.          }
  59.  
  60.    
  61.          catch (SoapException e)
  62.          {
  63.  
  64.    
  65.             Console.WriteLine(e.Code);
  66.  
  67.  
  68.    
  69.             Console.WriteLine("An unexpected error has occurred: " + e.Message);
  70.  
  71.    
  72.             Console.WriteLine(e.StackTrace);
  73.    
  74.             return false;
  75.          }
  76.  
  77.  
  78.    
  79.          if (lr.passwordExpired)
  80.          {
  81.             Console.WriteLine("An error has occurred. Your password has expired.");
  82.             return false;
  83.          }
  84.  
  85.    
  86.          String authEndPoint = binding.Url;
  87.  
  88.    
  89.          binding.Url = lr.serverUrl;
  90.  
  91.          binding.SessionHeaderValue = new SessionHeader();
  92.          binding.SessionHeaderValue.sessionId = lr.sessionId;
  93.  
  94.          printUserInfo(lr, authEndPoint);
  95.  
  96.          return true;
  97.       }
  98.  
  99.       private void printUserInfo(LoginResult lr, String authEP)
  100.       {
  101.          try
  102.          {
  103.             GetUserInfoResult userInfo = lr.userInfo;
  104.  
  105.             Console.WriteLine("\nLogging in ...\n");
  106.             Console.WriteLine("UserID: " + userInfo.userId);
  107.             Console.WriteLine("User Full Name: " +
  108.                 userInfo.userFullName);
  109.             Console.WriteLine("User Email: " +
  110.                 userInfo.userEmail);
  111.             Console.WriteLine();
  112.             Console.WriteLine("SessionID: " +
  113.                 lr.sessionId);
  114.             Console.WriteLine("Auth End Point: " +
  115.                 authEP);
  116.             Console.WriteLine("Service End Point: " +
  117.                 lr.serverUrl);
  118.             Console.WriteLine();
  119.          }
  120.          catch (SoapException e)
  121.          {
  122.             Console.WriteLine("An unexpected error has occurred: " + e.Message +
  123.                 " Stack trace: " + e.StackTrace);
  124.          }
  125.       }
  126.  
  127.       private void logout()
  128.       {
  129.          try
  130.          {
  131.             binding.logout();
  132.             Console.WriteLine("Logged out.");
  133.          }
  134.          catch (SoapException e)
  135.          {
  136.  
  137.    
  138.             Console.WriteLine(e.Code);
  139.    
  140.             Console.WriteLine("An unexpected error has occurred: " + e.Message);
  141.             Console.WriteLine(e.StackTrace);
  142.          }
  143.       }
  144.  
  145.    
  146.       private void describeGlobalSample()
  147.       {
  148.          try
  149.         {
  150.    
  151.             DescribeGlobalResult dgr = binding.describeGlobal();
  152.  
  153.             Console.WriteLine("\nDescribe Global Results:\n");          
  154.             for (int i = 0; i < dgr.sobjects.Length; i++)
  155.             {
  156.                Console.WriteLine(dgr.sobjects[i].name);
  157.             }
  158.          }
  159.          catch (SoapException e)
  160.          {
  161.             Console.WriteLine("An exception has occurred: " + e.Message +
  162.                 "\nStack trace: " + e.StackTrace);
  163.          }
  164.       }
  165.  
  166.    
  167.       private void describeSObjectsSample()
  168.       {
  169.          Console.Write("\nType the name of the object to " +
  170.              "describe (try Account): ");
  171.          string objectType = Console.ReadLine();
  172.          try
  173.          {
  174.  
  175.             DescribeSObjectResult[] dsrArray = binding.describeSObjects(new string[] { objectType });
  176.    
  177.             DescribeSObjectResult dsr = dsrArray[0];              
  178.    
  179.             Console.WriteLine("\n\nObject Name: " + dsr.name);
  180.  
  181.             if (dsr.custom) Console.WriteLine("Custom Object");
  182.             if (dsr.label != null) Console.WriteLine("Label: " + dsr.label);
  183.    
  184.             if (dsr.createable) Console.WriteLine("Createable");
  185.             if (dsr.deletable) Console.WriteLine("Deleteable");
  186.             if (dsr.queryable) Console.WriteLine("Queryable");
  187.             if (dsr.replicateable) Console.WriteLine("Replicateable");
  188.             if (dsr.retrieveable) Console.WriteLine("Retrieveable");
  189.             if (dsr.searchable) Console.WriteLine("Searchable");
  190.             if (dsr.undeletable) Console.WriteLine("Undeleteable");
  191.             if (dsr.updateable) Console.WriteLine("Updateable");
  192.  
  193.             Console.WriteLine("Number of fields: " + dsr.fields.Length);
  194.  
  195.             for (int i = 0; i < dsr.fields.Length; i++)
  196.             {
  197.  
  198.                Field field = dsr.fields[i];
  199.  
  200.                Console.WriteLine("Field name: " + field.name);
  201.                Console.WriteLine("\tField Label: " + field.label);
  202.    
  203.                if (field.nameField)
  204.                   Console.WriteLine("\tThis is a name field.");
  205.  
  206.                if (field.restrictedPicklist)
  207.                   Console.WriteLine("This is a RESTRICTED picklist field.");
  208.                Console.WriteLine("\tType is: " + field.type.ToString());
  209.  
  210.                if (field.length > 0)
  211.                   Console.WriteLine("\tLength: " + field.length);
  212.  
  213.                if (field.scale > 0)
  214.                   Console.WriteLine("\tScale: " + field.scale);
  215.  
  216.                if (field.precision > 0)
  217.                   Console.WriteLine("\tPrecision: " + field.precision);
  218.  
  219.                if (field.digits > 0)
  220.                   Console.WriteLine("\tDigits: " + field.digits);
  221.  
  222.                if (field.custom)
  223.                   Console.WriteLine("\tThis is a custom field.");
  224.                if (field.nillable) Console.WriteLine("\tCan be nulled.");
  225.                if (field.createable) Console.WriteLine("\tCreateable");
  226.                if (field.filterable) Console.WriteLine("\tFilterable");
  227.                if (field.updateable) Console.WriteLine("\tUpdateable");
  228.    
  229.                if (field.type.Equals(fieldType.picklist))
  230.                {
  231.                   Console.WriteLine("\tPicklist Values");
  232.                   for (int j = 0; j < field.picklistValues.Length; j++)
  233.                      Console.WriteLine("\t\t" + field.picklistValues[j].value);
  234.                }
  235.    
  236.                if (field.type.Equals(fieldType.reference))
  237.                {
  238.                   Console.WriteLine("\tCan reference these objects:");
  239.                   for (int j = 0; j < field.referenceTo.Length; j++)
  240.                      Console.WriteLine("\t\t" + field.referenceTo[j]);
  241.                }
  242.                Console.WriteLine("");
  243.             }
  244.          }
  245.          catch (SoapException e)
  246.          {
  247.             Console.WriteLine("An exception has occurred: " + e.Message +
  248.                 "\nStack trace: " + e.StackTrace);
  249.          }
  250.          Console.WriteLine("Press ENTER to continue...");
  251.          Console.ReadLine();
  252.       }
  253.  
  254.       private void querySample()
  255.       {
  256.          String soqlQuery = "SELECT FirstName, LastName FROM Contact";
  257.          try
  258.          {
  259.             QueryResult qr = binding.query(soqlQuery);
  260.             bool done = false;
  261.  
  262.             if (qr.size > 0)
  263.             {
  264.                Console.WriteLine("Logged-in user can see "
  265.                      + qr.records.Length + " contact records.");
  266.  
  267.                while (!done)
  268.                {
  269.                   Console.WriteLine("");
  270.                   sObject[] records = qr.records;
  271.                   for (int i = 0; i < records.Length; i++)
  272.                   {
  273.                      Contact con = (Contact)records[i];
  274.                      string fName = con.FirstName;
  275.                      string lName = con.LastName;
  276.                      if (fName == null)
  277.                         Console.WriteLine("Connect " + (i + 1) + ": " + lName);
  278.                      else
  279.                         Console.WriteLine("Connect " + (i + 1) + ": " + fName
  280.                                + " " + lName);
  281.                   }
  282.  
  283.                   if (qr.done)
  284.                   {
  285.                      done = true;
  286.                   }
  287.                   else
  288.                   {
  289.                      qr = binding.queryMore(qr.queryLocator);
  290.                   }
  291.                }
  292.             }
  293.             else
  294.             {
  295.                Console.WriteLine("No records found.");
  296.             }
  297.          }
  298.          catch (Exception ex)
  299.          {
  300.             Console.WriteLine("\nFailed to execute query succesfully," +
  301.                 "error message was: \n{0}", ex.Message);
  302.          }
  303.          Console.WriteLine("\nPress ENTER to continue...");
  304.          Console.ReadLine();
  305.       }
  306.    }
  307. }
Add Comment
Please, Sign In to add comment