use of com.salesforce.soap.partner.DescribeSObjectResult in project tdi-studio-se by Talend.
the class PartnerSamples method upsertSample.
private void upsertSample() {
// call the login function to do so
if (!loggedIn) {
if (!login()) {
return;
}
}
try {
DescribeSObjectResult dsr = binding.describeSObject("Account");
HashMap fieldMap = makeFieldMap(dsr.getFields());
if (!fieldMap.containsKey("External_Id__c")) {
System.out.println("\n\nATTENTION: To run this sample you need to \ncreate a custom text field on the Account object \nnamed External_Id with a length of 8 characters \nand with the 'external id' checkbox checked.");
} else {
// First, we need to make sure the test accounts do not exist.
QueryResult qr = binding.query("Select Id From Account Where External_Id__c = '11111111' or External_Id__c = '22222222'");
if (qr.getSize() > 0) {
SObject[] accounts = qr.getRecords();
// Get the ids
String[] ids = new String[accounts.length];
for (int i = 0; i < ids.length; i++) {
ids[i] = accounts[i].getId();
}
// Delete the accounts
binding.delete(ids);
}
// Create a new account using create, we wil use this to update via upsert
// We will set the external id to be ones so that we can use that value for the upsert
SObject newAccount = new SObject();
newAccount.setType("Account");
MessageElement acctName = newMessageElement("Name", "Account to update");
MessageElement extId = newMessageElement("External_Id__c", "11111111");
newAccount.set_any(new MessageElement[] { acctName, extId });
binding.create(new SObject[] { newAccount });
// Now we will create an account that should be updated on insert based
// on the external id field.
SObject updateAccount = new SObject();
updateAccount.setType("Account");
MessageElement webSite = newMessageElement("Website", "http://www.website.com");
MessageElement extId1 = newMessageElement("External_Id__c", "11111111");
updateAccount.set_any(new MessageElement[] { webSite, extId1 });
// This account is meant to be new
SObject createAccount = new SObject();
createAccount.setType("Account");
MessageElement cacctName = newMessageElement("Name", "My Company, Inc");
MessageElement extId3 = newMessageElement("External_Id__c", "22222222");
createAccount.set_any(new MessageElement[] { cacctName, extId3 });
// We have our two accounts, one should be new, the other should be updated.
try {
// Invoke the upsert call and save the results.
// Use External_Id custom field for matching records
UpsertResult[] upsertResults = binding.upsert("External_Id__c", new SObject[] { createAccount, updateAccount });
for (UpsertResult result : upsertResults) {
if (result.isSuccess()) {
System.out.println("\nUpsert succeeded.");
System.out.println((result.isCreated() ? "Inserted" : "Updated") + " account, id is " + result.getId().toString());
} else {
System.out.println("The Upsert failed because: " + result.getErrors(0).getMessage());
}
}
} catch (RemoteException ex) {
System.out.println("An unexpected error has occurred." + ex.getMessage());
}
}
getUserInput("\nPress the RETURN key to continue...");
} catch (ApiFault e) {
System.out.println("Error merging account: " + e.getExceptionMessage());
getUserInput("\nHit return to continue...");
} catch (RemoteException e) {
System.out.println("Error from the server on the merge sample: " + e.getMessage());
getUserInput("\nHit return to continue...");
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.salesforce.soap.partner.DescribeSObjectResult in project tdi-studio-se by Talend.
the class SObjectsToSchema method generateSchema.
void generateSchema(String inputFileName, String outputFileName) throws IOException {
File inputFile = new File(inputFileName);
File outputFile = new File(outputFileName);
StringBuffer inputContent = new StringBuffer();
// StringBuffer deleteContent = new StringBuffer();
StringBuffer insertContent = new StringBuffer();
StringBuffer updateContent = new StringBuffer();
StringBuffer upsertContent = new StringBuffer();
DescribeGlobalResult describeGlobalResult = null;
describeGlobalResult = binding.describeGlobal();
DescribeGlobalSObjectResult[] sobjectResults = describeGlobalResult.getSobjects();
for (int i = 0; i < sobjectResults.length; i++) {
String moduleName = sobjectResults[i].getName();
DescribeSObjectResult descSObjectRslt;
descSObjectRslt = binding.describeSObject(moduleName);
if (descSObjectRslt != null) {
// Report object level information
if (!descSObjectRslt.isCustom()) {
// if (descSObjectRslt.isQueryable()) {
//mark
inputContent.append(generateInputTable(moduleName, descSObjectRslt));
// }
// if (descSObjectRslt.isCreateable()) {
insertContent.append(generateOutputTable(moduleName, descSObjectRslt, INSERT));
// }
// if (descSObjectRslt.isUpdateable()) {
updateContent.append(generateOutputTable(moduleName, descSObjectRslt, UPDATE));
// }
// if(descSObjectRslt.isUpdateable()&&descSObjectRslt.isCreateable()){
upsertContent.append(generateOutputTable(moduleName, descSObjectRslt, UPSERT));
// }
// if (descSObjectRslt.isDeletable()) {
//
// }
}
}
}
BufferedWriter inOutput = new BufferedWriter(new FileWriter(inputFile));
inOutput.write(inputContent.toString());
inOutput.close();
String sepLint = "<!-- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -->";
BufferedWriter OuOutput = new BufferedWriter(new FileWriter(outputFile));
OuOutput.write(insertContent.toString() + "\r\n" + sepLint + "\r\n" + updateContent.toString() + "\r\n" + sepLint + "\r\n" + upsertContent.toString());
OuOutput.close();
}
use of com.salesforce.soap.partner.DescribeSObjectResult in project tdi-studio-se by Talend.
the class PartnerSamples method describeSample.
private void describeSample() {
if (!loggedIn) {
if (!login()) {
return;
}
}
String objectToDescribe = getUserInput("\nType the name of the object to describe (try Account): ");
try {
DescribeSObjectResult ds;
DescribeSObjectResult describeSObjectResult = binding.describeSObject(objectToDescribe);
if (!(describeSObjectResult == null)) {
Field[] fields = describeSObjectResult.getFields();
String objectName = describeSObjectResult.getName();
boolean isActivateable = describeSObjectResult.isActivateable();
boolean isCreateable = describeSObjectResult.isCreateable();
boolean isCustom = describeSObjectResult.isCustom();
boolean isDeleteable = describeSObjectResult.isDeletable();
boolean isQueryable = describeSObjectResult.isQueryable();
boolean isReplicateable = describeSObjectResult.isReplicateable();
boolean isRetrieveable = describeSObjectResult.isRetrieveable();
boolean isSearchable = describeSObjectResult.isSearchable();
boolean isUndeleteable = describeSObjectResult.isUndeletable();
boolean isUpdateable = describeSObjectResult.isUpdateable();
System.out.println("Metadata for " + objectToDescribe + " object:\n");
System.out.println("Object name = " + objectName);
System.out.println("Number of fields = " + fields.length);
System.out.println("Object can be activated = " + isActivateable);
System.out.println("Can create rows of data = " + isCreateable);
System.out.println("Object is custom object = " + isCustom);
System.out.println("Can delete rows of data = " + isDeleteable);
System.out.println("Can query for rows of data = " + isQueryable);
System.out.println("Object can be used in replication = " + isReplicateable);
System.out.println("Can use retrieve method on object = " + isRetrieveable);
System.out.println("Can use search method on object = " + isSearchable);
System.out.println("Can un-delete rows of data = " + isUndeleteable);
System.out.println("Can update rows of data = " + isUpdateable);
System.out.println("\nField metadata for " + objectToDescribe + " object:\n");
if (!(fields == null)) {
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
int byteLength = field.getByteLength();
int digits = field.getDigits();
String label = field.getLabel();
int length = field.getLength();
String name = field.getName();
PicklistEntry[] picklistValues = field.getPicklistValues();
int precision = field.getPrecision();
String[] referenceTos = field.getReferenceTo();
int scale = field.getScale();
FieldType fieldType = field.getType();
boolean fieldIsAutoNumber = field.isAutoNumber();
boolean fieldIsCreateable = field.isCreateable();
boolean fieldIsCustom = field.isCustom();
boolean fieldIsDefaultedOnCreate = field.isDefaultedOnCreate();
boolean fieldIsFilterable = field.isFilterable();
boolean fieldIsNillable = field.isNillable();
boolean fieldIsRestrictedPicklist = field.isRestrictedPicklist();
boolean fieldIsUpdateable = field.isUpdateable();
System.out.println("************* New Field ***************");
System.out.println("Name = " + name);
System.out.println("Label = " + label);
System.out.println("Length = " + length);
System.out.println("Bytelength = " + byteLength);
System.out.println("Digits = " + digits);
System.out.println("Precision = " + precision);
System.out.println("Scale = " + scale);
System.out.println("Field type = " + fieldType);
if (picklistValues != null) {
System.out.println("Picklist values = ");
for (int j = 0; j < picklistValues.length; j++) {
if (picklistValues[j].getLabel() != null) {
System.out.println(" Item: " + picklistValues[j].getLabel());
} else {
System.out.println(" Item: " + picklistValues[j].getValue());
}
System.out.println(" value = " + picklistValues[j].getValue());
System.out.println(" is default = " + picklistValues[j].isDefaultValue());
}
}
if (referenceTos != null) {
System.out.println("Field references the following objects:");
for (int j = 0; j < referenceTos.length; j++) {
System.out.println(" " + referenceTos[j]);
}
}
System.out.println("\n");
}
getUserInput("\nDescribe " + objectToDescribe + " was successful.\n\nHit the enter key to conutinue....");
}
}
} catch (ApiFault af) {
System.out.println("\nFailed to get " + objectToDescribe + " description, error message was: \n" + af.getExceptionMessage());
getUserInput("\nHit return to continue...");
} catch (Exception ex) {
System.out.println("\nFailed to get " + objectToDescribe + " description, error message was: \n" + ex.getMessage());
getUserInput("\nHit return to continue...");
}
}
use of com.salesforce.soap.partner.DescribeSObjectResult in project tdi-studio-se by Talend.
the class PartnerSamples method describeSObjectsSample.
private void describeSObjectsSample() {
if (!loggedIn) {
if (!login()) {
return;
}
}
try {
DescribeSObjectResult[] describeSObjectResults = binding.describeSObjects(new String[] { "account", "contact", "lead" });
for (int x = 0; x < describeSObjectResults.length; x++) {
DescribeSObjectResult describeSObjectResult = describeSObjectResults[x];
// Retrieve fields from the results
Field[] fields = describeSObjectResult.getFields();
// Get the name of the object
String objectName = describeSObjectResult.getName();
// Get some flags
boolean isActivateable = describeSObjectResult.isActivateable();
// Many other values are accessible
if (fields != null) {
// field
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
int byteLength = field.getByteLength();
int digits = field.getDigits();
String label = field.getLabel();
int length = field.getLength();
String name = field.getName();
PicklistEntry[] picklistValues = field.getPicklistValues();
int precision = field.getPrecision();
String[] referenceTos = field.getReferenceTo();
int scale = field.getScale();
FieldType fieldType = field.getType();
boolean fieldIsCreateable = field.isCreateable();
// Determine whether there are picklist values
if (picklistValues != null && picklistValues[0] != null) {
System.out.println("Picklist values = ");
for (int j = 0; j < picklistValues.length; j++) {
if (picklistValues[j].getLabel() != null) {
System.out.println(" Item: " + picklistValues[j].getLabel());
}
}
}
// Determine whether this field refers to another object
if (referenceTos != null && referenceTos[0] != null) {
System.out.println("Field references the following objects:");
for (int j = 0; j < referenceTos.length; j++) {
System.out.println(" " + referenceTos[j]);
}
}
}
}
}
getUserInput("\nHit return to continue...");
} catch (ApiFault af) {
System.out.println("\nFailed to get describe multiple objects, error message was: \n" + af.getExceptionMessage());
getUserInput("\nHit return to continue...");
} catch (Exception ex) {
System.out.println("\nFailed to get describe multiple objects, error message was: \n" + ex.getMessage());
getUserInput("\nHit return to continue...");
}
}
Aggregations