Why can my view not access the data of a new node when using Rules event After saving new content? Using the Rules module and Drupal 7, I set up a rule "After saving new content of type X".

This is what I want to happen when adding a new content of type "X":


  Based on its nid, call a view, which returns back a JSON, from where I fetch the info to work later. The info I need is on this view, not in the node I am inserting, but to get this view I need the nid and some other info from the node I am creating


The problem is that even if I get the nid of the new node, when I call the view it just returns an empty result. It seems like the data it's still not on Drupal database, so the view results empty. I tried to add a sleep(10) before calling the view, giving some time to Drupal, but no success.

The Content is published, and I added also a 'Save entity' Action to the Rule

Hope with this code here helps to understand; it's in Editing action "Execute custom PHP code", "After saving new content of type X"

dsm($node);  //I can see al attributes from the node I am inserting

$url="http://localhost/bopa/?q=export_cultivos/$node-&gt;nid";

dpm($url); 

//it gives me a correct URL, that tested later directly on the browser, works
//If instead of $node-&gt;nid I use an existing nid, I get back results correctly, so the code below is correct

sleep(10);
$data=file_get_contents($url);

$data2 = json_decode($data,true);

dsm($data2);
//EMPTY array
IMO your "issue" (about "It seems like the data it's still not on Drupal database, so the view results empty") is because of the event (= "after saving new content") you are using, which might simply not be the right event.

Moreover, have a look at issue # 430274, about an event which is a variation of your event here, and somehow explains that "after saving something" actually happens ... "before saving something" (which is not a bug, simply how the Rules module  works ...).

A possible compromise to avoid these kind of issues, is to transform what's you're trying to do into a Rules Component, so that your original Rule "schedules the execution" (using the Rules Scheduler submodule) of that Rules Component (eg X secs or mins "after saving that new user account" ... if your cron job runs frequent enough, if not it'll be next time cron runs). This compromise will ensure that at the time the Rules Component is executed, the content is for sure saved ... (so that can't be the reason anymore then why it would still be empty ...).