Hi @Nadda Muhammad,
Per my test, I can use following code to count the items selected and caculate the total amt then create a new item in another list.
<script src="https://code.jquery.com/jquery-2.2.4.js" type="text/javascript"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script>
function getselected(){
var ctx = SP.ClientContext.get_current();
var items = SP.ListOperation.Selection.getSelectedItems(ctx);
//alert (Object.keys(items).length);
console.log(Object.keys(items).length);
var count = Object.keys(items).length;
var ids = [];
items.forEach((item) => {
var itemid = item.id;
console.log(itemid);
ids.push(itemid)
});
var web = ctx.get_web();
var list = web.get_lists().getByTitle('ListTest11');
var result = []; //for storing items
ids.forEach(function(id){
var item = list.getItemById(id);
ctx.load(item);
result.push(item);
});
ctx.executeQueryAsync(
function() {
result.forEach(function(item){
var amt = 0;
amt += item.get_item('Amt');
console.log(amt);
// Specify list title here
var oList = web.get_lists().getByTitle("ListTest12");
// Get Item using CAML Query
var camlQuery = new SP.CamlQuery();
// New "ListItemCreationInformation" Object
var oListItemCreationInformation = new SP.ListItemCreationInformation();
var oListItem = oList.addItem(oListItemCreationInformation);
// Set value for each column here
oListItem.set_item('AmtTotal', amt);
oListItem.set_item('Count', count);
oListItem.update();
ctx.load(oListItem);
// Execute the query to the server.
ctx.executeQueryAsync(onsuccess, onfailed);
});
},
function(sender,args){
console.log(args.get_message());
}
);
}
function onsuccess() {
console.log("Success");
}
function onfailed(sender, args) {
console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
<button onclick="getselected()" type="button">add selected</button>
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.