/* * Created on Dec 6, 2005 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package sftest; import java.io.BufferedReader; import java.io.IOException; import com.sforce.soap.enterprise.sobject.*; import com.sforce.soap.enterprise.*; import com.sforce.soap.enterprise.Error; import javax.xml.rpc.*; import org.apache.log4j.Level; import org.apache.log4j.Logger; import java.rmi.*; import com.sforce.soap.enterprise.fault.*; /** * @author dcarroll * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class quickstart { // create a class scope reference to the service object private SoapBindingStub binding = null; private LoginResult lr = null; // maintain the login results private Account[] _accounts = null; static BufferedReader rdr = new BufferedReader( new java.io.InputStreamReader(System.in)); public static void main(String[] args) { Logger.getLogger("org.apache.axis").setLevel(Level.OFF); new quickstart(); } public quickstart() { try { Order order = new Order(); //mainLoop(); doLogin(); //getOpps(); //getCmp(); Lead lead = addOrFetchLead(order); convertLead(lead); //Product2 pr = findProductByCode(order.getSchedule().getProduct().getClassId()); //addProductToOpp(lead.getConvertedOpportunity(), pr, order); bindLeadToCampaign("00Q6000000DykeI","701300000003HWBAA2"); } catch (Exception e) { System.out.println(e.getMessage()); } } private void addProductToOpp(Opportunity opp, Product2 pr, Order o) { if (lr == null) { System.out.println("not logged in?"); return; } String pbId = opp.getPricebook2().getId(); String pbEntryId = getPricebookEntryIdFromPricebook(pr, pbId); OpportunityLineItem oli = new OpportunityLineItem(); oli.setOpportunity(opp); oli.setQuantity(1d); oli.setPricebookEntryId(pbEntryId); //oli.setServiceDate(o.getSchedule.getDate()); } private String getPricebookEntryIdFromPricebook(Product2 pr, String pricebookId) { if (lr == null) { System.out.println("not logged in?"); return null; } // create a variable to hold the query result QueryResult qr = null; // call the query saving the results in qr try { qr = binding .query("select Id from PricebookEntry where Product2Id = "+pr.getId()+" AND Pricebook2Id = "+pricebookId);; } catch (UnexpectedErrorFault uef) { System.out.println(uef.getExceptionMessage() + "\n\n"); return null; } catch (Exception e) { e.printStackTrace(); System.out.println("\n\n"); return null; } // always a good idea if (qr != null) { SObject[] records = qr.getRecords(); if (records.length > 1) { System.out.println("Got more than one PricebookEntry"); } else { return ((PricebookEntry)records[0]).getId(); } } } private Product2 findProductByCode(String code) { if (lr == null) { System.out.println("not logged in?"); System.exit(0); } // create a variable to hold the query result QueryResult qr = null; // call the query saving the results in qr try { qr = binding .query("select Id from Product where ProductCode = "+code); } catch (UnexpectedErrorFault uef) { System.out.println(uef.getExceptionMessage() + "\n\n"); return null; } catch (Exception e) { e.printStackTrace(); System.out.println("\n\n"); return null; } // always a good idea if (qr != null) { SObject[] records = qr.getRecords(); if (records.length > 0) { return (Product2)records[0]; } } } private Lead addOrFetchLead(Order o) { Lead lead; if (lr == null) { System.out.println("not logged in?"); System.exit(0); } QueryResult qr = null; try { qr = binding.query("Select Id from Lead WHERE email = "+o.getEmail()); } catch (UnexpectedErrorFault uef) { System.out.println(uef.getExceptionMessage() + "\n\n"); return null; } catch (Exception e) { e.printStackTrace(); System.out.println("\n\n"); return null; } // always a good idea if (qr != null) { SObject[] records = qr.getRecords(); if (records.length > 1) { System.out.println("More than one lead for this email address??"); } Lead[] leads = new Lead[records.length]; // we can loop through the returned records for (int i = 0; i < records.length; i++) { // Because we asked for accounts we will convert // the SObject for each record into an Account object Lead t = (Lead) records[i]; leads[i] = t; // Now we can access any of the fields we had in the query // select clause directly from the account variable System.out.print(new Integer(i).toString() + ". "); } if (leads.length > 0) { lead = leads[0]; } System.out.println(""); } if (lead == null) { lead = new Lead(); } // update information in lead if (lead.getFirstName() == null) { lead.setFirstName(o.getFirstname()); } if (lead.getLastName() == null) { lead.setLastName(o.getLastname()); } if (lead.getEmail() == null) { lead.setEmail(o.getEmail()); } if (lead.getPhone() == null) { // fuck } if (lead.getStreet() == null) { lead.setStreet(o.getAddress1() + (o.getAddress2() != null ? ", " + o.getAddress2() : "")); } if (lead.getState() == null) { lead.setState(o.getState()); } if (lead.getCountry() == null) { lead.setCountry(o.getCountry()); } if (lead.getPostalCode() == null) { lead.setPostalCode(o.getZip()); } SaveResult[] sr = null; // save or update if (lead.getId() == null) { sr = binding.create(new SObject[] {lead}); } else { sr = binding.update(new SObject[] {lead}); } if (sr != null && sr.length == 1 && sr[0].isSuccess()) { lead.setId(sr[0].getId()); } return lead; } private Boolean convertLead (String leadId, String contactId, String accountId, boolean overWriteLeadSource, boolean doNotCreateOpportunity, String opportunityName, String convertedStatus, boolean sendEmailToOwner) { LeadConvert leadConvert = new LeadConvert(); leadConvert.setLeadId(leadId); leadConvert.setContactId(contactId); leadConvert.setAccountId(accountId); leadConvert.setOverwriteLeadSource(overWriteLeadSource); leadConvert.setDoNotCreateOpportunity(doNotCreateOpportunity); leadConvert.setOpportunityName(opportunityName); leadConvert.setConvertedStatus(convertedStatus); leadConvert.setSendNotificationEmail(sendEmailToOwner); LeadConvertResult[] lcr = null; try { lcr = binding.convertLead(new LeadConvert[] {leadConvert}); for (int i=0; i