use of org.apache.axis.message.MessageElement 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 org.apache.axis.message.MessageElement in project tdi-studio-se by Talend.
the class PartnerSamples method newMessageElement.
private MessageElement newMessageElement(String name, Object value) throws Exception {
// , value);
MessageElement me = new MessageElement("", name);
me.setObjectValue(value);
Element e = me.getAsDOM();
e.removeAttribute("xsi:type");
e.removeAttribute("xmlns:ns1");
e.removeAttribute("xmlns:xsd");
e.removeAttribute("xmlns:xsi");
me = new MessageElement(e);
return me;
}
use of org.apache.axis.message.MessageElement in project tdi-studio-se by Talend.
the class PartnerSamples method createTaskSample.
private void createTaskSample() {
// call the login function to do so
if (!loggedIn) {
if (!login()) {
return;
}
}
try {
// create an array to create 4 items in one call
SObject[] taskarray = new SObject[4];
for (int x = 0; x < 4; x++) {
// Declare a new task object to hold our values
final java.text.SimpleDateFormat ISO8601UTC = new java.text.SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss+SSSS");
// Set the appropriate values on the task
SObject task = new SObject();
task.setType("Task");
Date activityDate = new Date();
int taskUbound = 5;
int index = 0;
if (contacts != null)
taskUbound++;
if (accounts != null)
taskUbound++;
if (x > 1)
taskUbound++;
MessageElement[] fields = new MessageElement[taskUbound];
fields[index++] = newMessageElement("ActivityDate", ISO8601UTC.format(activityDate));
fields[index++] = newMessageElement("Description", "Get in touch with this person");
fields[index++] = newMessageElement("Priority", "Normal");
fields[index++] = newMessageElement("Status", "Not Started");
fields[index++] = newMessageElement("Subject", "Setup Call");
// make sure we get some errors on records 3 and 4
if (x > 1) {
fields[index++] = newMessageElement("OwnerId", "DSF:LJKSDFLKJ");
// The two lines below illustrate associating an object with
// another object. If
// we have created an account and/or a contact prior to
// creating the task, we will
// just grab the first account and/or contact id and place
// it in the appropriate
// reference field. WhoId can be a reference to a contact or
// a lead or a user.
// WhatId can be a reference to an account, campaign, case,
// contract or opportunity
}
if (contacts != null) {
fields[index++] = newMessageElement("WhoId", contacts[0]);
}
if (accounts != null) {
fields[index++] = newMessageElement("WhatId", accounts[0]);
}
taskarray[x] = task;
}
// call the create method passing the array of tasks as sobjects
SaveResult[] sr = binding.create(taskarray);
for (int j = 0; j < sr.length; j++) {
if (sr[j].isSuccess()) {
System.out.println("A task was create with an id of: " + sr[j].getId());
if (accounts != null) {
System.out.println(" - the task was associated with the account you created with an id of " + accounts[0] + ".");
}
if (tasks == null) {
tasks = new String[] { sr[j].getId() };
} else {
String[] tempTasks = null;
tempTasks = new String[tasks.length + 1];
for (int i = 0; i < tasks.length; i++) {
tempTasks[i] = tasks[i];
}
tempTasks[tasks.length] = sr[j].getId();
tasks = tempTasks;
}
} else {
// array and write them to the screen
for (int i = 0; i < sr[j].getErrors().length; i++) {
// get the next error
com.salesforce.soap.partner.Error err = sr[j].getErrors()[i];
System.out.println("Errors were found on item " + new Integer(j).toString());
System.out.println("Error code is: " + err.getStatusCode().toString());
System.out.println("Error message: " + err.getMessage());
}
}
}
getUserInput("\nHit return to continue...");
} catch (ApiFault af) {
System.out.println("\nFailed to succesfully create a task, error message was: \n" + af.getExceptionMessage());
getUserInput("\nHit return to continue...");
} catch (Exception ex) {
System.out.println("\nFailed to succesfully create a task, error message was: \n" + ex.getMessage());
getUserInput("\nHit return to continue...");
}
}
use of org.apache.axis.message.MessageElement in project tdi-studio-se by Talend.
the class PartnerSamples method createAndDeleteAnAccount.
private String createAndDeleteAnAccount(String accountName) {
String returnId = null;
SObject acct = new SObject();
acct.setType("Account");
String acctName = (accountName == null) ? "QueryAll Sample" : accountName;
try {
MessageElement nameField = newMessageElement("Name", acctName);
acct.set_any(new MessageElement[] { nameField });
// We are only creating one account so we can index the return array directly
SaveResult sr = binding.create(new SObject[] { acct })[0];
if (sr.isSuccess()) {
acct.setId(sr.getId());
// Ok, now we will delete that account
DeleteResult dr = binding.delete(new String[] { acct.getId() })[0];
if (!dr.isSuccess()) {
System.out.println("The web service would not let us delete the account: \n" + dr.getErrors(0).getMessage());
getUserInput("\nHit return to continue...");
} else {
returnId = acct.getId();
}
} else {
System.out.println("The web service would not let us create the account: \n" + sr.getErrors(0).getMessage());
getUserInput("\nHit return to continue...");
}
} catch (ApiFault e) {
System.out.println("Error creating test account: " + e.getExceptionMessage());
getUserInput("\nHit return to continue...");
} catch (RemoteException e) {
System.out.println("Error from the server on create test account: " + e.getMessage());
getUserInput("\nHit return to continue...");
} catch (Exception e) {
e.printStackTrace();
}
return returnId;
}
use of org.apache.axis.message.MessageElement in project tdi-studio-se by Talend.
the class PartnerSamples method createAccountSample.
private void createAccountSample() {
// call the login function to do so
if (!loggedIn) {
if (!login()) {
return;
}
}
try {
SObject[] accs = new SObject[2];
for (int j = 0; j < accs.length; j++) {
MessageElement[] account = new MessageElement[14];
int index = 0;
if (accounts == null) {
account[index++] = newMessageElement("AccountNumber", "00000000");
} else {
account[index++] = newMessageElement("AccountNumber", "000000" + (accounts.length + 1));
}
account[index++] = newMessageElement("BillingCity", "Wichita");
account[index++] = newMessageElement("BillingCountry", "US");
account[index++] = newMessageElement("BillingState", "KA");
account[index++] = newMessageElement("BillingStreet", "4322 Haystack Boulevard");
account[index++] = newMessageElement("BillingPostalCode", "87901");
account[index++] = newMessageElement("Description", "World class hay makers.");
account[index++] = newMessageElement("Fax", "555.555.5555");
account[index++] = newMessageElement("Industry", "Farming");
account[index++] = newMessageElement("Name", "Golden Straw");
account[index++] = newMessageElement("NumberOfEmployees", new java.lang.Integer(40).toString());
account[index++] = newMessageElement("Ownership", "Privately Held");
account[index++] = newMessageElement("Phone", "666.666.6666");
account[index++] = newMessageElement("Website", "www.oz.com");
accs[j] = new SObject();
accs[j].set_any(account);
accs[j].setType("Account");
accs[j + 1] = accs[j];
j++;
}
// create the object(s) by sending the array to the web service
SaveResult[] sr = binding.create(accs);
for (int j = 0; j < sr.length; j++) {
if (sr[j].isSuccess()) {
System.out.println("An account was create with an id of: " + sr[j].getId());
// save the account ids in a class array
if (accounts == null) {
accounts = new String[] { sr[j].getId() };
} else {
String[] tempAccounts = null;
tempAccounts = new String[accounts.length + 1];
for (int i = 0; i < accounts.length; i++) {
tempAccounts[i] = accounts[i];
}
tempAccounts[accounts.length] = sr[j].getId();
accounts = tempAccounts;
}
} else {
// array and write them to the screen
for (int i = 0; i < sr[j].getErrors().length; i++) {
// get the next error
com.salesforce.soap.partner.Error err = sr[j].getErrors()[i];
System.out.println("Errors were found on item " + new Integer(j).toString());
System.out.println("Error code is: " + err.getStatusCode().toString());
System.out.println("Error message: " + err.getMessage());
}
}
}
getUserInput("\nHit return to continue...");
} catch (ApiFault af) {
System.out.println("\nFailed to create account, error message was: \n" + af.getExceptionMessage());
getUserInput("\nHit return to continue...");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("\nFailed to create account, error message was: \n" + ex.getMessage());
getUserInput("\nHit return to continue...");
}
}
Aggregations