2

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);        
        }
    }

    }
7
  • 1
    It might be the format you're using for the CloseDate - typically, it uses the format "2014-08-08". If that doesn't fix it, do you get an error message back that you can post?
    – jongpie
    Commented Aug 8, 2014 at 15:16
  • @jongpie Thumbs Up for You..! That worked. Thanks....! Commented Aug 8, 2014 at 15:18
  • Awesome, glad that worked!
    – jongpie
    Commented Aug 8, 2014 at 15:18
  • @jongpie please post this as an answer so that the op can accept it when you get a chance :)
    – Rao
    Commented Aug 8, 2014 at 17:51
  • @rao, done! I'm still new to answering on stackexchange & didn't realize comments can't be flagged as answers.
    – jongpie
    Commented Aug 8, 2014 at 17:53

1 Answer 1

4

Posting my comment as the answer so it can be marked as accepted...

It might be the format you're using for the CloseDate - typically, it uses the format "2014-08-08". If that doesn't fix it, do you get an error message back that you can post?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .