Search in sources :

Example 1 with SaveResult

use of com.salesforce.soap.partner.SaveResult 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...");
    }
}
Also used : MessageElement(org.apache.axis.message.MessageElement) SaveResult(com.salesforce.soap.partner.SaveResult) Date(java.util.Date) RemoteException(java.rmi.RemoteException) ServiceException(javax.xml.rpc.ServiceException) IOException(java.io.IOException) ApiFault(com.salesforce.soap.partner.fault.ApiFault) SObject(com.salesforce.soap.partner.sobject.SObject)

Example 2 with SaveResult

use of com.salesforce.soap.partner.SaveResult 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;
}
Also used : ApiFault(com.salesforce.soap.partner.fault.ApiFault) SObject(com.salesforce.soap.partner.sobject.SObject) MessageElement(org.apache.axis.message.MessageElement) SaveResult(com.salesforce.soap.partner.SaveResult) RemoteException(java.rmi.RemoteException) DeleteResult(com.salesforce.soap.partner.DeleteResult) RemoteException(java.rmi.RemoteException) ServiceException(javax.xml.rpc.ServiceException) IOException(java.io.IOException)

Example 3 with SaveResult

use of com.salesforce.soap.partner.SaveResult 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...");
    }
}
Also used : MessageElement(org.apache.axis.message.MessageElement) SaveResult(com.salesforce.soap.partner.SaveResult) RemoteException(java.rmi.RemoteException) ServiceException(javax.xml.rpc.ServiceException) IOException(java.io.IOException) ApiFault(com.salesforce.soap.partner.fault.ApiFault) SObject(com.salesforce.soap.partner.sobject.SObject)

Example 4 with SaveResult

use of com.salesforce.soap.partner.SaveResult 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;
}
Also used : HashMap(java.util.HashMap) Error(com.salesforce.soap.partner.Error) SaveResult(com.salesforce.soap.partner.SaveResult) DeleteResult(com.salesforce.soap.partner.DeleteResult) UpsertResult(com.salesforce.soap.partner.UpsertResult)

Example 5 with SaveResult

use of com.salesforce.soap.partner.SaveResult in project tdi-studio-se by Talend.

the class SforceManagementImpl method update.

/**
     * update, one record, one time.
     */
@Override
public SaveResult[] update(String tablename, String idStr, OMElement[] updatefields, String[] fieldsToNull) throws Exception {
    // create the account object to hold our changes
    SObject item = new SObject();
    item.setType(tablename);
    ID id = new ID();
    id.setID(idStr);
    // need to have the id so that API knows which account to update
    item.setId(id);
    // set a new value for the name property
    item.setExtraElement(updatefields);
    // set a null field for the name property
    item.setFieldsToNull(fieldsToNull);
    updateItems.add(item);
    // call the update passing an array of object
    if (updateItems.size() >= commitLevel) {
        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());
                }
            }
        }
        return saveResults;
    }
    return null;
}
Also used : DescribeSObject(com.salesforce.soap.partner.DescribeSObject) SObject(com.salesforce.soap.partner.sobject.SObject) SaveResult(com.salesforce.soap.partner.SaveResult) ID(com.salesforce.soap.partner.ID) Update(com.salesforce.soap.partner.Update)

Aggregations

SaveResult (com.salesforce.soap.partner.SaveResult)10 SObject (com.salesforce.soap.partner.sobject.SObject)9 ApiFault (com.salesforce.soap.partner.fault.ApiFault)6 IOException (java.io.IOException)6 RemoteException (java.rmi.RemoteException)6 ServiceException (javax.xml.rpc.ServiceException)6 MessageElement (org.apache.axis.message.MessageElement)6 DeleteResult (com.salesforce.soap.partner.DeleteResult)3 DescribeSObject (com.salesforce.soap.partner.DescribeSObject)3 Create (com.salesforce.soap.partner.Create)2 ID (com.salesforce.soap.partner.ID)2 Update (com.salesforce.soap.partner.Update)2 UpsertResult (com.salesforce.soap.partner.UpsertResult)2 Delete (com.salesforce.soap.partner.Delete)1 DeleteResponse (com.salesforce.soap.partner.DeleteResponse)1 Error (com.salesforce.soap.partner.Error)1 MergeRequest (com.salesforce.soap.partner.MergeRequest)1 MergeResult (com.salesforce.soap.partner.MergeResult)1 Upsert (com.salesforce.soap.partner.Upsert)1 Date (java.util.Date)1