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;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

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
	    sfconfig.setUsername("username");
	    //Use your saleforce password with your security token look like: passwordjeIzBAQKkR6FBW8bw5HbVkkkk
	    sfconfig.setPassword("passwordsecuritytoken");
	    
		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("mail.smtp.host", "smtp.gmail.com");
		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 Salesforce.com by passing an array of Ids
	      DeleteResult[] deleteResults = entercon.delete(ids);
	     
	      
	      Message message = new MimeMessage(session);
	      message.setFrom(new InternetAddress("email@gmail.com"));
	      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("emailtosalesforce@7fbwcbhx0qtqo67npeds3yzro.aao2fmaq.f.le.salesforce.com"));
	      message.setSubject("Salesforce Record Delete");
	      message.setText("Salesforce Record Delete," + strconcat);
	      Transport.send(message);
	      
	      // 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) {
	      e.printStackTrace();
	    }
	}
}

Output

Additional To: emailtosalesforce@7fbwcbhx0qtqo67npeds3yzro.aao2fmaq.f.le.salesforce.com
CC: 
Attachment: 

Subject: Salesforce Record Delete
Body:
Salesforce Record Delete,001F000000kvR7DIAU, 001F000000kvRA8IAM, 001F000000kvRC6IAM,
About these ads

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 253 other followers

%d bloggers like this: