How to get last Sharepoint list item by using lookup function

  • 28 November 2017
  • 4 replies
  • 10 views

Badge +2

Hi 

How we can return last sharepoint list item by using lookup function, I am using given below lookup but it is return value for first list item , not the value of last list item.  

lookup("TestList","Title",Title,"Created" )


4 replies

Userlevel 6
Badge +13

That formula will return the first item in your list where the "Title" column matches the Title control.

Are you saying that your lookup should return multiple results and that you want to return the last result? Or are you trying to simply lookup the last created item in that list?

Badge +2

Last created item in the list

Badge +9

Try the below code.

NWF$(document).ready(function()
{
NWF$('.title input').blur(function() //call function on change of title value
{
retrieveItemIdMax();
});
});
function retrieveItemIdMax()
{
var title = NWF$('.title input').val();
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('Test Request Number');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View><Query><OrderBy><FieldRef Name='ID' Ascending='False'/></OrderBy><Where><Eq><FieldRef Name='Currency'/><Value Type='Text'>"+title+"</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>"); //ordering by Id and limiting query result to one row.
window.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededNumber), Function.createDelegate(this, this.onQueryFailedNumber));
}
function onQuerySucceededNumber(sender, args)
{
if(collListItem)
{
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext())
{
var oListItem = listItemEnumerator.get_current();
var number = oListItem.get_item('ID'); //get item id value
NWF$('.cal').text(number); //set value to calculated field
}
}
}
function onQueryFailedNumber(sender, args) {
alert('Request failed. ' + args.get_message() + ' ' + args.get_stackTrace());
}

211085_pastedImage_1.png

Userlevel 5
Badge +14

with use of lookup() function you can get date of latest created item as follows

- create a helper list field that will flag items that should be consider in max date evaluation (if you already have such a field you can you use for sure existing one). if you need to take all the items into account, then you will need to set a value for each item in this field.

in my example below I created Yes/No field named 'CheckMax'

- add following helper assignment to form settings > Custom javascript

jsDateObj = Date;

- create calculated value control with following formula

new jsDateObj(max(lookup('testlist','CheckMax',true,'Created',true)))

212140_pastedImage_2.png

Reply