Is it possible to use custom code to query salesforce? Is this documented anywhere?

I would like to write a salesforce query and loop over the returned records from salesforce? Is this possible?

Comments

drupalnuts created an issue. See original summary.

aaronbauman’s picture

Status: Active » Needs review

Yes, please use \Drupal\salesforce\Rest\RestClient::query or \Drupal\salesforce\Rest\RestClient::queryAll

drupalnuts’s picture

Do you have an example in code where I see how this is used?

acrosman’s picture

Status: Needs review » Closed (works as designed)
Related issues: +#2879844: Update SelectQuery interface to closely resemble core database API

To run a query your code would look not totally unlike:

$query = new \Drupal\salesforce\SelectQuery('Opportunity');
$query->fields = [
  'Id',
  'Name',
  'Description',
  'CloseDate',
  'Amount',
  'StageName',
];
$query->addCondition('AccountId', $desiredAccountId, '=');
$query->conditions[] = [
  "(StageName", '=', "'Closed Won'",
  'OR', 'StageName', '=', "'Closed Lost')",
];
$query->conditions[] = ['CloseDate', '>=', $someSelectedDate];

$sfResponse = \Drupal::service('salesforce.client')->query($query);

More details: https://spinningcode.org/2019/10/salesforce-queries-and-proxies-in-drupa...

You might also want to look at the sample sub module for ideas of other things you can do with the suite.

richard moger’s picture

Very helpful snippet. thanks :-)

Small note for others, you must include an Id field in your fields array else your query will be returned successfully from sf but records will not get created by the salesforce module as they will be deemed invalid.