use of com.salesforce.soap.partner.UpsertResult 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.UpsertResult in project tdi-studio-se by Talend.
the class SforceManagementImpl method readResult.
@Override
public Map<String, String> readResult(Object[] results) throws Exception {
Map<String, String> resultMessage = null;
if (results instanceof SaveResult[]) {
for (SaveResult result : (SaveResult[]) results) {
resultMessage = new HashMap<String, String>();
if (result.getId() != null) {
resultMessage.put("id", result.getId().getID());
}
resultMessage.put("success", String.valueOf(result.getSuccess()));
if (!result.getSuccess()) {
for (Error error : result.getErrors()) {
if (error.getStatusCode() != null) {
resultMessage.put("StatusCode", error.getStatusCode().toString());
}
if (error.getFields() != null) {
StringBuffer fields = new StringBuffer();
for (String field : error.getFields()) {
fields.append(field);
fields.append(",");
}
if (fields.length() > 0) {
fields.deleteCharAt(fields.length() - 1);
}
resultMessage.put("Fields", fields.toString());
}
resultMessage.put("Message", error.getMessage());
}
}
}
return resultMessage;
} else if (results instanceof DeleteResult[]) {
for (DeleteResult result : (DeleteResult[]) results) {
resultMessage = new HashMap<String, String>();
if (result.getId() != null) {
resultMessage.put("id", result.getId().getID());
}
resultMessage.put("success", String.valueOf(result.getSuccess()));
if (!result.getSuccess()) {
for (Error error : result.getErrors()) {
if (error.getStatusCode() != null) {
resultMessage.put("StatusCode", error.getStatusCode().toString());
}
resultMessage.put("Message", error.getMessage());
}
}
}
return resultMessage;
} else if (results instanceof UpsertResult[]) {
for (UpsertResult result : (UpsertResult[]) results) {
resultMessage = new HashMap<String, String>();
if (result.getId() != null) {
resultMessage.put("id", result.getId().getID());
}
resultMessage.put("success", String.valueOf(result.getSuccess()));
resultMessage.put("created", String.valueOf(result.getCreated()));
if (!result.getSuccess()) {
for (Error error : result.getErrors()) {
if (error.getStatusCode() != null) {
resultMessage.put("StatusCode", error.getStatusCode().toString());
}
if (error.getFields() != null) {
StringBuffer fields = new StringBuffer();
for (String field : error.getFields()) {
fields.append(field);
fields.append(",");
}
if (fields.length() > 0) {
fields.deleteCharAt(fields.length() - 1);
}
resultMessage.put("Fields", fields.toString());
}
resultMessage.put("Message", error.getMessage());
}
}
}
return resultMessage;
}
return null;
}
use of com.salesforce.soap.partner.UpsertResult in project tdi-studio-se by Talend.
the class SforceManagementImpl method upsert.
/**
* upsert, one record, one time.
*/
@Override
public UpsertResult[] upsert(String tablename, String upsertkey, OMElement[] updatefields, String[] fieldsToNull) throws Exception {
// create the account object to hold our changes
SObject item = new SObject();
item.setType(tablename);
// set a new value for the name property
item.setExtraElement(updatefields);
// set a null field for the name property
item.setFieldsToNull(fieldsToNull);
upsertItems.add(item);
upsertKeyColumn = upsertkey;
// call the update passing an array of object
if (upsertItems.size() >= commitLevel) {
SObject[] upds = upsertItems.toArray(new SObject[upsertItems.size()]);
String[] changedItemKeys = new String[upds.length];
for (int ix = 0; ix < upds.length; ++ix) {
changedItemKeys[ix] = "No value for " + upsertKeyColumn + " ";
OMElement[] oms = upds[ix].getExtraElement();
for (OMElement om : oms) {
if (upsertKeyColumn != null && om != null && upsertKeyColumn.equals(om.getLocalName())) {
changedItemKeys[ix] = om.getText();
break;
}
}
}
Upsert upsert = new Upsert();
upsert.setSObjects(upds);
upsert.setExternalIDFieldName(upsertKeyColumn);
UpsertResult[] upsertResults = sforceConn.upsert(upsert).getResult();
upsertItems.clear();
upds = null;
if (upsertResults != null && upsertResults.length != 0) {
int batch_idx = -1;
for (UpsertResult result : upsertResults) {
++batch_idx;
StringBuilder errors = new StringBuilder("");
if (result.getSuccess()) {
// TODO: send back the ID
} else {
errors = addLog(result.getErrors(), batch_idx < changedItemKeys.length ? changedItemKeys[batch_idx] : "Batch index out of bounds");
}
if (exceptionForErrors && errors.toString().length() > 0) {
if (logWriter != null) {
logWriter.close();
}
throw new Exception(errors.toString());
}
}
}
return upsertResults;
}
return null;
}
use of com.salesforce.soap.partner.UpsertResult in project tdi-studio-se by Talend.
the class SforceManagementImpl method logout.
/**
* logout
*/
@Override
public void logout() throws Exception {
Object returnObject = null;
// if there are still records to be commited:
try {
if (insertItems.size() > 0) {
SObject[] accs = insertItems.toArray(new SObject[insertItems.size()]);
String[] changedItemKeys = new String[accs.length];
Create create = new Create();
create.setSObjects(accs);
SaveResult[] sr = sforceConn.create(create).getResult();
insertItems.clear();
accs = null;
if (sr != null && sr.length != 0) {
int batch_idx = -1;
for (SaveResult result : sr) {
++batch_idx;
StringBuilder errors = new StringBuilder("");
if (result.getSuccess()) {
// TODO: send back the ID
} else {
errors = addLog(result.getErrors(), batch_idx < changedItemKeys.length ? "" + (batch_idx + 1) : "Batch index out of bounds");
}
if (exceptionForErrors && errors.toString().length() > 0) {
if (logWriter != null) {
logWriter.close();
}
throw new Exception(errors.toString());
}
}
}
returnObject = sr;
}
if (deleteItems.size() > 0) {
ID[] delIDs = deleteItems.toArray(new ID[deleteItems.size()]);
String[] changedItemKeys = new String[delIDs.length];
for (int ix = 0; ix < delIDs.length; ++ix) {
changedItemKeys[ix] = delIDs[ix].getID();
}
Delete dels = new Delete();
dels.setIds(delIDs);
DeleteResponse dresp = sforceConn.delete(dels);
DeleteResult[] dr = dresp.getResult();
deleteItems.clear();
delIDs = null;
if (dr != null && dr.length != 0) {
int batch_idx = -1;
for (DeleteResult result : dr) {
++batch_idx;
StringBuilder errors = new StringBuilder("");
if (result.getSuccess()) {
// TODO: send back the ID
} else {
errors = addLog(result.getErrors(), batch_idx < changedItemKeys.length ? changedItemKeys[batch_idx] : "Batch index out of bounds");
}
if (exceptionForErrors && errors.toString().length() > 0) {
if (logWriter != null) {
logWriter.close();
}
throw new Exception(errors.toString());
}
}
}
returnObject = dr;
}
if (updateItems.size() > 0) {
SObject[] upds = updateItems.toArray(new SObject[updateItems.size()]);
String[] changedItemKeys = new String[upds.length];
for (int ix = 0; ix < upds.length; ++ix) {
changedItemKeys[ix] = upds[ix].getId().getID();
}
Update update = new Update();
update.setSObjects(upds);
SaveResult[] saveResults = sforceConn.update(update).getResult();
updateItems.clear();
upds = null;
if (saveResults != null && saveResults.length != 0) {
int batch_idx = -1;
for (SaveResult result : saveResults) {
++batch_idx;
StringBuilder errors = new StringBuilder("");
if (result.getSuccess()) {
// TODO: send back the ID
} else {
errors = addLog(result.getErrors(), batch_idx < changedItemKeys.length ? changedItemKeys[batch_idx] : "Batch index out of bounds");
}
if (exceptionForErrors && errors.toString().length() > 0) {
if (logWriter != null) {
logWriter.close();
}
throw new Exception(errors.toString());
}
}
}
returnObject = saveResults;
}
if (upsertItems.size() > 0) {
SObject[] upds = upsertItems.toArray(new SObject[upsertItems.size()]);
String[] changedItemKeys = new String[upds.length];
for (int ix = 0; ix < upds.length; ++ix) {
changedItemKeys[ix] = "No value for " + upsertKeyColumn + " ";
OMElement[] oms = upds[ix].getExtraElement();
for (OMElement om : oms) {
if (upsertKeyColumn != null && om != null && upsertKeyColumn.equals(om.getLocalName())) {
changedItemKeys[ix] = om.getText();
break;
}
}
}
Upsert upsert = new Upsert();
upsert.setSObjects(upds);
upsert.setExternalIDFieldName(upsertKeyColumn);
UpsertResult[] upsertResults = sforceConn.upsert(upsert).getResult();
upsertItems.clear();
upds = null;
if (upsertResults != null && upsertResults.length != 0) {
int batch_idx = -1;
for (UpsertResult result : upsertResults) {
++batch_idx;
StringBuilder errors = new StringBuilder("");
if (result.getSuccess()) {
// TODO: send back the ID
} else {
errors = addLog(result.getErrors(), batch_idx < changedItemKeys.length ? changedItemKeys[batch_idx] : "Batch index out of bounds");
}
if (exceptionForErrors && errors.toString().length() > 0) {
if (logWriter != null) {
logWriter.close();
}
throw new Exception(errors.toString());
}
}
}
returnObject = upsertResults;
}
} catch (Exception e) {
throw new Exception(e.toString());
}
if (logWriter != null) {
logWriter.close();
}
}
Aggregations