Skip to content

Instantly share code, notes, and snippets.

@veresh
Last active April 15, 2016 06:37
Show Gist options
  • Save veresh/b16e71241c9fbaffb140166de4e7b9f0 to your computer and use it in GitHub Desktop.
Save veresh/b16e71241c9fbaffb140166de4e7b9f0 to your computer and use it in GitHub Desktop.
C# code to get list of all objects from SFDC instance.
using log4net;
using log4net.Config;
using SFDCtoCRM_Migration.WebReference;
using System;
using System.Configuration;
// Code assumes that you have imported SFDC Enterprise WSDL into your solution
namespace SFDCtoCRM_Migration
{
class Migrate
{
private static readonly ILog logger = LogManager.GetLogger(typeof(Migrate));
static void Main(string[] args)
{
DOMConfigurator.Configure();
string userName = ConfigurationManager.AppSettings.Get("sfdc.username");
string password = ConfigurationManager.AppSettings.Get("sfdc.password");
logger.Info("SFDC Username " + userName);
WebReference.SforceService SfdcBinding = null;
WebReference.LoginResult CurrentLoginResult = null;
SfdcBinding = new SforceService();
try
{
CurrentLoginResult = SfdcBinding.login(userName, password);
//Change the binding to the new endpoint
SfdcBinding.Url = CurrentLoginResult.serverUrl;
//Create a new session header object and set the session id to that returned by the login
SfdcBinding.SessionHeaderValue = new SessionHeader();
SfdcBinding.SessionHeaderValue.sessionId = CurrentLoginResult.sessionId;
DescribeGlobalResult globalResult = SfdcBinding.describeGlobal();
int length = globalResult.sobjects.Length;
logger.Debug("Count of Sobjects ::: " + length);
for (int i = 0; i < length; ++i)
{
String sobjname = globalResult.sobjects[i].name;
logger.Debug("Sobject is ::: " + sobjname);
int fieldLen = SfdcBinding.describeSObject(sobjname).fields.Length;
DescribeSObjectResult sobjectResult = SfdcBinding.describeSObject(sobjname);
String query = "";
for (int j = 0; j < fieldLen; ++j)
{
logger.Debug(" Field is " + sobjectResult.fields[j].name + " - " + sobjectResult.fields[j].type);
query = query + sobjectResult.fields[j].name;
if (j != fieldLen - 1)
{
query = query + " , ";
}
}
logger.Debug("select " + query + " from " + sobjname);
}
}
catch (Exception e)
{
//
SfdcBinding = null;
logger.Error(e);
}
finally
{
if (SfdcBinding != null)
{
SfdcBinding.logout();
SfdcBinding.Dispose();
SfdcBinding = null;
}
}
logger.Debug("Current OrgID is :: " + CurrentLoginResult.userInfo.organizationId);
// This will just block things
logger.Info("Execution completed, hit any key to close this window ..");
Console.Read();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment