Ashok Priyadarshan
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_PD RFsRBGTuqs N.m3Vd1bIw EF5_FNoEUK GVX0gBqUM1 YdL52614KX SW9O6pL6ab Wun9soVyw" ;
public static final String clientSecret="8F418A0BCE45 412081ED5D 3057A20B17 8C203A05EC 368BFC67D8 2FF5C4BFBD 92";
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("userna me", username));
loginParams.add(new BasicNameValuePair("passwo rd", password));
final HttpPost post = new HttpPost(TOKEN_URL);
post.setEntity(new UrlEncodedFormEntity(login Params));
final HttpResponse loginResponse = httpclient.execute(post);
// parse
final ObjectMapper mapper = new ObjectMapper().enable(Seri alizationF eature.IND ENT_OUTPUT );
final JsonNode loginResult = mapper.readValue(loginResp onse.getEn tity().get Content(), JsonNode.class);
final String accessToken = loginResult.get("access_to ken").asTe xt();
final String instanceUrl = loginResult.get("instance_ url").asTe xt();
final URIBuilder builder = new URIBuilder(instanceUrl);
builder.setPath("/services /data/v47. 0/query/") .setParame ter("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("Authorizati on", "Bearer " + accessToken);
final HttpResponse queryResponse = httpclient.execute(get);
final JsonNode queryResults = mapper.readValue(queryResp onse.getEn tity().get Content(), JsonNode.class);
System.out.println(mapper. writerWith DefaultPre ttyPrinter ().writeVa lueAsStrin g(queryRes ults));
ReturnString += mapper.writerWithDefaultPr ettyPrinte r().writeV alueAsStri ng(queryRe sults);
}
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/") .setParame ter("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/sobj ects/Oppor tunity/006 6g00000fjf OqAAI" }, "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+0 000", "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
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_PD
public static final String clientSecret="8F418A0BCE45
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
loginParams.add(new BasicNameValuePair("client
loginParams.add(new BasicNameValuePair("grant_
loginParams.add(new BasicNameValuePair("userna
loginParams.add(new BasicNameValuePair("passwo
final HttpPost post = new HttpPost(TOKEN_URL);
post.setEntity(new UrlEncodedFormEntity(login
final HttpResponse loginResponse = httpclient.execute(post);
// parse
final ObjectMapper mapper = new ObjectMapper().enable(Seri
final JsonNode loginResult = mapper.readValue(loginResp
final String accessToken = loginResult.get("access_to
final String instanceUrl = loginResult.get("instance_
final URIBuilder builder = new URIBuilder(instanceUrl);
builder.setPath("/services
ReturnString = new String("update Opportunity set " + Field + "=" + "'" + Value + "'" + " where ID = " + "'" + ID + "'" );
final HttpGet get = new HttpGet(builder.build());
get.setHeader("Authorizati
final HttpResponse queryResponse = httpclient.execute(get);
final JsonNode queryResults = mapper.readValue(queryResp
System.out.println(mapper.
ReturnString += mapper.writerWithDefaultPr
}
catch (Exception e) {
e.toString();
e.printStackTrace();
}
return ReturnString;
}
} //end of class
/////////// same query works for a select statement
builder.setPath("/services
returns:
{ "totalSize" : 1, "done" : true, "records" : [ { "attributes" : { "type" : "Opportunity", "url" : "/services/data/v47.0/sobj
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
SOQL and Apex update differently than normal SQL. You build object Opportunity into variable e.g. opp then you "update opp;"
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 likebuilder.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 + "'");
q=update+Opportunity+set+f
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.
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.
ASKER
I need a example to update a salesforce record. Full working example.... JAVA CODE PLEASE
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.