I am trying to make rest call and insert Account , case and Opportunity. My code work well for Account and case, but fails for opportunity. When making rest Call for oppurtunity, 400(Bad Request) response is received.i am unable to figure out why.
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.json.JSONObject;
import org.json.JSONTokener;
public class Login {
/**
* @param args
* @throws IOException
* @throws HttpException
*/
public static void main(String[] args) throws HttpException, IOException {
try{
PostMethod method = new PostMethod("https://login.salesforce.com/services/oauth2/token");
StringBuilder content = new StringBuilder();
content.append("grant_type=password");
content.append("&client_id=clientid");
content.append("&client_secret=clientsecret");
content.append("&username=username.com");
content.append("&password=password+securitytoken");
method.setRequestEntity(new StringRequestEntity(content.toString(), "text/plain", "UTF-8"));
method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
HttpClient client = new HttpClient();
client.executeMethod(method);
JSONObject authResponse = new JSONObject(new JSONTokener(new InputStreamReader(method.getResponseBodyAsStream())));
System.out.println("Auth response: " + authResponse.toString(2));
String accessToken = authResponse.getString("access_token");
String instanceUrl = authResponse.getString("instance_url");
HttpClient client1 = new HttpClient();
JSONObject account = new JSONObject();
String test = "test";
account.put("Name", test);
PostMethod post = new PostMethod(instanceUrl + "/services/data/v20.0/sobjects/Account/");
post.setRequestHeader("Authorization", "OAuth " + accessToken);
post.setRequestEntity(new StringRequestEntity(account.toString(),"application/json", null));
if(201 == client1.executeMethod(post))
{
authResponse = new JSONObject(new JSONTokener(new InputStreamReader(post.getResponseBodyAsStream())));
System.out.println("Auth response: " + authResponse.toString(2));
}
String accId = authResponse.getString("id");
JSONObject cases = new JSONObject();
cases.put("status", new String("New"));
cases.put("origin", new String("Phone"));
PostMethod post2 = new PostMethod(instanceUrl +"/services/data/v20.0/sobjects/case/");
post2.setRequestHeader("Authorization", "OAuth " + accessToken);
post2.setRequestEntity(new StringRequestEntity(cases.toString(),"application/json", null));
if(201 == client1.executeMethod(post))
{
authResponse = new JSONObject(new JSONTokener(new InputStreamReader(post.getResponseBodyAsStream())));
System.out.println("Auth response: " + authResponse.toString(2));
}
JSONObject opp = new JSONObject();
opp.put("Name", new String("New"));
opp.put("CloseDate", new String("8/8/2014"));
opp.put("StageName", new String("Qualification"));
opp.put("AccountId", accId);
opp.put("Probability",new String("10.0"));
PostMethod post3 = new PostMethod(instanceUrl +"/services/data/v20.0/sobjects/Opportunity/");
post3.setRequestHeader("Authorization", "OAuth " + accessToken);
post3.setRequestEntity(new StringRequestEntity(opp.toString(),"application/json", null));
System.out.println(client1.executeMethod(post3));
if(201 == client1.executeMethod(post3))
{
authResponse = new JSONObject(new JSONTokener(new InputStreamReader(post3.getResponseBodyAsStream())));
System.out.println("Auth response: " + authResponse.toString(2));
}
else
{
System.out.println();
}
}
catch(Exception e)
{
System.out.print(e);
}
}
}