Salesforce API Delete Account Record Send Email

package com.thysmichels;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.DeleteResult;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.Error;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.SaveResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.Contact;

public class SalesforceDeleteObject {

	 * @param args
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ConnectorConfig sfconfig = new ConnectorConfig();
		//Use your salesforce username = email
	    //Use your saleforce password with your security token look like: passwordjeIzBAQKkR6FBW8bw5HbVkkkk
		final String username = "emailusername";
		final String password = "emailpassword";
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("", "");
		props.put("mail.smtp.port", "587");
		Session session = Session.getInstance(props,
		  new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
	    EnterpriseConnection entercon = null;
	    String[] ids = new String[3];
	    try {
	      String strconcat = "";
	      entercon = Connector.newConnection(sfconfig);
	      QueryResult queryResults = entercon.query("SELECT Id, Name FROM Account ORDER BY " + "CreatedDate DESC LIMIT 3");
	      if (queryResults.getSize() > 0) {
	        for (int i=0;i<queryResults.getRecords().length;i++) {
	          Account a = (Account)queryResults.getRecords()[i];
	          ids[i] = a.getId();
	          strconcat += a.getId() + ", ";
	      // delete the records in by passing an array of Ids
	      DeleteResult[] deleteResults = entercon.delete(ids);
	      Message message = new MimeMessage(session);
	      message.setFrom(new InternetAddress(""));
	      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(""));
	      message.setSubject("Salesforce Record Delete");
	      message.setText("Salesforce Record Delete," + strconcat);
	      // check the results for any errors
	      for (int i=0; i< deleteResults.length; i++) {
	        if (deleteResults[i].isSuccess()) {
	        	System.out.println("Successfully deleted : " + deleteResults[i]);
	        } else {
	          Error[] errors = deleteResults[i].getErrors();
	          for (int j=0; j< errors.length; j++) {
	            System.out.println("ERROR deleting record: " + errors[j].getMessage());
	    } catch (Exception e) {


Additional To:

Subject: Salesforce Record Delete
Salesforce Record Delete,001F000000kvR7DIAU, 001F000000kvRA8IAM, 001F000000kvRC6IAM,

2 Comments on “Salesforce API Delete Account Record Send Email”

  1. Hai Michel,

    Great Posts about java and Salesforce. I need help .We can create objects in to Salesforce using Java,Can we create Classes(Controller) in Salesforce from Java?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: