Search in sources :

Example 1 with MergeRequest

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

Aggregations

MergeRequest (com.salesforce.soap.partner.MergeRequest)1 MergeResult (com.salesforce.soap.partner.MergeResult)1 SaveResult (com.salesforce.soap.partner.SaveResult)1 ApiFault (com.salesforce.soap.partner.fault.ApiFault)1 SObject (com.salesforce.soap.partner.sobject.SObject)1 IOException (java.io.IOException)1 RemoteException (java.rmi.RemoteException)1 ServiceException (javax.xml.rpc.ServiceException)1 MessageElement (org.apache.axis.message.MessageElement)1