Search in sources :

Example 1 with LeadConvert

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

the class PartnerSamples method convertLeadSample.

private void convertLeadSample() {
    // call the login function to do so
    if (!loggedIn) {
        if (!login())
            return;
    }
    try {
        // This is the structure we need to fill in to effect a conversion
        LeadConvert lc = new LeadConvert();
        String summary = "Converting lead to contact.\n";
        // When converting a lead you have the option of creating a new
        // contact, or
        // merging the lead with an existing contact. This sample will show
        // both
        // cases. So the first thing to do, is get the lead to convert.
        // Step 1 - Determine the String of the lead to conver
        SObject lead;
        lead = this.getUnconvertedLead();
        // Set the lead id on the structure.
        String lid = new String();
        lid = lead.getId().substring(0, 15);
        lc.setLeadId(lid);
        // Step2 - determine if the user wants to merge the lead to an
        // existing contact,
        // or have a new contact created.
        String merge = getUserInput("\nDo you want to merge this lead into an existing contact (Y/N)? ");
        SObject contact = null;
        if (merge != null && merge.toLowerCase().equals("y")) {
            contact = getContact();
            if (contact == null) {
                summary += "  Create new contact.\n";
            } else {
                summary += "  Merge with " + getFieldValue(contact.get_any(), "FirstName") + " " + getFieldValue(contact.get_any(), "LastName") + "\n";
                String cid = new String();
                cid = contact.getId().substring(0, 15);
                lc.setContactId(cid);
                // We have the option of resetting the contact status to
                // that of the lead,
                // so we will query the user for that information.
                String overWrite = getUserInput("Do you want to overwrite the contact status with the lead status (Y/N)? ");
                if (overWrite != null && overWrite.toLowerCase().equals("y"))
                    lc.setOverwriteLeadSource(true);
                else
                    lc.setOverwriteLeadSource(false);
            }
        } else
            summary += "  Create new contact.\n";
        // Step 3 - Determine account id, if a contact is chosen, we will
        // use the accountid of the contact, this must
        // be specified. If the contact is not chosen, then the user needs
        // to select
        // an account to place the new contact in.
        String accountId = null;
        if (contact != null) {
            accountId = getFieldValue(contact.get_any(), "AccountId");
            String aid = new String();
            aid = accountId.substring(0, 15);
            lc.setAccountId(aid);
        } else // get an account chosen by the user
        {
            String newAccount = getUserInput("Do you want to create a new account for this lead (Y/N)? ");
            if (newAccount == null || !newAccount.toLowerCase().equals("y"))
                accountId = this.getAccount().getId().substring(0, 15);
            if (accountId != null) {
                String aid = new String();
                aid = accountId;
                lc.setAccountId(aid);
                String accountName;
                accountName = getAccountName(accountId);
                if (accountName != null)
                    summary += "  New contact will be in account '" + accountName + "'.\n";
                else
                    summary += "  New contact will be in account with an String of '" + accountId + "'.\n";
            } else {
                summary += "  A new account will be created.";
            }
        }
        // Step 4 - Determine if opportunity is to be created.
        // We will now ask the user if they would like to create a new
        // opportunity
        // based on the information on this lead
        String opp = getUserInput("Do you want to create an opportunity for this conversion (Y/N)? ");
        lc.setDoNotCreateOpportunity(true);
        if (opp != null && opp.toLowerCase().equals("y")) {
            lc.setDoNotCreateOpportunity(false);
            String oppName = getUserInput("Enter the name of the opportunity to create.. ");
            if (oppName == null) {
                System.out.println("No opportunity name given, NO opportunity will be created.");
                lc.setDoNotCreateOpportunity(true);
                summary += "  No opportunity will be created.\n";
            } else {
                lc.setOpportunityName(oppName);
                summary += "  An opportunity named: " + oppName + " will be created.\n";
            }
        }
        // Step 5 - Obtain the proper lead status to place on the converted
        // lead
        // The lead needs to have it's status updated to reflect the
        // conversion operation,
        // so we will ask the user to select the status to use
        System.out.println("Select the lead status to use to update the converted lead. ");
        lc.setConvertedStatus(getLeadStatus());
        summary += "  The converted lead will be assigned a status of " + lc.getConvertedStatus() + ".\n";
        // Step 6 - Finally, we have the option of notifying the owner of
        // the lead that it
        // has been converted.
        String sendMail = getUserInput("Would you like to have an email sent to the owner of the lead after the conversion (Y/N)?");
        if (sendMail != null && sendMail.toLowerCase().equals("y")) {
            lc.setSendNotificationEmail(true);
            summary += "  Email notification will be sent.\n";
        } else {
            lc.setSendNotificationEmail(false);
            summary += "  Email notificatino will NOT be sent.\n";
        }
        String cont;
        System.out.println("\n\nDEBUG VALUES\n");
        System.out.println("account id: " + (lc.getAccountId() == null ? "(create new)" : lc.getAccountId()));
        System.out.println("contact id: " + (lc.getContactId() == null ? "(create new)" : lc.getContactId()));
        System.out.println("converted status: " + lc.getConvertedStatus());
        System.out.println("do not create opp: " + new Boolean(lc.isDoNotCreateOpportunity()).toString());
        System.out.println("lead id: " + lc.getLeadId());
        System.out.println("opp name: " + (lc.getOpportunityName() == null ? "(no opp)" : lc.getOpportunityName()));
        System.out.println("overwrite lead source: " + new Boolean(lc.isOverwriteLeadSource()).toString());
        System.out.println("send email: " + new Boolean(lc.isSendNotificationEmail()).toString());
        // Get a final verification of lead conversion from the user.
        cont = getUserInput(summary + "\n Enter 'Y' to convert the lead.");
        if (cont != null && cont.toLowerCase().equals("y")) {
            LeadConvertResult[] lcr = null;
            lcr = binding.convertLead(new LeadConvert[] { lc });
            // LeadConvertResults
            for (int i = 0; i < lcr.length; i++) {
                if (lcr[i].isSuccess()) {
                    try {
                        System.out.println("Conversion of lead " + getFieldValue(lead.get_any(), "FirstName") + " " + getFieldValue(lead.get_any(), "LastName") + " was successful.");
                        if (contact != null && contact.getId().equals(lcr[i].getContactId()))
                            System.out.println("  Contact with String of '" + lcr[i].getContactId() + "' was merged.");
                        else
                            System.out.println("  Contact with String of '" + lcr[i].getContactId() + "' was created.");
                        if (lcr[i].getOpportunityId() != null)
                            System.out.println("  An opportunity with an String of '" + lcr[i].getOpportunityId() + "' was created.");
                        else
                            System.out.println("  No opportunity was created.");
                        System.out.println("  The contact was created in the account with an String of '" + lcr[i].getAccountId() + "'.");
                    } catch (Exception e) {
                        getUserInput("Although the lead was converted, some error prevented the reporting of results.");
                    }
                } else {
                    System.out.println("One or more errors where encountered during the lead conversion process...\n");
                    for (int j = 0; j < lcr[i].getErrors().length; j++) {
                        System.out.println((j + 1) + ". " + lcr[0].getErrors(j).getMessage());
                    }
                }
            }
        } else {
            System.out.println("Lead conversion was aborted.");
        }
    } catch (ApiFault af) {
        System.out.println("Errors occurred during conversion preventing any conversion to take place.\n\n" + af.getExceptionMessage());
        getUserInput("\nHit return to continue...");
    } catch (Exception e) {
        System.out.println("Errors occurred during conversion preventing any conversion to take place.\n\n" + e.getMessage());
    }
}
Also used : ApiFault(com.salesforce.soap.partner.fault.ApiFault) LeadConvert(com.salesforce.soap.partner.LeadConvert) SObject(com.salesforce.soap.partner.sobject.SObject) LeadConvertResult(com.salesforce.soap.partner.LeadConvertResult) RemoteException(java.rmi.RemoteException) ServiceException(javax.xml.rpc.ServiceException) IOException(java.io.IOException)

Aggregations

LeadConvert (com.salesforce.soap.partner.LeadConvert)1 LeadConvertResult (com.salesforce.soap.partner.LeadConvertResult)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