简体   繁体   中英

Camel Salesforce component REST API salesforce:query cannot retrieve records

I want to execute a SOQL by using Camel Salesforce component salesforce:query API for retrieving SObject Contact(s) by using AccountId. But it only return a DTO Contact which all fields are null. And I have debugged with Camel Salesforce component source code, and I found it got the Response with code 200.

BTW, does anyone know how to implement a dynamic SOQL, I found I cannot use ${body} in it, say, "sObjectQuery=SELECT LastName FROM Contact WHERE AccountID='${body}'"

  1. It is a simple maven project
  2. DTOs: Contact, Account have been generated by camel-salesforce-maven-plugin
  3. Other Camel Salesforce component REST API: salesforce:upsertSObject, salesforce:getSObjectWithId, salesforce:deleteSObjectWithId and salesforce:createSObject can be run and returned the correct results

Route:

from("direct:query")
    .to("salesforce:query?sObjectQuery=SELECT LastName FROM Contact&sObjectClass="+ Contact.class.getName())
    .process(exchange -> {
        Object body = exchange.getIn().getBody();
        System.out.println(body.getClass());
        System.out.println(body);
 });
ProducerTemplate template = camelCtx.createProducerTemplate();
Object obj = template.requestBody("direct:query", null, Object.class);

Actual Result: class com.salesforce.test.Contact {"Reporting_States__c":null,"Specialty__c":null}

Expected Result: return a QueryRecordsContact object which contains a fields of Contact List. (QueryRecordsContact is a DTO which was generated by Camel Salesforce maven plugin)

Try rewriting your route like this:

    from("direct:query")
        .setHeader("sObjectQuery", simple("SELECT LastName FROM Contact WHERE id='${body}'"))
        .to("salesforce:query?sObjectQuery=&sObjectClass=" + QueryRecordsContact.class.getName())
        .process(exchange -> {
            Object body = exchange.getIn().getBody();
            System.out.println(body.getClass());
            System.out.println(body);
        });
  • you can place on sObjectQuery header the query you are interested in and build it dynamically there
  • the SF query returns a json containing a list of records, so you should pass QueryRecordsContact to the endpoint in order to make the right mapping

Actually query return "done" , "totalSize" ,"records" as response .whatever you queried to salesforce query is in the records.To view this you have to add below line and their getter and setter in your class.

private List <yourClassName> records;

and then mapped list of object to appropriate class.

For more details: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_query.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM