Search in sources :

Example 6 with SObject

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

use of com.salesforce.soap.partner.sobject.SObject 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 8 with SObject

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

Example 9 with SObject

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

the class PartnerSamples method getContact.

private SObject getContact() throws Exception {
    SObject contactToReturn = null;
    // get a list of contacts so the user can select one
    QueryResult qr = binding.query("Select Id, FirstName, LastName, AccountId from Contact Where not AccountId = null");
    if (qr.getSize() == 0) {
        // No leads where found that have not been
        // converted, so....
        // we will create a lead and then run the query again
        System.out.println("No contacts found, will create one for you...");
        this.createContactSample();
        qr = binding.query("Select Id, FirstName, LastName, AccountId from Contact");
    }
    if (qr.getSize() > 0) {
        for (int i = 0; i < qr.getRecords().length; i++) {
            SObject contact = qr.getRecords(i);
            System.out.println(new Integer(i + 1).toString() + ": " + getFieldValue(contact.get_any(), "FirstName") + " " + getFieldValue(contact.get_any(), "LastName"));
        }
        String selectedContact = getUserInput("/nSelect the number of the contact to use. ");
        if (selectedContact != null) {
            try {
                int selContact = new Integer(selectedContact).intValue();
                contactToReturn = qr.getRecords(selContact - 1);
            } catch (Exception ex) {
                System.out.println("The number you selected is not valid, exiting...");
            }
        }
    }
    if (contactToReturn == null) {
        throw new Exception("No contact was selected, conversion failed.");
    } else {
        return contactToReturn;
    }
}
Also used : QueryResult(com.salesforce.soap.partner.QueryResult) SObject(com.salesforce.soap.partner.sobject.SObject) RemoteException(java.rmi.RemoteException) ServiceException(javax.xml.rpc.ServiceException) IOException(java.io.IOException)

Example 10 with SObject

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

the class PartnerSamples method setPasswordSample.

private void setPasswordSample() {
    if (!loggedIn) {
        if (!login()) {
            return;
        }
    }
    try {
        QueryResult qr = binding.query("select UserName, LastName, FirstName, Id from User");
        if (qr.getSize() > 0) {
            SObject[] users = qr.getRecords();
            if (users != null) {
                System.out.println("\nUser List: ");
                for (int i = 0; i < users.length; i++) {
                    int printInd = i + 1;
                    SObject user = users[i];
                    System.out.println(printInd + ". " + user.get_any()[0].getValue() + " - " + user.get_any()[2].getValue() + " " + user.get_any()[1].getValue());
                }
            }
            String idToReset = getUserInput("\nEnter user to set password for: ");
            if (idToReset != null) {
                int userIndex = new Integer(idToReset).intValue() - 1;
                String newPassword = getUserInput("Enter the new password: ");
                if (newPassword != null) {
                    String verify = getUserInput("Please verify that you want to reset the password for \n" + users[userIndex].get_any()[2] + " " + users[userIndex].get_any()[1] + "\n to " + newPassword + " by entering OK.");
                    if (verify.equals("OK")) {
                        SetPasswordResult setPasswordResult = binding.setPassword(users[userIndex].getId(), newPassword);
                        if (setPasswordResult != null) {
                            System.out.println("\nThe password for user " + users[userIndex].get_any()[2].getValue() + " " + users[userIndex].get_any()[1].getValue() + " has been reset to " + newPassword);
                            getUserInput("\nHit enter to continue...");
                            return;
                        }
                    }
                }
            }
        }
    } catch (ApiFault af) {
        System.out.println("\nFailed to succesfully set password, error message was: \n" + af.getExceptionMessage());
        getUserInput("\nHit return to continue...");
    } catch (RemoteException ex) {
        System.out.println("\nFailed to succesfully set password, error message was: \n" + ex.getMessage());
        getUserInput("\nHit return to continue...");
    }
    getUserInput("No password was set....\nHit return to continue...");
}
Also used : QueryResult(com.salesforce.soap.partner.QueryResult) ApiFault(com.salesforce.soap.partner.fault.ApiFault) SObject(com.salesforce.soap.partner.sobject.SObject) SetPasswordResult(com.salesforce.soap.partner.SetPasswordResult) RemoteException(java.rmi.RemoteException)

Aggregations

SObject (com.salesforce.soap.partner.sobject.SObject)25 RemoteException (java.rmi.RemoteException)17 IOException (java.io.IOException)15 ServiceException (javax.xml.rpc.ServiceException)15 ApiFault (com.salesforce.soap.partner.fault.ApiFault)14 QueryResult (com.salesforce.soap.partner.QueryResult)10 SaveResult (com.salesforce.soap.partner.SaveResult)9 MessageElement (org.apache.axis.message.MessageElement)7 DescribeSObject (com.salesforce.soap.partner.DescribeSObject)5 UpsertResult (com.salesforce.soap.partner.UpsertResult)3 OMElement (org.apache.axiom.om.OMElement)3 Create (com.salesforce.soap.partner.Create)2 DeleteResult (com.salesforce.soap.partner.DeleteResult)2 ID (com.salesforce.soap.partner.ID)2 Update (com.salesforce.soap.partner.Update)2 Upsert (com.salesforce.soap.partner.Upsert)2 HashMap (java.util.HashMap)2 Delete (com.salesforce.soap.partner.Delete)1 DeleteResponse (com.salesforce.soap.partner.DeleteResponse)1 DescribeSObjectResult (com.salesforce.soap.partner.DescribeSObjectResult)1