Programmatically creating a node doesn't save custom fields I'm using this code in Rules, after saving a certain type of content, in execute custom PHP code. By now, i'm just trying to create anothe node of that type after the first is created.
The problem is that this code creates a new node indeed, but without any custom fields. It has only language associated to it. Any idea on what's wrong?
I followed the tutorial at http://fooninja.net/2011/04/13/guide-to-programmatic-node-creation-in-drupal-7/

$nodo = new stdClass(); // Create a new node object
$nodo-&gt;type = "capitoli"; // Or page, or whatever content type you like
node_object_prepare($nodo); // Set some default values
$nodo-&gt;language = 'it'; // Or e.g. 'en' if locale is enabled
$nodo-&gt;uid = $node-&gt;uid; // UID of the author of the node; or use $nodo-&gt;name
$nodo-&gt;field_numero_capitolo[$nodo-&gt;language][0]['value']="2220";
$nodo-&gt;field_link[$nodo-&gt;language][0]['value']="aafafsf";
$path = 'node_created_on' . date('YmdHis'); // I prefer using pathauto, which would override the below path
$nodo-&gt;path = array('alias' =&gt; $path);
if($nodo = node_submit($nodo)) { // Prepare node for saving
    node_save($nodo);
    echo "Node with nid " . $nodo-&gt;nid . " saved!\n";
}


If I replace 

$nodo-&gt;language = 'it';


with

$nodo-&gt;language = LANGUAGE_NONE;


this code create another node identical to the first one, not considering the values I used in the code and I can't figure out why.

EDIT:
I tried doing as suggested in comments

$nodo-&gt;field_numero_capitolo['und'][0]['value']="2220";
$nodo-&gt;field_link['und'][0]['value']="aafafsf";


And it seems to work. What i did was to insert directly 'und'. Generally I think it is not a good idea to write directyl 'und', but these two fields are a link and a number, that should not change depending on language.
If you're using Rules anyway why don't you use the Create new entity action which should take care of everything? You will need to use the Set a data value action multiple times and possibly the Save entity action too.

In answer to your question I think you need to set the language before node_object_prepare so your code would become:

$nodo = new stdClass(); // Create a new node object
$nodo-&gt;type = "capitoli"; // Or page, or whatever content type you like
$nodo-&gt;language = 'it'; // Or e.g. 'en' if locale is enabled
node_object_prepare($nodo); // Set some default values


Also you don't need the node_submit($nodo) line