Jump to content

Inside API loop not looping within main loop


botlife

Recommended Posts

Hey there, so I have a custom Wordpress loop that is working just fine. Inside that loop I have a second loop using Salesforce API.

 

What is happening is that the main loop is looping but the secondary Salesforce loop is showing the same record for each and not looping. There is a unique ID for each so that isn't the problem.

 

while ($wp_query->have_posts()) : $wp_query->the_post();

$sid = get_the_ID();

 

$SalesForceId = get_post_meta($sid, 'SalesForceId', true);

$query = "SELECT Name, Phone, BillingCity, BillingState from Account WHERE Id = '" . $SalesForceId . "'";

$response = $mySforceConnection->query(($query));

 

foreach ($response->records as $record) {

 

$Name = $record->{Name};

$Phone = $record->{Phone};

$BillingCity = $record->{BillingCity};

$BillingState = $record->{BillingState};

 

}

 

echo $Name;

echo $BillingCity;

echo $BillingState;

 

endwhile;

?>

Link to comment
Share on other sites

First, CODE tags make things much clearer.

 

Second, just before

 

foreach ($response->records as $record) {

 

Can you do a

 

var_dump($response->records);die();

This is the output

array(1) { [0]=> object(stdClass)#4627 (5) { ["Id"]=> NULL ["BillingCity"]=> string(6) "OXNARD" ["BillingState"]=> string(2) "CA" ["Name"]=> string(28) "AMERICAN TALL SHIP INSTITUTE" ["Phone"]=> string(10) "8059010585" } }

Edited by botlife
Link to comment
Share on other sites

Ok, turn the fancy editor off by clicking the toggle switch, and try that again. 

 

But:

foreach ($response->records as $record) {

  $Name = $record->{Name};
  $Phone = $record->{Phone};
  $BillingCity = $record->{BillingCity};
  $BillingState = $record->{BillingState};

 }

echo $Name;
echo $BillingCity;
echo $BillingState;

 ?>

 

If you don't echo them until after your foreach() loop, you will only ever see the last set. 

Link to comment
Share on other sites

 

 

Well there's your issue, the array has only 1 element. your inner foreach is working fine.

 

The array has only one row because the query is focused on only one ID and not all the IDs.

 

The main loop is pulling the Salesforce IDs I have stored in Wordpress. The second loop is then taking that ID and running the second query through Salesforce. So without the second loop it will only return that one result which happened in the Dump and is happening in the second loop currently. Each run through should re-run the Salesforce query to yield a result and it isn't doing that. I can echo all the IDs so that isn't the problem. The problem seems to be that I can only use the second query once. I think I am doing something backwards. 

Link to comment
Share on other sites

Echo the query to see if it has the same ID each time or a different ID each time.

 

Really you should write one query to join these tables, if you want to do it the right way, post the tables structures and someone can help you write a JOIN to get all the data the right way.

Edited by Jessica
Link to comment
Share on other sites

Echo the query to see if it has the same ID each time or a different ID each time.

 

Really you should write one query to join these tables, if you want to do it the right way, post the tables structures and someone can help you write a JOIN to get all the data the right way.

 

Okay, when I echo the query, the first entry has results and every other entry is echoing 

 

SELECT Name, Phone, BillingCity, BillingState from Account WHERE Id = '003G00000184ong'array(0) { }

 

with no results. 

 

The ID though is different on each one so that is working, it just isn't running the second query more than once. Maybe it is because $query is the same variable name each time. Maybe there should be ++ on each $query and $response. I didn't join the tables because one is on a Wordpress database and the second is using Salesforce API. I have joined on the same database before, but not to separate entities. 

 

Link to comment
Share on other sites

Since you're sending a raw query, you can at least make just one query instead of many. First, query just wordpress (using RAW SQL) to get all of your IDs and put them in an array. Then use IN to put them in your other system's query.

Link to comment
Share on other sites

Since you're sending a raw query, you can at least make just one query instead of many. First, query just wordpress (using RAW SQL) to get all of your IDs and put them in an array. Then use IN to put them in your other system's query.

 

That is a really good idea. Let me whip something up. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.