Created
March 27, 2012 22:42
-
-
Save joshbirk/2221105 to your computer and use it in GitHub Desktop.
java code for Integration Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| JSONObject mechandise = new JSONObject(); | |
| try { | |
| if (name != null && !name.trim().equals("")) | |
| mechandise.put("Name", name); | |
| if (price != null && !price.trim().equals("")) | |
| mechandise.put("Price__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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| String postURL = (url==null || url.equals(""))?"https://login.salesforce.com":url; | |
| postURL+="/services/oauth2/token?grant_type=password&client_id="+id+"&client_secret="+secret+"&username="+uName+"&password="+pwd; | |
| DefaultHttpClient httpclient = new DefaultHttpClient(); | |
| HttpPost post = new HttpPost(postURL); | |
| try { | |
| HttpResponse response = httpclient.execute(post); | |
| final int statusCode = response.getStatusLine().getStatusCode(); | |
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| props.load(new FileInputStream("AddMerchandise.properties")); | |
| String username = props.getProperty("username"); | |
| String password = props.getProperty("password"); | |
| if (username == null || username.trim().equals("") || | |
| password == null || password.trim().equals("")){ | |
| throw new IllegalArgumentException("Please provide a username and password in the properties file"); | |
| } | |
| 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"); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ForceLogin login = login2ForceDotCom(username, password, clientId, clientSecret, loginURL); | |
| if (login != null){ | |
| String merchId = addMerchandise(login, name, price, desc, inventory); | |
| if (merchId != null){ | |
| System.out.println("Merchandise record created successfully. Id="+merchId); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment