Salesforce Remote Access Using Property File in Java

Properties File
Navigate to Setup->Develop->Remote Access to create a new consumerkey and consumersecret when you create a new Remote Access to Salesforce

username=username@email.com
password=passwordsecuritytoken
client.id=consumerkey
client.secret=consumersecret
login.url=https://login.salesforce.com
merchandise.name=Target
merchandise.price=6.25
merchandise.description=Retail Store
merchandise.inventory=200

Java File to Add a new Merchandize Record to Salesforce

package com.thysmichels;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;


public class AddMerchandise {

		public static void main(String [] args){
			Properties props = new Properties();

            try {
                    //Load Properties File
	            props.load(new FileInputStream("AddMerchandise.properties"));
                    //Assign username and password to variables
	            String username = props.getProperty("username");
	            String password = props.getProperty("password");
	            //Check if username and password has values
	            if (username == null || username.trim().equals("") || 
	            	password == null || password.trim().equals("")){
	            	throw new IllegalArgumentException("Please provide a username and password in the properties file");
	            }
                    //Get clientId and clientSecret and check if they have values assigned
	            String clientId = props.getProperty("client.id");
	            String clientSecret = props.getProperty("client.secret");
	            if (clientId == null || clientId.trim().equals("") ||
	            	clientSecret == null || clientSecret.trim().equals("")){
	            	throw new IllegalArgumentException("Please provide a valid client id and client secret in the properties file");
	            }

	            String loginURL = props.getProperty("login.url");
	            
	            String name = props.getProperty("merchandise.name");
	            String price = props.getProperty("merchandise.price");
	            String desc = props.getProperty("merchandise.description");
	            String inventory = props.getProperty("merchandise.inventory");
	            //Login to Salesforce using:username + password + clientid + clientSecret and loginURL
	            ForceLogin login = login2ForceDotCom(username, password, 
	            				    				 clientId, clientSecret, 
	            									 loginURL);
	            if (login != null){	
                        //add Merchandise record
	            	String merchId = addMerchandise(login, name, price, desc, inventory);
	            	if (merchId != null){
	            		System.out.println("Merchandise record created successfully. Id="+merchId);
	            	}
	            }
            }
            catch(IOException e){
            	e.printStackTrace();
            }
		}
		
		private static ForceLogin login2ForceDotCom(String uName, 
												String pwd,
												String id,
												String secret,											
												String url){
			String postURL = (url==null || url.equals(""))?"https://login.salesforce.com":url;
			//Login to Salesforce REST interface
			postURL+="/services/oauth2/token?grant_type=password&client_id="+id+"&client_secret="+secret+"&username="+uName+"&password="+pwd;
			//Assign new http client and post the REST URI
			DefaultHttpClient httpclient = new DefaultHttpClient();
			HttpPost post = new HttpPost(postURL);
			
			try {
            	HttpResponse response = httpclient.execute(post);
                final int statusCode = response.getStatusLine().getStatusCode();
                //Check of statusCode is OK else throw error
                if (statusCode != HttpStatus.SC_OK) {
                	System.out.println("Error authenticating to Force.com:"+statusCode);
                	System.out.println("Error is:"+EntityUtils.toString(response.getEntity()));
                	return null;
                }
                
                String result = EntityUtils.toString(response.getEntity()); 
    			JSONObject object = (JSONObject) new JSONTokener(result).nextValue();
    			
    			ForceLogin login = new AddMerchandise().new ForceLogin();
    			login.accessToken = object.getString("access_token");
    			login.instanceUrl= object.getString("instance_url");
    			
    			return login;
            }	
    	    catch (Exception e)
    	    {
    	    	e.printStackTrace();
    	    }	
			return null;
		} 
		
		public static String addMerchandise(ForceLogin login,
											String name,
											String price,
											String desc,
											String inventory){
			JSONObject mechandise = new JSONObject();
			
			try {
				if (name != null && !name.trim().equals(""))
					mechandise.put("Name", name);
				if (price != null && !price.trim().equals(""))
					mechandise.put("Pice__c", price);
				if (desc != null && !desc.trim().equals(""))
					mechandise.put("Description__c", desc);
				if (inventory != null && !inventory.trim().equals(""))
					mechandise.put("Total_Inventory__c", inventory);

				String restResourceURI = login.instanceUrl + "/services/data/v23.0/sobjects/Merchandise__c/";

				HttpPost post = new HttpPost(restResourceURI);
				StringEntity se = new StringEntity(mechandise.toString());
				post.setEntity(se);
				post.setHeader("Authorization", "OAuth " + login.accessToken);
				post.setHeader("Content-type", "application/json");
					
				DefaultHttpClient client = new DefaultHttpClient();
		    	HttpResponse resp = client.execute(post);
		    	String result = EntityUtils.toString(resp.getEntity()); 
		    			    	
		    	
		    	if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED)
		    	{
		    		JSONObject ret = ((JSONArray) new JSONTokener(result).nextValue()).getJSONObject(0);
		    		System.out.println("Could not create new Merchandise record:"+resp.getStatusLine().getStatusCode());
                	System.out.println("Error code:"+ret.getString("errorCode"));
                	System.out.println("Error message:"+ret.getString("message"));
		    		return null;
		    	}
		    	else{
		    	   	return ((JSONObject) new JSONTokener(result).nextValue()).getString("id");
		    	}   	
			} catch (Exception e) {
				e.printStackTrace();
			}
 		
			return null;
		} 
		
		public class ForceLogin {
			private String instanceUrl;
			private String accessToken;
			
			public String getInstanceUrl()
			{
				return instanceUrl;
			}
			
			public void setInstanceUrl(String i)
			{
				instanceUrl = i;
			}

			public String getAccessToken()
			{
				return accessToken;
			}
			
			public void setAccessToken(String a)
			{
				accessToken = a;
			}
		}
}
About these ads

One Comment on “Salesforce Remote Access Using Property File in Java

  1. ForceLogin login = new AddMerchandise().new ForceLogin();
    what does it means ???
    plz reply…

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 215 other followers

%d bloggers like this: