Skip to content

Instantly share code, notes, and snippets.

@ericabouaf
Created December 20, 2010 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericabouaf/748673 to your computer and use it in GitHub Desktop.
Save ericabouaf/748673 to your computer and use it in GitHub Desktop.
A simple SOQL call from a Groovy script
import com.sforce.soap.enterprise.Connector
import com.sforce.soap.enterprise.DeleteResult
import com.sforce.soap.enterprise.EnterpriseConnection
import com.sforce.soap.enterprise.Error
import com.sforce.soap.enterprise.QueryResult
import com.sforce.soap.enterprise.SaveResult
import com.sforce.soap.enterprise.sobject.Account
import com.sforce.soap.enterprise.sobject.Contact
import com.sforce.ws.ConnectionException
import com.sforce.ws.ConnectorConfig
ConnectorConfig config = new ConnectorConfig()
config.setUsername("your-mail@your-domain.com")
config.setPassword("your_password_plus_api_token")
connection = Connector.newConnection(config)
// query for the 5 newest contacts
QueryResult queryResults = connection.query("SELECT Id, FirstName, LastName, Account.Name " +
"FROM Contact WHERE AccountId != NULL ORDER BY CreatedDate DESC LIMIT 5")
// Render the results as HTML
String t = new String()
if (queryResults.getSize() > 0) {
for (int i=0;i<queryResults.getRecords().length;i++) {
// cast the SObject to a strongly-typed Contact
Contact c = (Contact)queryResults.getRecords()[i];
t += "<a href='https://emea.salesforce.com/"+c.getId()+"'>"+("Id: " + c.getId() + " - Name: "+c.getFirstName()+" "+
c.getLastName()+" - Account: "+c.getAccount().getName())+"</a><br />";
}
}
// Return the HTML string
t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment