ForEach循环反复返回相同的名称

时间:2018-04-25 23:59:35

标签: php arrays object salesforce

每当我通过SalesForce PHP api运行此代码时,它都会失败并显示错误:注意:尝试获取非对象的属性

    $query ="SELECT accountId,Status,Id,Service_Account_DMKT__r.name,(select Activity_Type__c from Tasks) from case where Owner.Name ='" . $name . "' AND CreatedDate = THIS_MONTH AND Record_type_name__c='Performance Reviews' AND status NOT IN ('')";
    $response = $mySforceConnection->query($query);
    $queryResult = new QueryResult($response); 
      foreach($queryResult->records as $case){ 
        //for ($queryResult->rewind(); $queryResult->pointer < $queryResult->size; $queryResult->next()) {    
        $callCounter = 0;
        $emailCounter = 0;
      $accountId = $case->current()->accountId; 
      $accountName=$case->current()->Service_Account_DMKT__r->Name;
      $caseId= $case->current()->Id; 


      if($case->any['Tasks']->records) { 
      $counter=0;       
      foreach($case->any['Tasks']->records as $record) {         
          $taskRecord = $record->any;
          if (strpos($taskRecord, 'Call - Outbound') !== false) {
            $callCounter++;
        } else {
          $emailCounter++;
        }
        $counter++;
        }
      }
      echo '<p>AccountName=' . $accountName . '</p><p>CaseId=' . $caseId . '</p>';
        echo '<p>' . $callCounter . ' Calls and ' . $emailCounter . ' emails';
        echo'<hr>';
        $index++;
      }

print_r($case);

我知道这是因为这三行。我没有正确地穿过这个物体。

 $accountId = $case->current()->accountId; 
 $accountName=$case->current()->Service_Account_DMKT__r->Name;
 $caseId= $case->current()->Id; 

但我不确定要使用什么而不是current()。以下是SF API的响应对象

stdClass Object
(
[type] => Case
[Id] => Array
    (
        [0] => 5000e00001J7L0pAAF
        [1] => 5000e00001J7L0pAAF
    )

[any] => Array
    (
        [0] => 00130000002bqXiAAIClosed - Contact Declined5000e00001J7L0pAAF
        [Service_Account_DMKT__r] => stdClass Object
            (
                [type] => Account
                [Id] => 
                [any] => brinsoncorsicanafordfd
            )

        [Tasks] => stdClass Object
            (
                [done] => 1
                [queryLocator] => 
                [records] => Array
                    (
                        [0] => stdClass Object
                            (
                                [type] => Task
                                [Id] => 
                                [any] => 
                            )

                    )

                [size] => 1
            )

    )

)

1 个答案:

答案 0 :(得分:0)

我终于设法通过将响应转换回另一个对象来修复它

$query ="SELECT accountid,Status,Id,Service_Account_DMKT__r.name,(select Activity_Type__c,subject from Tasks) from case where Owner.Name ='" . $SFName . "' AND CreatedDate = THIS_MONTH AND Record_type_name__c='Performance Reviews' AND status NOT IN ('')";
    $response = $mySforceConnection->query($query);
    $queryResult = new QueryResult($response); 
      foreach($queryResult->records as $case){  //For every record within $queryResult
        $callCounter = 0;                       //Set up our task counters
        $emailCounter = 0;
        $sObject = new SObject($case); //turn $case back into a SObj to easy step thru
        $accountId= $sObject->AccountId; //Pull AccountId from $sObject
        $accountName=$sObject->Service_Account_DMKT__r->Name;
        $caseId=$sObject->Id;
        $caseStatus=$sObject->Status;
      if(!isset($sObject->queryResult)) { //Check if there are any tasks on the record, otherwise we'll get an error
        $callCounter=0; //if there are no tasks, set counters to 0
        $emailCounter=0;
      }else{
      $counter=0;
      foreach($case->any['Tasks']->records as $record) {  //for each task in the $case 
        $taskObject = new SObject($record);      //Turn $record into taskObject so we can step through it.
         $taskType = $taskObject->Activity_Type__c; //Pull the activity type out of TaskObject
         if($taskType == "Call - Outbound"){ //Calling $taskType actually allows us to compare the obj to a string, where as going through this in an array format would not!
           $callCounter++; //increase counter if the taskType is a call
       } else {
         $emailCounter++;
       }            
       }    
       }
       echo '<p>AccountName=' . $accountName . '</p><p>AccountID=' . $accountId . '</p><p>CaseId=' . $caseId . '</p><p>CaseStatus=' . $caseStatus . '</p>';
       echo '<p>' . $callCounter . ' Calls and ' . $emailCounter . ' emails';
       echo'<hr>'; 
     }