/* * 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<lcr.length; i++) { if (lcr[i].isSuccess()) { System.out.println("Conversion succeeded.\n"); LeadConvertResult result = lcr[i]; System.out.println("The new contact id is: " + result.getContactId()); } else { System.out.println("The conversion failed because: " + lcr[i].getErrors(0).getMessage()); } } } catch (UnexpectedErrorFault e) { System.out.println("Unexpected error encountered:\n\n" + e.getExceptionMessage()); return Boolean.FALSE; } catch (RemoteException e) { System.out.println("Remote exception encountered:\n\n" + e.getMessage()); return Boolean.FALSE; } return Boolean.TRUE;} private void bindLeadToCampaign(String leadId, String cmpId) { if (lr == null) { System.out.println("not logged in?"); System.exit(0); } // create campaign member CampaignMember member = new CampaignMember(); member.setCampaignId(cmpId); member.setLeadId(leadId); // create a variable to hold the query result QueryResult qr = null; // call the query saving the results in qr try { binding.create(new SObject[] {member}); } catch (UnexpectedErrorFault uef) { System.out.println(uef.getExceptionMessage() + "\n\n"); return; } catch (Exception e) { e.printStackTrace(); System.out.println("\n\n"); return; } System.out.println("Campaign added to lead!"); } private void doGetAccounts() { // check to see if we are already logged in if (lr == null) { System.out.println("Run the login sample before the others.\n"); return; } // create a variable to hold the query result QueryResult qr = null; // call the query saving the results in qr try { qr = binding .query("select Name, numberOfEmployees, Id, Industry from Account"); } catch (UnexpectedErrorFault uef) { System.out.println(uef.getExceptionMessage() + "\n\n"); return; } catch (Exception e) { e.printStackTrace(); System.out.println("\n\n"); return; } // always a good idea if (qr != null) { SObject[] records = qr.getRecords(); _accounts = new Account[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 Account account = (Account) records[i]; _accounts[i] = account; // 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() + ". "); System.out.print(account.getName() + " - "); System.out.println(account.getId()); } System.out.println(""); } } private void getOpps() { 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 Name, Id, Type, StageName from Opportunity"); } catch (UnexpectedErrorFault uef) { System.out.println(uef.getExceptionMessage() + "\n\n"); return; } catch (Exception e) { e.printStackTrace(); System.out.println("\n\n"); return; } // always a good idea if (qr != null) { SObject[] records = qr.getRecords(); Opportunity[] opps = new Opportunity[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 Opportunity opp = (Opportunity) records[i]; opps[i] = opp; // 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() + ". "); System.out.print(opp.getName() + " - "); System.out.println(opp.getId()); } System.out.println(""); } } private void getCmp() { String cmpId = "701300000003HWBAA2"; 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 Name, Id, Type, StageName from Opportunity"); } catch (UnexpectedErrorFault uef) { System.out.println(uef.getExceptionMessage() + "\n\n"); return; } catch (Exception e) { e.printStackTrace(); System.out.println("\n\n"); return; } // always a good idea if (qr != null) { SObject[] records = qr.getRecords(); Opportunity[] opps = new Opportunity[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 Opportunity opp = (Opportunity) records[i]; opps[i] = opp; // 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() + ". "); System.out.print(opp.getName() + " - "); System.out.println(opp.getId()); } System.out.println(""); } } private void updateAccount(String accountID, String newAccountName) { // First we will create an account object array, all calls are batch // ours is a single element batch SObject[] accounts = new Account[1]; // instantiate the new account object Account account = new Account(); Account oldAccount = _accounts[new Integer(accountID).intValue()]; // set the id, the id must always be set for an update account.setId(oldAccount.getId()); // set the new name of the account account.setName(newAccountName); // add the account to the array of objects accounts[0] = account; // we are now ready to update the record // create an array for the results SaveResult[] saveResults = null; try { saveResults = binding.update(accounts); } catch (UnexpectedErrorFault uef) { System.out.println(uef.getExceptionMessage() + "\n\n"); return; } catch (Exception e) { e.printStackTrace(); System.out.println("\n\n"); return; } if (saveResults == null) return; for (int i = 0; i < saveResults.length; i++) { // we will create a separate save result object for clarity SaveResult saveResult = saveResults[i]; // check to see if the first update was a success if (saveResult.isSuccess()) { // the id that we passed should be the one we get back. System.out.println("Update account with id: " + saveResult.getId()); } else { // an error occurred on this record Error error = saveResult.getErrors()[0]; System.out.println(error.getMessage()); } } } private void doLogin() { String userName = ""; String pwd = ""; try { SforceServiceLocator sf = new SforceServiceLocator(); sf.setSoapEndpointAddress("https://test.salesforce.com/services/Soap/c/9.0"); binding = (SoapBindingStub) sf.getSoap(); } catch (ServiceException ex1) { System.out.println(ex1.getMessage()); return; } try { lr = binding.login(userName, pwd); } catch (UnexpectedErrorFault ex2) { System.out.println(ex2.getExceptionMessage() + "\n\n"); return; } catch (LoginFault ex2) { System.out.println(ex2.getExceptionMessage() + "\n\n"); return; } catch (RemoteException ex2) { System.out.println(ex2.getMessage() + "\n\n"); return; } System.out.println("Login was successfull."); System.out.print("The returned session id is: "); System.out.println(lr.getSessionId()); System.out.print("Your logged in user id is: "); System.out.println(lr.getUserId() + " \n\n"); // on a successful login, you should always set up your session id // and the url for subsequent calls // reset the url endpoint property, this will cause subsequent calls // to made to the serverURL from the login result binding._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, lr .getServerUrl()); // create a session head object SessionHeader sh = new SessionHeader(); // set the sessionId property on the header object using // the value from the login result sh.setSessionId(lr.getSessionId()); // add the header to the binding stub String sforceURI = new SforceServiceLocator().getServiceName() .getNamespaceURI(); binding.setHeader(sforceURI, "SessionHeader", sh); return; }}