How to insert field value programmatically after node is created? Let's say user creates a node of article type.There is a field called my_field which can be added multiple times to that node.If user creates or edits the node leaving my_field blank, a default value something like NA should be created in database and should be visible inside the my_field when user edits  node next time.

Tried so far:

Inside page.tpl.php

$query = new EntityFieldQuery();

$query-&gt;entityCondition('entity_type', 'node')
      -&gt;entityCondition('bundle', 'article')
      -&gt;propertyCondition('status', 1);

$result = $query-&gt;execute();

if (!empty($result['node'])) {
  $nids = array_keys($result['node']);

  foreach ($nids as $nid) {

    $node = node_load($nid);

   if(empty($node-&gt;field_my_field))
   {
     $field_language = field_language('node', $node, 'field_my_field');
     $node-&gt;field_my_field['LANGUAGE_NONE'][0]['value'] = 'NA';

      field_attach_insert('node', $node);
   }
  }
 }


Note: I don't want to create default value inside field settings as my_field can be added multiple times which can go upto more than 20.

I am using drupal 7.41.
I suppose it is due to your field_attach_insert(). Usually one should save the entire node using node_save(). 
However it is a lot easier if you install the entity API module and utilize its Entity Metadata Wrappers.

And I'd really recommend creating a custom module. You will need something similar to this:

function mymodule_node_insert($node) {
  if ($node-&gt;type == 'article') {
    mymodule_update_empty_value($node);
  }
}

function mymodule_node_update($node) {
  if ($node-&gt;type == 'article') {
    mymodule_update_empty_value($node);
  }
}

function mymodule_update_empty_value($node) {
  $wrapper = entity_metadata_wrapper('node', $node);
  // No value? 
  if  (empty($wrapper-&gt;field_myfield-&gt;value())) {
    $wrapper-&gt;field_myfield-&gt;set('NA');
    $wrapper-&gt;save();
  }
}