SOQL & SalesForce.com
Get all records for id:
List<id> listaToTrue=newList<id>();
for(Account a : [select id from Account where ParentId in:addAccountsActivate]){
listaParaActivar.add(a.id);
}
private SforceService binding;
public SalesForceTest()
{
ProcessConnection();
}
public bool ProcessConnection()
{
Boolean success = false;
// Create a service object
binding = new SforceService();
LoginResult lr;
try
{
Console.WriteLine("\nLogging in...\n");
lr = binding.login(LoginUserName, LoginUserPassword + LoginSecurityToken);
/**
* The login results contain the endpoint of the virtual server instance
* that is servicing your organization. Set the URL of the binding
* to this endpoint.
*/
// Save old authentication end point URL
String authEndPoint = binding.Url;
// Set returned service endpoint URL
binding.Url = lr.serverUrl;
/** Get the session ID from the login result and set it for the
* session header that will be used for all subsequent calls.
*/
binding.SessionHeaderValue = new SessionHeader();
binding.SessionHeaderValue.sessionId = lr.sessionId;
// Print user and session info
GetUserInfoResult userInfo = lr.userInfo;
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: " +
authEndPoint);
Console.WriteLine("Service End Point: " +
lr.serverUrl);
Console.WriteLine();
// Return true to indicate that we are logged in, pointed
// at the right URL and have our security token in place.
success = true;
}
catch (SoapException e)
{
Console.WriteLine("An unexpected error has occurred: " +
e.Message + "\n" + e.StackTrace);
}
return success;
}
public void CloseConnection()
{
binding.invalidateSessions(new[] {binding.SessionHeaderValue.sessionId});
Console.WriteLine("Closed connection");
}
public void GetQuery()
{
QueryResult qResult = null;
String soqlQuery = "SELECT ID, FirstName, LastName FROM Contact";
qResult = binding.query(soqlQuery);
Boolean done = false;
if (qResult.size > 0)
{
Console.WriteLine("Logged-in user can see a total of "
+ qResult.size + " contact records.");
while (!done)
{
sObject[] records = qResult.records;
for (int i = 0; i < records.Length; ++i)
{
Contact con = (Contact) records[i];
String fName = con.FirstName;
String lName = con.LastName;
Console.WriteLine("Contact " + (i + 1) + ": ID:" + con.Id +" "+fName +" " + lName);
}
if (qResult.done)
{
done = true;
}
else
{
qResult = binding.queryMore(qResult.queryLocator);
}
}
}
else
{
Console.WriteLine("No records found.");
}
Console.WriteLine("\nQuery succesfully executed.");
}
public List<string> CustomSelect()
{
var list= new List<string>();
QueryResult qResult = null;
String soqlQuery = "SELECT ID,firstName__c,LastName__c FROM ContactPerson__c";
qResult = binding.query(soqlQuery);
Boolean done = false;
if (qResult.size > 0)
{
Console.WriteLine("Logged-in user can see a total of "
+ qResult.size + " contact records.");
while (!done)
{
sObject[] records = qResult.records;
for (int i = 0; i < records.Length; ++i)
{
var con = (ContactPerson__c) records[i];
String fName = con.FirstName__c;
String lName = con.LastName__c;
list.Add(con.Id);
Console.WriteLine("Contact " + (i + 1) + ": ID:" + con.Id +" "+fName +" " + lName);
}
if (qResult.done)
{
done = true;
}
else
{
qResult = binding.queryMore(qResult.queryLocator);
}
}
}
else
{
Console.WriteLine("No records found.");
}
Console.WriteLine("\nQuery succesfully executed.");
return list;
}
public void Insert()
{
var contactPerson = new ContactPerson__c();
contactPerson.FirstName__c = string.Format("{0}{1}", "testFirstName", DateTime.Now.ToShortTimeString());
contactPerson.LastName__c = string.Format("{0}{1}", "testLastName", DateTime.Now.ToShortTimeString());
contactPerson.Email__c = string.Format("{0}{1}", DateTime.Now.ToString("ddMMhhmm"), "@gmail.com");
contactPerson.LandLine__c = string.Format("{0}{1}", "01225", DateTime.Now.ToShortTimeString());
contactPerson.CreatedDate = DateTime.Now;
var saveResult = binding.create(new[] {contactPerson});
if (saveResult.Length == 0)
{
Console.WriteLine("Failed to save");
}
foreach (var result in saveResult)
{
Console.WriteLine("id:{0} success:{1} errors:{2}", result.id, result.success,
(result.errors != null) ? result.errors.Length : 0);
if (result.errors != null)
foreach (var error in result.errors)
{
Console.WriteLine("Error: {0} Field: {1}", error.message, error.fields);
}
}
}
public bool UpdateRecord(string recordId)
{
try
{
sObject[] sObjects = binding.retrieve("ID, FirstName__c,LastName__c,Email__c,LandLine__c", "ContactPerson__c",new[] { recordId });
// Verify that some objects were returned.
// Even though we began with valid object IDs,
// someone else might have deleted them in the meantime.
// Cast the SObject into an ContactPerson__c object
ContactPerson__c contactPerson = (ContactPerson__c)sObjects.First();
if (contactPerson != null)
{
Console.WriteLine("contact person found");
ContactPerson__c con = (ContactPerson__c) contactPerson;
con.FirstName__c = DateTime.Today.ToShortDateString();
var saveResult = binding.update(new[] { con });
if (saveResult.Length == 0)
{
Console.WriteLine("Failed to save");
}
foreach (var result in saveResult)
{
Console.WriteLine("id:{0} success:{1} errors:{2}", result.id, result.success,
(result.errors != null) ? result.errors.Length : 0);
if (result.errors != null)
foreach (var error in result.errors)
{
Console.WriteLine("Error: {0} Field: {1}", error.message, error.fields);
}
}
}
else
{
Console.WriteLine("Nof Found contact person");
}
}
catch (SoapException e)
{
Console.WriteLine("An unexpected error has occurred: " +
e.Message + "\n" + e.StackTrace);
}
return false;
}
/// <summary>
/// Deletes the record.
/// </summary>
/// <param name="recordId">The record id.</param>
public void DeleteRecord(string recordId)
{
try
{
DeleteResult[] deleteResults = binding.delete(new[] { recordId });
for (int i = 0; i < deleteResults.Length; i++)
{
DeleteResult deleteResult = deleteResults[i];
if (deleteResult.success)
{
Console.WriteLine("Deleted Record ID: " + deleteResult.id);
}
else
{
// Handle the errors.
// We just print the first error out for sample purposes.
Error[] errors = deleteResult.errors;
if (errors.Length > 0)
{
Console.WriteLine("Error: could not delete " + "Record ID "
+ deleteResult.id + ".");
Console.WriteLine(" The error reported was: ("
+ errors[0].statusCode + ") "
+ errors[0].message + "\n");
}
}
}
}
catch (SoapException e)
{
Console.WriteLine("An unexpected error has occurred: " +
e.Message + "\n" + e.StackTrace);
}
}