Search in sources :

Example 6 with SaveResult

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

the class SforceManagementImpl method insert.

/**
     * create, one time one record.
     */
@Override
public SaveResult[] insert(String tablename, OMElement[] nameValues) throws Exception {
    if (tablename == null || tablename.trim().length() == 0) {
        return null;
    }
    if (nameValues == null || nameValues.length == 0) {
        return null;
    }
    SObject item = new SObject();
    item.setType(tablename);
    item.setExtraElement(nameValues);
    // item.setId("00T9000000VLEqBDAX");
    insertItems.add(item);
    if (insertItems.size() >= commitLevel) {
        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());
                }
            }
        }
        return sr;
    }
    return null;
}
Also used : Create(com.salesforce.soap.partner.Create) DescribeSObject(com.salesforce.soap.partner.DescribeSObject) SObject(com.salesforce.soap.partner.sobject.SObject) SaveResult(com.salesforce.soap.partner.SaveResult)

Example 7 with SaveResult

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

the class PartnerSamples method mergeSample.

private void mergeSample() {
    // call the login function to do so
    if (!loggedIn) {
        if (!login()) {
            return;
        }
    }
    try {
        SObject masterAccount = new SObject();
        MessageElement masterAccountName = newMessageElement("Name", "MasterAccount");
        masterAccount.set_any(new MessageElement[] { masterAccountName });
        masterAccount.setType("Account");
        SaveResult masterAccountSaveResult;
        masterAccountSaveResult = binding.create(new SObject[] { masterAccount })[0];
        masterAccount.setId(masterAccountSaveResult.getId());
        MessageElement masterAccountDescription = newMessageElement("Description", "Old description");
        masterAccount.set_any(new MessageElement[] { masterAccountName, masterAccountDescription });
        SObject accountToMerge = new SObject();
        accountToMerge.setType("Account");
        MessageElement mergeAccountName = newMessageElement("Name", "AccountToMerge");
        MessageElement mergeAccountDesc = newMessageElement("Description", "Duplicate account");
        accountToMerge.set_any(new MessageElement[] { mergeAccountDesc, mergeAccountName });
        SaveResult accountToMergeSaveResult = binding.create(new SObject[] { accountToMerge })[0];
        // Attach a note, which will get re-parented
        SObject note = new SObject();
        note.setType("Note");
        MessageElement noteParentId = newMessageElement("ParentId", accountToMergeSaveResult.getId());
        MessageElement noteBody = newMessageElement("Body", "This note will be moved to the MasterAccount during merge.");
        MessageElement noteTitle = newMessageElement("Title", "Test note to be reparented.");
        note.set_any(new MessageElement[] { noteBody, noteParentId, noteTitle });
        @SuppressWarnings("unused") SaveResult noteSave = binding.create(new SObject[] { note })[0];
        MergeRequest mr = new MergeRequest();
        // Perform an update on the master record as part of the merge:
        masterAccountDescription = newMessageElement("Description", "Was merged");
        masterAccount.set_any(new MessageElement[] { masterAccountName, masterAccountDescription });
        mr.setMasterRecord(masterAccount);
        mr.setRecordToMergeIds(new String[] { accountToMergeSaveResult.getId() });
        MergeResult result = binding.merge(new MergeRequest[] { mr })[0];
        getUserInput("Merged " + result.isSuccess() + " got " + result.getUpdatedRelatedIds().length + " updated child records\nHit return 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();
    }
}
Also used : MergeRequest(com.salesforce.soap.partner.MergeRequest) ApiFault(com.salesforce.soap.partner.fault.ApiFault) SObject(com.salesforce.soap.partner.sobject.SObject) MergeResult(com.salesforce.soap.partner.MergeResult) MessageElement(org.apache.axis.message.MessageElement) SaveResult(com.salesforce.soap.partner.SaveResult) RemoteException(java.rmi.RemoteException) RemoteException(java.rmi.RemoteException) ServiceException(javax.xml.rpc.ServiceException) IOException(java.io.IOException)

Example 8 with SaveResult

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

the class PartnerSamples method createContactSample.

private void createContactSample() {
    // call the login function to do so
    if (!loggedIn) {
        if (!login()) {
            return;
        }
    }
    try {
        SObject[] cons = new SObject[2];
        MessageElement[] contact;
        for (int j = 0; j < cons.length; j++) {
            int index = 0;
            if (accounts != null) {
                contact = new MessageElement[18];
                contact[index++] = newMessageElement("AccountId", accounts[0]);
            } else {
                contact = new MessageElement[17];
            }
            contact[index++] = newMessageElement("AssistantName", "Jane");
            contact[index++] = newMessageElement("AssistantPhone", "777.777.7777");
            contact[index++] = newMessageElement("Department", "Purchasing");
            contact[index++] = newMessageElement("Description", "International IT Purchaser");
            contact[index++] = newMessageElement("Email", "joeblow@isp.com");
            contact[index++] = newMessageElement("Fax", "555.555.5555");
            contact[index++] = newMessageElement("MailingCity", "San Mateo");
            contact[index++] = newMessageElement("MailingCountry", "US");
            contact[index++] = newMessageElement("MailingState", "CA");
            contact[index++] = newMessageElement("MailingStreet", "1129 B Street");
            contact[index++] = newMessageElement("MailingPostalCode", "94105");
            contact[index++] = newMessageElement("MobilePhone", "888.888.8888");
            contact[index++] = newMessageElement("FirstName", "Joe");
            contact[index++] = newMessageElement("LastName", "Blow");
            contact[index++] = newMessageElement("Salutation", "Mr.");
            contact[index++] = newMessageElement("Phone", "999.999.9999");
            contact[index++] = newMessageElement("Title", "Purchasing Director");
            cons[j] = new SObject();
            cons[j].setType("Contact");
            cons[j].set_any(contact);
        }
        SaveResult[] sr = binding.create(cons);
        for (int j = 0; j < sr.length; j++) {
            if (sr[j].isSuccess()) {
                System.out.println("A contact was create with an id of: " + sr[j].getId());
                if (accounts != null) {
                    System.out.println(" - the contact was associated with the account you created with an id of " + accounts[0] + ".");
                }
                if (contacts == null) {
                    contacts = new String[] { sr[j].getId() };
                } else {
                    String[] tempContacts = null;
                    tempContacts = new String[contacts.length + 1];
                    for (int i = 0; i < contacts.length; i++) {
                        tempContacts[i] = contacts[i];
                    }
                    tempContacts[contacts.length] = sr[j].getId();
                    contacts = tempContacts;
                }
            } 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 contacts, error message was: \n" + af.getExceptionMessage());
        getUserInput("\nHit return to continue...");
    } catch (Exception ex) {
        System.out.println("\nFailed to create contacts, 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 9 with SaveResult

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

the class PartnerSamples method updateAccountSample.

private void updateAccountSample() {
    // call the login function to do so
    if (!loggedIn) {
        if (!login()) {
            return;
        }
    }
    try {
        // check to see if there are any accounts created in this session
        if (accounts == null) {
            System.out.println("\nUpdate operation not completed.  You will need to create an account during this session to run the update sample.");
            getUserInput("\nHit return to continue...");
            return;
        }
        // create the account object to hold our changes
        SObject updateAccount = new SObject();
        updateAccount.setType("Account");
        // need to have the id so that web service knows which account to
        // update
        updateAccount.setId(accounts[0]);
        // set a new value for the name property
        MessageElement[] ufields = new MessageElement[1];
        ufields[0] = newMessageElement("Name", "New Account Name from Update Sample");
        updateAccount.set_any(ufields);
        // create one that will throw an error
        SObject errorAccount = new SObject();
        errorAccount.setType("Account");
        errorAccount.setId(new String("SLFKJLFKJ"));
        errorAccount.setFieldsToNull(new String[] { "Name" });
        // call the update passing an array of object
        SaveResult[] saveResults = binding.update(new SObject[] { updateAccount, errorAccount });
        // loop through the results, checking for errors
        for (int j = 0; j < saveResults.length; j++) {
            System.out.println("Item: " + new Integer(j).toString());
            if (saveResults[j].isSuccess()) {
                System.out.println("An account with an id of: " + saveResults[j].getId() + " was updated.\n");
            } else {
                System.out.println("Item " + new Integer(j).toString() + " had an error updating.");
                System.out.println("    The error reported was: " + saveResults[j].getErrors()[0].getMessage() + "\n");
            }
        }
    } catch (ApiFault af) {
        System.out.println("\nFailed to succesfully update an account, error message was: \n" + af.getExceptionMessage());
        getUserInput("\nHit return to continue...");
    } catch (Exception ex) {
        System.out.println("\nFailed to succesfully update an account, error message was: \n" + ex.getMessage());
    }
    getUserInput("\nHit return to continue...");
}
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) ServiceException(javax.xml.rpc.ServiceException) IOException(java.io.IOException)

Example 10 with SaveResult

use of com.salesforce.soap.partner.SaveResult 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();
    }
}
Also used : Delete(com.salesforce.soap.partner.Delete) Upsert(com.salesforce.soap.partner.Upsert) SaveResult(com.salesforce.soap.partner.SaveResult) OMElement(org.apache.axiom.om.OMElement) Update(com.salesforce.soap.partner.Update) DeleteResponse(com.salesforce.soap.partner.DeleteResponse) Create(com.salesforce.soap.partner.Create) DescribeSObject(com.salesforce.soap.partner.DescribeSObject) SObject(com.salesforce.soap.partner.sobject.SObject) DescribeSObject(com.salesforce.soap.partner.DescribeSObject) SObject(com.salesforce.soap.partner.sobject.SObject) ID(com.salesforce.soap.partner.ID) DeleteResult(com.salesforce.soap.partner.DeleteResult) UpsertResult(com.salesforce.soap.partner.UpsertResult)

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