1

I'm relatively new to Salesforce and the Salesforce API and came across something that seemed odd, your help would be appreciated.

I'm using the following query with the PHP API:

$query="Select FirstName, LastName, Email, (select Campaign.Name, CampaignId from CampaignMembers) From Lead where Email ='testsflead@example.com'";
    $response = $mySforceConnection->query($query);
    $queryResult = new QueryResult($response);
    for ($queryResult->rewind(); $queryResult->pointer < $queryResult->size; $queryResult->next()) {
      $record = $queryResult->current();
      foreach($record->queryResult[0]->records as $campaignResults) {
        var_dump($campaignResults);
      }
    }

When I tried to run comparison operators on some of the results they failed. var_dump revealed that the character count for some of the results were not as they appeared.

Look at the any['Campaign']->any and any[0] strings, see how the character count is 19 characters more for any['Campaign']->any and 31 characters more for any[0].

Here is the output of var_dump:

object(stdClass)#11 (3) {
  ["type"]=>
  string(14) "CampaignMember"
  ["Id"]=>
  NULL
  ["any"]=>
  array(2) {
    ["Campaign"]=>
    object(stdClass)#12 (3) {
      ["type"]=>
      string(8) "Campaign"
      ["Id"]=>
      NULL
      ["any"]=>
      string(29) "Campaign 1"
    }
    [0]=>
    string(49) "701F0000000m06gIAA"
  }
}
object(stdClass)#13 (3) {
  ["type"]=>
  string(14) "CampaignMember"
  ["Id"]=>
  NULL
  ["any"]=>
  array(2) {
    ["Campaign"]=>
    object(stdClass)#14 (3) {
      ["type"]=>
      string(8) "Campaign"
      ["Id"]=>
      NULL
      ["any"]=>
      string(29) "Campaign 2"
    }
    [0]=>
    string(49) "701F0000000m06jIAA"
  }
}
object(stdClass)#15 (3) {
  ["type"]=>
  string(14) "CampaignMember"
  ["Id"]=>
  NULL
  ["any"]=>
  array(2) {
    ["Campaign"]=>
    object(stdClass)#16 (3) {
      ["type"]=>
      string(8) "Campaign"
      ["Id"]=>
      NULL
      ["any"]=>
      string(29) "Campaign 2"
    }
    [0]=>
    string(49) "701F0000000m06kIAA"
  }
}
0

As a workaround, instead of joining the CampaignMember query with the main Lead query, I tried splitting them into two separate queries and running the second query inside the results loop of the first query. This time the results were returned with the correct character count and I was able to run the comparison operators with success.

Here is my updated code:

$query="Select Id, FirstName, LastName, Email From Lead where Email ='testsflead@example.com'";
$response = $mySforceConnection->query($query);
$queryResult = new QueryResult($response);
for ($queryResult->rewind(); $queryResult->pointer < $queryResult->size; $queryResult->next()) {
  $record = $queryResult->current();
  $query_campaigns="select Campaign.Name, CampaignId from CampaignMember where LeadId ='".$record->Id."'";
  $response_campaigns = $mySforceConnection->query($query_campaigns);
  $queryResult_campaigns = new QueryResult($response_campaigns);
  for ($queryResult_campaigns->rewind(); $queryResult_campaigns->pointer < $queryResult_campaigns->size; $queryResult_campaigns->next()) {
    $record_campaigns = $queryResult_campaigns->current();
    var_dump($record_campaigns);
  }
}

The same can be accomplished for Contacts, like this:

$query="Select Id, FirstName, LastName, Email From Contact where Email ='testsfcontact@example.com'";
$response = $mySforceConnection->query($query);
$queryResult = new QueryResult($response);
for ($queryResult->rewind(); $queryResult->pointer < $queryResult->size; $queryResult->next()) {
  $record = $queryResult->current();
  $query_campaigns="select Campaign.Name, CampaignId from CampaignMember where ContactId ='".$record->Id."'";
  $response_campaigns = $mySforceConnection->query($query_campaigns);
  $queryResult_campaigns = new QueryResult($response_campaigns);
  for ($queryResult_campaigns->rewind(); $queryResult_campaigns->pointer < $queryResult_campaigns->size; $queryResult_campaigns->next()) {
    $record_campaigns = $queryResult_campaigns->current();
    var_dump($record_campaigns);
  }
}

This does not answer the question of why the character count is not the same as displayed, but the code works...

I hope this helps somebody.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.