I have a SharePoint 2010 Parent/Child list setup using a lookup field to join both the lists, this is working well.
The Parent list contains requisition requests and the child list contains related item details. Each of the related child list items has a 'Line Item Total' field. I need to aggregate the Line Item Totals in the child list and place the result in a 'Requisition Total' in the parent. So for any given parent record the sum of it's related child 'Line Item Totals' is reflected in the 'Requisition' total in the parent row.
I am struggling with the JavaScript code below. I am trying to aggregate Line Item Totals for a given Requisition#? Could someone please review and advise?
<script language="javascript" src="/Purchasing/Documents/jquery-1.8.2.js" type="text/javascript"></script>
<script language="javascript" src="/Purchasing/Documents/jquery.SPServices-2014.01.min.js" type="text/javascript"></script>
<script>
$(document).ready(function()
{
$('select[title=Requisition#]').children().attr('disabled',true);
$("input[title='Old Amount']").attr("readonly","true").css('background-color','#F6F6F6');
// Retrieve the name of the selected parent item.
var strParent=$("select[title$='Requisition#'] :selected").text();
splist li = web.lists["ReqDetails"]; // This is the child list
spquery q = new spquery();
q.Query= "<Where>" +
"<And>" +
"<Neq>" +
"<FieldRef Name='ID'/><Value Type='Number'>0</Value>" +
"</Neq>" +
"<eq>" +
"<FieldRef Name='LinkID'/><Value Type='Number'>ReqID</Value>" +
"</eq>" +
"<And>" +
"</Where>" +
"<OrderBy>" +
"<FieldRef Name='LineItemTotal'/>" +
"</OrderBy>"
q.ViewFields = string.Concat("<FieldRef Name='LineItemTotal' />");
q.ViewFieldsOnly = true; // Fetch only the data that we need
splistitemcollection items = li.getitems(q);
foreach(SPListItem item in items)
{
sum += parseInt(item["LineItemTotal"], 10);
}
alert(sum)
});
</script>