select multiple rows in SharePoint and add the amount in amount column in Js using classic experience.

Nadda Muhammad 60 Reputation points
2023-08-05T04:17:55.83+00:00

I have a list name sub expense. when I select the multiple rows and click add selected button it should add the amount of selected rows and display the amount on another SharePoint list name expense with some default value. for example title will be expense name, type etc.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,229 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,577 questions
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 40,446 Reputation points Microsoft External Staff
    2023-08-07T09:11:24.1933333+00:00

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.