/// <summary> /// Use Enterprise API to create new SFDC records /// </summary> /// <param name="sessionId"></param> /// <param name="serverUrl"></param> private static void CreateEnterpriseRecords() { Console.WriteLine("Creating an account record with the Enterprise API ..."); //set query endpoint to value returned by login request EndpointAddress apiAddr = new EndpointAddress(serverUrl); //instantiate session header object and set session id enterprise.SessionHeader header = new enterprise.SessionHeader(); header.sessionId = sessionId; //create service client to call API endpoint using (enterprise.SoapClient createClient = new enterprise.SoapClient("Soap", apiAddr)) { enterprise.Account newAcct = new enterprise.Account(); newAcct.Name = "DevForce02"; newAcct.AccountNumber = "10043332"; //all non-string fields must have their corresponding <name>Specified property set newAcct.AnnualRevenue = 4000000f; //newAcct.AnnualRevenueSpecified = true; enterprise.Opportunity o = new enterprise.Opportunity(); o.Name = "Opp2"; o.StageName = "Prospecting"; o.CloseDate = DateTime.Parse("2013-03-22"); o.CloseDateSpecified = true; enterprise.SaveResult[] results; createClient.create( header, //sessionheader null, //assignmentruleheader null, //mruheader null, //allowfieldtruncationheader null, //disablefeedtrackingheader null, //streamingenabledheader null, //allornoneheader null, //debuggingheader null, //packageversionheader null, //emailheader new enterprise.sObject[] { o }, //objects to add out results //results of the creation operation ); //only added one item, so looking at first index of results object if (results[0].success) { Console.WriteLine("Account successfully created."); } else { Console.WriteLine(results[0].errors[0].message); } Console.ReadLine(); } }
/// <summary> /// Call SFDC endpoint and retrieve authentication token and API URL for SOAP callers /// </summary> private static void AuthenticateSfdcEnterpriseUser() { //print message to console Console.WriteLine("Authenticating against the Enterprise API ..."); //use default binding and address from app.config using (enterprise.SoapClient loginClient = new enterprise.SoapClient("Soap")) { //set account password and account token variables string sfdcPassword = "******"; string sfdcToken = "[token]"; //set to Force.com user account that has API access enabled string sfdcUserName = "******"; //create login password value string loginPassword = sfdcPassword + sfdcToken; //call Login operation from Enterprise WSDL enterprise.LoginResult result = loginClient.login( null, //LoginScopeHeader sfdcUserName, loginPassword); //get response values sessionId = result.sessionId; serverUrl = result.serverUrl; //print response values Console.WriteLine(string.Format("The session ID is {0} and server URL is {1}", sessionId, serverUrl)); Console.WriteLine(""); Console.WriteLine("Press [Enter] to continue ..."); Console.ReadLine(); } }
/// <summary> /// Use Enteprise API to query and retrieve SFDC records /// </summary> /// <param name="sessionId"></param> /// <param name="serverUrl"></param> private static void QueryEnterpriseRecord() { Console.WriteLine("Querying account records with the Enterprise API ..."); //set query endpoint to value returned by login request EndpointAddress apiAddr = new EndpointAddress(serverUrl); //instantiate session header object and set session id enterprise.SessionHeader header = new enterprise.SessionHeader(); header.sessionId = sessionId; //create service client to call API endpoint using (enterprise.SoapClient queryClient = new enterprise.SoapClient("Soap", apiAddr)) { //query standard or custom objects //create SOQL query statement string query = "SELECT Name, AccountNumber, BillingState FROM Account WHERE BillingState = 'CA'"; enterprise.QueryResult result = queryClient.query( header, //sessionheader null, //queryoptions null, //mruheader null, //packageversion query); //cast query results IEnumerable<enterprise.Account> accountList = result.records.Cast<enterprise.Account>(); //show results foreach (var account in accountList) { Console.WriteLine(string.Format("Account Name: {0}", account.Name)); } Console.WriteLine(""); Console.WriteLine("Query complete."); Console.ReadLine(); //retrieve example //call retrieve operation to get one or more records of a given type and ID enterprise.sObject[] retrievedAccounts = queryClient.retrieve( header, //sessionheader null, //queryoptions null, //mruheader null, //packageversion "Name, BillingState", //fieldlist "Account", //objectype new string[] { "001E000000N1H1O" } //record IDs ); foreach (enterprise.sObject so in retrievedAccounts) { enterprise.Account acct = (enterprise.Account)so; Console.WriteLine(string.Format("Account Name: {0}, Account State: {1}", acct.Name, acct.BillingState)); } Console.WriteLine(""); Console.WriteLine("Retrieve complete."); Console.ReadLine(); } }
/* * login sample * Prompts for username and password, set class variable binding * resets the url for the binding and adds the session header to * the binding class variable */ private bool login() { Console.Write("Enter user name: "); un = Console.ReadLine(); if (un == null) { return false; } Console.Write("Enter password: "******"Enter Token: "); token = Console.ReadLine(); if (token == null) { return false; } //Provide feed back while we create the web service binding Console.WriteLine("Creating the binding to the web service..."); /* * Create the binding to the sforce servics */ binding = new SoapCaller.SfdcReference.SoapClient(); // Time out after a minute //binding.Timeout = 60000; //Attempt the login giving the user feedback Console.WriteLine("LOGGING IN NOW...."); //binding.Proxy = new System.Net.WebProxy("localhost:8082"); try { Console.Write("Enter user name: "); un = Console.ReadLine(); if (un == null) { return false; } Console.Write("Enter password: "******"Enter Token: "); token = Console.ReadLine(); if (token == null) { return false; } //Provide feed back while we create the web service binding Console.WriteLine("Creating the binding to the web service..."); //login to the SOAP Enterprise endpoint AuthenticateSfdcEnterpriseUser(un,pw,token); //login to the SOAP Partner endpoint AuthenticateSfdcPartnerUser(un, pw, token); ; } catch (System.Web.Services.Protocols.SoapException e) { // This is likley to be caused by bad username or password Console.Write(e.Message + ", please try again.\n\nHit return to continue..."); Console.ReadLine(); return false; } catch (Exception e) { // This is something else, probably comminication Console.Write(e.Message + ", please try again.\n\nHit return to continue..."); Console.ReadLine(); return false; } Console.WriteLine("\nThe session id is: " + loginResult.sessionId); Console.WriteLine("\nThe new server url is: " + loginResult.serverUrl); //Change the binding to the new endpoint serverUrl = loginResult.serverUrl; //Create a new session header object and set the session id to that returned by the login sessionId = loginResult.sessionId; loggedIn = true; // call the getServerTimestamp method getServerTimestampSample(); ///call the getUserInfo method getUserInfoSample(); return true; }
/// <summary> /// Use Enteprise API to query and retrieve SFDC records /// </summary> /// <param name="sessionId"></param> /// <param name="serverUrl"></param> private static void QueryEnterpriseRecord() { Console.WriteLine("Querying account records with the Enterprise API ..."); //set query endpoint to value returned by login request EndpointAddress apiAddr = new EndpointAddress(serverUrl); //instantiate session header object and set session id enterprise.SessionHeader header = new enterprise.SessionHeader(); header.sessionId = sessionId; //create service client to call API endpoint using (enterprise.SoapClient queryClient = new enterprise.SoapClient("Soap", apiAddr)) { //query standard or custom objects //create SOQL query statement string query = "SELECT Name, AccountNumber, BillingState FROM Account WHERE BillingState = 'CA'"; enterprise.QueryResult result = queryClient.query( header, //sessionheader null, //queryoptions null, //mruheader null, //packageversion query); //cast query results IEnumerable <enterprise.Account> accountList = result.records.Cast <enterprise.Account>(); //show results foreach (var account in accountList) { Console.WriteLine(string.Format("Account Name: {0}", account.Name)); } Console.WriteLine(""); Console.WriteLine("Query complete."); Console.ReadLine(); //retrieve example //call retrieve operation to get one or more records of a given type and ID enterprise.sObject[] retrievedAccounts = queryClient.retrieve( header, //sessionheader null, //queryoptions null, //mruheader null, //packageversion "Name, BillingState", //fieldlist "Account", //objectype new string[] { "001E000000N1H1O" } //record IDs ); foreach (enterprise.sObject so in retrievedAccounts) { enterprise.Account acct = (enterprise.Account)so; Console.WriteLine(string.Format("Account Name: {0}, Account State: {1}", acct.Name, acct.BillingState)); } Console.WriteLine(""); Console.WriteLine("Retrieve complete."); Console.ReadLine(); } }