Link to home
Start Free TrialLog in
Avatar of Ashok Priyadarshan
Ashok PriyadarshanFlag for United States of America

asked on

SALESFORCE with JAVA

hold.tarI have failing call to update a table of salesforce. It returns this message:

update Opportunity set NextStep='Contact John Smith' where ID = '0066g00000fjfOqAAI'[ { "message" : "unexpected token: update", "errorCode" : "MALFORMED_QUERY" } ]


Here is my source code

public class SFEngine {
        public static final String clientId="3MVG9LBJLApeX_PDRFsRBGTuqsN.m3Vd1bIwEF5_FNoEUKGVX0gBqUM1YdL52614KXSW9O6pL6abWun9soVyw";

        public static final String clientSecret="8F418A0BCE45412081ED5D3057A20B178C203A05EC368BFC67D82FF5C4BFBD92";
        public static final String redirectUrl="https://na174.salesforce.com/services/oauth2/callback";
        public static  String tokenUrl="https://login.salesforce.com/services/oauth2/token";
        public static final String environment="https://login.salesforce.com";
        public static final String username="xxxxxxxxxxx";
        public static final String password="xxxxxxxxxxx";                

        public static  String accessToken="";
        public static  String instanceUrl="";
        public HttpClient httpclient;
        public  HttpResponse queryResponse ;
        public  JsonNode queryResults;
        public  HttpGet get;
        public  URIBuilder builder;

private static final String TOKEN_URL =  "https://login.salesforce.com/services/oauth2/token";
public static String  OpenRest(String ID, String Field, String Value) {

        String consumerKey = clientId;
        String consumerSecret = clientSecret;
        String ReturnString = new String();


        try {
            // login
            final CloseableHttpClient httpclient = HttpClients.createDefault();

            final List<NameValuePair> loginParams = new ArrayList<NameValuePair>();
            loginParams.add(new BasicNameValuePair("client_id", consumerKey));
            loginParams.add(new BasicNameValuePair("client_secret", consumerSecret));
            loginParams.add(new BasicNameValuePair("grant_type", "password"));
            loginParams.add(new BasicNameValuePair("username", username));
            loginParams.add(new BasicNameValuePair("password", password));

            final HttpPost post = new HttpPost(TOKEN_URL);
            post.setEntity(new UrlEncodedFormEntity(loginParams));

            final HttpResponse loginResponse = httpclient.execute(post);

            // parse
            final ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);

            final JsonNode loginResult = mapper.readValue(loginResponse.getEntity().getContent(), JsonNode.class);
            final String accessToken = loginResult.get("access_token").asText();
            final String instanceUrl = loginResult.get("instance_url").asText();


            final URIBuilder builder = new URIBuilder(instanceUrl);
builder.setPath("/services/data/v47.0/query/").setParameter("q", "update Opportunity set "  + Field + "=" + "'" + Value + "'" + " where ID = " +  "'" + ID + "'"  );
ReturnString = new String("update Opportunity set "  + Field + "=" + "'" + Value + "'" + " where ID = " +  "'" + ID + "'"  );

            final HttpGet get = new HttpGet(builder.build());
            get.setHeader("Authorization", "Bearer " + accessToken);

            final HttpResponse queryResponse = httpclient.execute(get);

            final JsonNode queryResults = mapper.readValue(queryResponse.getEntity().getContent(), JsonNode.class);

            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(queryResults));
            ReturnString += mapper.writerWithDefaultPrettyPrinter().writeValueAsString(queryResults);

  }
        catch (Exception e) {
            e.toString();
            e.printStackTrace();
        }
      return ReturnString;
    }



 } //end of class


/////////// same query works for a select statement
 builder.setPath("/services/data/v47.0/query/").setParameter("q", "SELECT ID, Name, Description, LeadSource, Type, Amount, Probability, LastViewedDate, CloseDate, ExpectedRevenue, LastActivityDate, IsWon, NextStep  FROM opportunity where ID = " + "'" + ID + "'");


returns:
{ "totalSize" : 1, "done" : true, "records" : [ { "attributes" : { "type" : "Opportunity", "url" : "/services/data/v47.0/sobjects/Opportunity/0066g00000fjfOqAAI" }, "Id" : "0066g00000fjfOqAAI", "Name" : "AWSWaveLength.COM", "Description" : "AWS Experts is designing EDGE servers. This information will also be in SERVICENOW as a KB article once this is completed and closed.", "LeadSource" : "Partner Referral", "Type" : "New Customer", "Amount" : 800000.0, "Probability" : 85.0, "LastViewedDate" : "2020-01-23T19:01:40.000+0000", "CloseDate" : "2020-03-31", "ExpectedRevenue" : 680000.0, "LastActivityDate" : null, "IsWon" : false, "NextStep" : "WINTEMPLATES" } ] }


 
YOU CAN TEST these with these RESTAPI calls
select
http://ec2-18-217-26-94.us-east-2.compute.amazonaws.com:8080/VZServiceNowHub/Server/SFOpportunity/0066g00000fjfOqAAI 

update

 http://ec2-18-217-26-94.us-east-2.compute.amazonaws.com:8080/VZServiceNowHub/Server/SFOpportunityEdit/0066g00000fjfOqAAI/NextStep/Contact John Smith
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

SOQL and Apex update differently than normal SQL.  You build object Opportunity into variable e.g. opp then you "update opp;"
builder.setPath("/services/data/v47.0/query/").setParameter("q", "SELECT ID, Name, Description, LeadSource, Type, Amount, Probability, LastViewedDate, CloseDate, ExpectedRevenue, LastActivityDate, IsWon, NextStep  FROM opportunity where ID = " + "'" + ID + "'");

Open in new window

If that's producing a url query string, you will need to encode it with something like java.net.URLEncoder. It should then look more like

q=update+Opportunity+set+foo%3D%27bar%27+where+ID+%3D+%27200%27
Avatar of Ashok Priyadarshan

ASKER

I was told this....
SOQL and Apex update differently than normal SQL.  You build object Opportunity into variable e.g. opp then you "update opp;"

Can I get an example I have provided the non working source.
I need a example to update a salesforce record. Full working example.... JAVA CODE PLEASE
ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial