Search in sources :

Example 1 with DeleteResult

use of com.salesforce.soap.partner.DeleteResult 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 2 with DeleteResult

use of com.salesforce.soap.partner.DeleteResult 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 3 with DeleteResult

use of com.salesforce.soap.partner.DeleteResult 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)

Example 4 with DeleteResult

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

the class SforceManagementImpl method delete.

/**
     * delete, one time one record.
     */
@Override
public DeleteResult[] delete(String id) throws Exception {
    if (id == null) {
        return null;
    }
    // String[] ids = new String[] { id };
    ID dID = new ID();
    dID.setID(id);
    deleteItems.add(dID);
    if (deleteItems.size() >= commitLevel) {
        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());
                }
            }
        }
        return dr;
    }
    return null;
}
Also used : Delete(com.salesforce.soap.partner.Delete) DeleteResponse(com.salesforce.soap.partner.DeleteResponse) ID(com.salesforce.soap.partner.ID) DeleteResult(com.salesforce.soap.partner.DeleteResult)

Aggregations

DeleteResult (com.salesforce.soap.partner.DeleteResult)4 SaveResult (com.salesforce.soap.partner.SaveResult)3 Delete (com.salesforce.soap.partner.Delete)2 DeleteResponse (com.salesforce.soap.partner.DeleteResponse)2 ID (com.salesforce.soap.partner.ID)2 UpsertResult (com.salesforce.soap.partner.UpsertResult)2 SObject (com.salesforce.soap.partner.sobject.SObject)2 Create (com.salesforce.soap.partner.Create)1 DescribeSObject (com.salesforce.soap.partner.DescribeSObject)1 Error (com.salesforce.soap.partner.Error)1 Update (com.salesforce.soap.partner.Update)1 Upsert (com.salesforce.soap.partner.Upsert)1 ApiFault (com.salesforce.soap.partner.fault.ApiFault)1 IOException (java.io.IOException)1 RemoteException (java.rmi.RemoteException)1 HashMap (java.util.HashMap)1 ServiceException (javax.xml.rpc.ServiceException)1 OMElement (org.apache.axiom.om.OMElement)1 MessageElement (org.apache.axis.message.MessageElement)1