11

I am stuck at a place where I have to update a list which has a Lookup column. For example I am using this code to update my normal list

    var clientContext = SP.ClientContext.get_current();
    var webSite = clientContext.get_web();
    var lists = webSite.get_lists();
    var testList = lists.getByTitle("TestList");
    var itemCreationInfo = new SP.ListItemCreationInformation();
    var listItem = testList.addItem(itemCreationInfo);
    listItem.set_item("Title", "ABC");
    listItem.set_item("Name", "XYZ");
    listItem.set_item("Age", 21);
    listItem.update();
    clientContext.executeQueryAsync(Function.createDelegate(this,this.Succeeded),Function.createDelegate(this,this.Failed));

Now, I have to update another list with the ID of the just created item in the TestList, as a lookup column. On Succeeded function again I am trying to update the second List,

function Succeeded(sender,args) 
{
....
....
var carsList = lists.getByTitle("LookupList");
var itemCreationInfo = new SP.ListItemCreationInformation();
var listItem = carsList.addItem(itemCreationInfo);
listItem.set_item("Title", "Plan");
listItem.set_item("Place", "Delhi");
listItem.set_item("PlanId", ***???*** ); // Lookup Column 
listItem.update();

But how can I get recently added item id to provide in PlanId column at Succeeded function. Please let me know what needs to be done for the same.

3 Answers 3

6
function onQuerySucceeded() {
    alert('Item created: ' + listItem.get_id());
}
2
  • this! using a search by title or top 1 is not a viable solution for a transactional system. Commented Jul 10, 2012 at 18:33
  • I'm confused. Does listItem.get_id() use title or top 1?
    – Tom Resing
    Commented Jul 10, 2012 at 19:46
1

You can easily get the last item id via the REST API:

http://PathToSite/_vti_bin/listdata.svc/ListName?$select=Id&$orderby=Id%20desc&$top=1

In plain English: get the list ids ($select=Id) in descending order ($orderby=Id desc), and only retrieve the top item ($top=1).

0

There is no direct method in the JavaScript Object Model to achieve what you are doing. You will have to make another call to the 'TestList' to fetch the newly created item.

You can achieve this through the following way:

    function Suceeded(){
      //1) Query the 'TestList' to get items by Created Column.
      //2) Set Ascending='FALSE' so that the latest created item will be fetched first.
      //3) Set RowLimit of the Query to 1 so that only one item (the one you just created) will be fetched.

       clientContext.executeQueryAsync(Function.createDelegate(this,this.FetchCurrentItemIdSucceeded),Function.createDelegate(this,this.Failed));
    }

    function FetchCurrentItemIdSucceeded(){
        //Write the code here which was originally in the 'Suceeded' function.
    }

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.