title: Error trying to update Sharepoint list with javascript
body: <p><strong>Update</strong></p>
  
  <p>I have now tried a new approach. This works when I call it one time but the second time it tells me that the collection hasnt been initiated for some reason. The comments in the test function of this code snippet will clarify my problem.</p>
  
  <p>Javascript:</p>
  
  <pre><code>function test(){
      //this first call works
      countRetrieve('Very', 'Difficult');
      //this second call generates error that collListItem hasnt been initiated
      countRetrieve('Less', 'Interesting');
  }   
  
  
  function countRetrieve(grade, title) {
      var siteUrl = '/sites/MySite';
      var clientContext = new SP.ClientContext(siteUrl);
      var oList = clientContext.get_web().get_lists().getByTitle('Summary');
  
      var camlQuery = new SP.CamlQuery();
  
      camlQuery.set_viewXml('&lt;View&gt;&lt;Query&gt;&lt;Where&gt;' +
      '&lt;And&gt;' +
      '&lt;Eq&gt;&lt;FieldRef Name=\'Grad\'/&gt;&lt;Value Type=\'Text\'&gt;' +
      grade +
      '&lt;/Value&gt;&lt;/Eq&gt;' +
      '&lt;Eq&gt;&lt;FieldRef Name=\'Title\'/&gt;&lt;Value Type=\'Text\'&gt;' +
      title +
      '&lt;/Value&gt;&lt;/Eq&gt;' +
      '&lt;/And&gt;' +  
      '&lt;/Where&gt;&lt;/Query&gt;&lt;/View&gt;');
      this.collListItem = oList.getItems(camlQuery);
      clientContext.load(collListItem);
      clientContext.executeQueryAsync(Function.createDelegate(this, this.onRetrieveQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
  }
  
  function onRetrieveQuerySucceeded(sender, args) {
      listItemEnumerator = collListItem.getEnumerator();
  
      while (listItemEnumerator.moveNext()) {
          var oListItem = listItemEnumerator.get_current();
          itemId = oListItem.get_id();
          itemCount = oListItem.get_item('Count');
      }
      updateCount();
  }
  
  
  
  function updateCount() {
      var clientContext = new SP.ClientContext('/sites/MySite');
      var oList = clientContext.get_web().get_lists().getByTitle('Summary');
  
      this.oListItem = oList.getItemById(itemId);
      //update the count, raise it by one
      var c = itemCount + 1;
      oListItem.set_item('Count', c);
  
      oListItem.update();
  
      clientContext.executeQueryAsync(Function.createDelegate(this, this.onUpdateSucceeded), Function.createDelegate(this, this.onQueryFailed));
  }
  
  function onUpdateSucceeded(sender, args){
      alert('item count updated');
  }
  </code></pre>
  
  <p>Im trying to retrieve the current value of the column "Count" in my list, and then rise this value by 1. However i get an error that tells me the Collection has not been initialized. </p>
  
  <p>Didnt it get initialized in <em>this.collListItem = oList.getItems(camlQuery);</em> ?</p>
  
  <p>It is possible this function is totally wrong, Im very grateful for tips on how to perform this task as Im new both to Sharepoint and Javascript. </p>
  
  <p>This is my code (javascript):</p>
  
  <pre><code>function countUpdate() {
              var siteUrl = '/sites/MySite';
              var clientContext = new SP.ClientContext(siteUrl);
              var oList = clientContext.get_web().get_lists().getByTitle('Summary');
  
              var camlQuery = new SP.CamlQuery();
  
              camlQuery.set_viewXml('&lt;View&gt;&lt;Query&gt;&lt;Where&gt;' +
              '&lt;And&gt;' +
              '&lt;Eq&gt;&lt;FieldRef Name=\'Grade\'/&gt;&lt;Value Type=\'Text\'&gt;' +
              'Really' +
              '&lt;/Value&gt;&lt;/Eq&gt;' +
              '&lt;Eq&gt;&lt;FieldRef Name=\'Property\'/&gt;&lt;Value Type=\'Text\'&gt;' +
              'Narrow' +
              '&lt;/Value&gt;&lt;/Eq&gt;' +
              '&lt;/And&gt;' +  
              '&lt;/Where&gt;&lt;/Query&gt;&lt;/View&gt;');
              this.collListItem = oList.getItems(camlQuery);
              clientContext.load(collListItem);
  
              clientContext.executeQueryAsync(Function.createDelegate(this, this.onUpdateQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
  
              //update
              var listItemEnumerator = collListItem.getEnumerator();
              while (listItemEnumerator.moveNext()) {
                  var oListItem = listItemEnumerator.get_current();
                  var count = oListItem.get_item('Count');
                  oListItem.set_item('Count', '40');
                  oListItem.update();
              }
              clientContext.executeQueryAsync(Function.createDelegate(this, this.onUpdateQuerySucceeded),Function.createDelegate(this, this.onQueryFailed));
          }
  </code></pre>