0

My snippet code below. What i'm trying to do first is be able to update batch items with large number of items(THIS WORKS) and in between be able to get a lookup id field from one of the fields and update another column with the id.

Updating multiple items in batches work but i can't get the lookup field id value so i can't set the field i want and not sure what i'm missing here.

Thanks in Advance

<script language="javascript" type="text/javascript">
var items;
var oList;
var oListItem;
var LookUpColumnId;


var IDs = [];  
var LookUpIDs = [];  


function getItemsToBeUpdated() 
{

  $('#lblprogress').text("Updating items please wait.... ");
  $('#lblprogress').show();


    var siteUrl = '/sites/Dev/'; 

    var clientContext = new SP.ClientContext(siteUrl);

    oList = clientContext.get_web().get_lists().getByTitle('ForceUpdateTest');

    var lookupFieldName = "LookUpColumn";

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View><Query></Query><RowLimit>900</RowLimit><ViewFields><FieldRef Name='LookUpColumn' LookupId='TRUE' /></ViewFields></View>");
    items = oList.getItems(camlQuery);

    clientContext.load(items);    
    clientContext.executeQueryAsync(onUpdateSucceeded, onUpdateFailed);


}

function onUpdateSucceeded() {

   var itemEnumerator = items.getEnumerator();
                while (itemEnumerator.moveNext()) {

                   var item = itemEnumerator.get_current();



                    // You now have the item you were looking for!
                    //console.log(item.get_item("LookUpColumn").get_lookupId());
                    //This works
                    LookUpColumnId = item.get_item("LookUpColumn").get_lookupId();

                    IDs.push(item.get_item("ID"));
                    LookUpIDs.push(item.get_item("LookUpColumn").get_lookupId());     


                }

    console.log(IDs);
    console.log(LookUpIDs);

    var siteUrl = '/sites/Dev/'; 
     var clientContext = new SP.ClientContext(siteUrl);
     oList = clientContext.get_web().get_lists().getByTitle('ForceUpdateTest');

    var itemsToLoadForUpdate = Array(); // Holds our "current" items to update
    var totalItemsToUpdate = 900;
    var limitItemsToUpdate = 50; // Update every 90 items

    var batchUpdatesCounter = Math.ceil(totalItemsToUpdate / limitItemsToUpdate); // If you need to count the remaining batch updates
    var restItemsToUpdate = 0; // Items to update after (see below)

    //This routine below does the update in batches and it works when i have large items

  for (var i = 0; i < IDs.length; i++) {

        var oListItem = oList.getItemById(IDs[i]);

        clientContext.load(oListItem);


         //I can't get the lookupid here and getting error "The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested."
        //var text = oListItem.get_item("LookUpColumn")
        //var lookupid = oListItem.get_item("Title").get_lookupId();

          //Can get the item IDs
          console.log(IDs[i]);

          console.log(t);
          console.log(text); 




       //This works when i hardcode a value but not when i use lookupid value 
          oListItem.set_item('LookUpID', 10);

         //This doesn't work when i try to set a new column with the lookupid
          oListItem.set_item('LookUpID',lookupid)


        oListItem.update();
        itemsToLoadForUpdate[i] = oListItem;

        clientContext.load(itemsToLoadForUpdate[i]);
        if (i && i % limitItemsToUpdate == 0) {
            console.log('Updating ' + limitItemsToUpdate + ' items...');
            clientContext.executeQueryAsync(this.onUpdateSucceeded, this.onUpdateFailed);
            restItemsToUpdate = 0;
        }
        else { // Remaining items to update
            restItemsToUpdate = i % limitItemsToUpdate;
        }

    }
    if (restItemsToUpdate) {
         //console.log('Updating rest items...');
         console.log('Updating ' + restItemsToUpdate + ' items...');

        clientContext.executeQueryAsync(this.onUpdateSucceeded, this.onUpdateFailed);
    }



$('#lblprogress').text("Records are being updated");
    $('#reset').show();
}


function onUpdateFailed(sender, args) 
    {    

     $('#lblprogress').hide();
     $('#lblerror').text('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
     $('#reset').show();

    }



</script>

1 Answer 1

0

In your code there is a below line :

clientContext.load(oListItem);

I cannot see "executeQueryAsync" after this line.

You code will not make a call to server just by "clientContext.load(oListItem)".

Here you will have to add one more line to make a call to server and get the required data from server. Refer below example :

clientContext.load(oListItem);
clientContext.executeQueryAsync(yoursucceddhandler, yourFailureHandler);

And then in successhandler you will be able to access the value.

You can Refer the function "getItemsToBeUpdated()" from your code which looks perfect.

2
  • Thanks Rohit will give it a shot and let you know
    – naijacoder
    Commented Apr 6, 2019 at 2:29
  • Rohit i tried that but still getting the error :- The property or field has not been initialized. It has not been requested or the request has not been execute. On the line var getLookupID = oListItem.get_item("LookUpColumn").get_lookupId();
    – naijacoder
    Commented Apr 8, 2019 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.