/* * 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 boolean notify(Order order) /* throws Exception */ { try { doLogin(); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); throw new Exception("Couldn't log in"); } Lead lead = null; try { lead = addOrFetchLead(order); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); throw new Exception("Unable to get lead"); } if (lead.getIsConverted() == null || !lead.getIsConverted()) { // convert, but don't create opp try { convertLead(lead); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); throw new Exception("Could not convert lead"); } } Product2 pr = findProductByCode(Integer.toString(order.getSchedule().getProduct().getClassId())); // create new opp String oppName = order.getFirstname(); if (order.getLastname() != null) { oppName += " "+order.getLastname(); } oppName += " - "+order.getSchedule().getProduct().getTitle(); Opportunity opp = new Opportunity(); opp.setAccountId(lead.getConvertedAccountId()); opp.setName(oppName); opp.setStageName("Pending"); opp.setCloseDate(new java.util.Date()); opp.setCurrencyIsoCode("USD"); // save it SaveResult[] sr = binding.create(new SObject[] {opp}); if (sr.length > 0 && sr[0].isSuccess()) { SObject[] so = binding.retrieve("Id, Pricebook2Id", "Opportunity", new String[] {sr[0].getId()}); if (so.length > 0) { opp = (Opportunity) so[0]; } } addProductToOpp(opp, pr, order); } public quickstart() { try { Order order = new Order(); order.setAddress1("334 E 79th st"); order.setAddress2("Apt 4b"); order.setCity("New York"); order.setState("NY"); order.setAmount(19.99d); order.setComplete(true); order.setCountry("USA"); order.setEmail("gharding2@gmail.com"); order.setFirstname("Grant"); order.setLastname("Harding"); order.setOrderTimestamp(new java.util.Date()); order.setZip("10021"); notify(order); } 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.getPricebook2Id(); String pbEntryId = getPricebookEntryIdFromPricebook(pr, pbId); OpportunityLineItem oli = new OpportunityLineItem(); oli.setOpportunityId(opp.getId()); oli.setQuantity(1d); oli.setPricebookEntryId(pbEntryId); oli.setServiceDate(o.getSchedule().getDate()); oli.setTotalPrice(o.getSchedule().getProduct().getPrice()); oli.setTransaction_ID__c(o.getOrderstring()); //oli.setCurrencyIsoCode("USD"); try { SaveResult[] sr = binding.create(new SObject[] {oli}); if (sr.length > 0) { } } catch (Exception e) { e.printStackTrace(); } } 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,UseStandardPrice from PricebookEntry where Product2Id = '"+pr.getId()+"'"); } 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(); } } return null; } private Product2 findProductByCode(String code) throws Exception { 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 Product2 where ProductCode = '"+code+"'"); } catch (UnexpectedErrorFault uef) { System.out.println(uef.getExceptionMessage() + "\n\n"); uef.printStackTrace(); throw uef; } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); throw e; } if (qr == null || qr.getRecords().length == 0) { throw new Exception("No matching products"); } // always a good idea if (qr != null) { SObject[] records = qr.getRecords(); if (records.length > 0) { return (Product2)records[0]; } } return null; } private Lead addOrFetchLead(Order o) throws Exception { Lead lead = null; if (lr == null) { System.out.println("not logged in?"); System.exit(0); } QueryResult qr = null; try { qr = binding.query("Select Id,IsConverted,ConvertedAccountId,ConvertedContactId,IsDeleted from Lead WHERE email = '"+o.getEmail()+"' AND IsDeleted = false"); } catch (UnexpectedErrorFault uef) { System.out.println(uef.getExceptionMessage() + "\n\n"); throw uef; } catch (Exception e) { e.printStackTrace(); throw e; } // always a good idea if (qr != null && qr.getSize() > 0) { 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++) { Lead t = (Lead) records[i]; leads[i] = t; break; } if (leads.length > 0) { lead = leads[0]; } } 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()); } lead.setCompany(o.getFirstname() + " " + o.getLastname()); SaveResult[] sr = null; // save or update try { if (lead.getId() == null) { sr = binding.create(new SObject[] {lead}); } else { sr = binding.update(new SObject[] {lead}); } } catch (Exception e) { e.printStackTrace(); throw new Exception("Unable to save lead"); } if (sr != null && sr.length == 1 && sr[0].isSuccess()) { lead.setId(sr[0].getId()); } return lead; } private String[] getConvertedStatuses() { String q = "Select Id, MasterLabel from LeadStatus where IsConverted=true"; if (lr == null) return null; QueryResult qr = null; try { qr = binding.query(q); } catch (UnexpectedErrorFault uef) { System.out.println(uef.getExceptionMessage() + "\n\n"); return null; } catch (Exception e) { e.printStackTrace(); System.out.println("\n\n"); return null; } if (qr != null && qr.getSize() > 0) { SObject[] records = qr.getRecords(); String[] str = new String[records.length]; //LeadStatus[] leads = new LeadStatus[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 LeadStatus t = (LeadStatus) records[i]; str[i] = t.getMasterLabel(); // Now we can access any of the fields we had in the query // select clause directly from the account variable } return str; } return null; } private Boolean convertLead(Lead lead) { return convertLead(lead, null, null, false, true, null, null, false); } private Boolean convertLead(Lead lead, String contactId, String accountId, boolean overWriteLeadSource, boolean doNotCreateOpportunity, String opportunityName, String convertedStatus, boolean sendEmailToOwner) { String leadId = lead.getId(); getConvertedStatuses(); LeadConvert leadConvert = new LeadConvert(); leadConvert.setLeadId(leadId); leadConvert.setContactId(contactId); leadConvert.setAccountId(accountId); leadConvert.setOverwriteLeadSource(overWriteLeadSource); leadConvert.setDoNotCreateOpportunity(doNotCreateOpportunity); leadConvert.setOpportunityName(opportunityName); leadConvert.setConvertedStatus("Converted Lead"); leadConvert.setSendNotificationEmail(sendEmailToOwner); LeadConvertResult[] lcr = null; try { lcr = binding.convertLead(new LeadConvert[] {leadConvert}); for (int i=0; i