How to get specific record in a List

I have a list of transactions interms of record i want to iterate the List to get specific transaction

this is my contract

`   contract AccountContract=
            record user_transactin={
                         transaction_id:int,
                          tran_owner: address,
                         amount:int,
                        
                         
               }
            record user_account = {
                            id :int,
                           acc_name:string,
                           history: list(user_transaction) //what i want to find single transaction from this list and update the transaction amount
             }




`
1 Like

Hi,
as I understood, you want to update some field inside some record on the list, right?
Maybe something like this will solve your problem:

include "List.aes"                                                                                                                                                                             
                                                                                                                                                                                               
contract AccountContract =                                                                                                                                                                     
  record user_transaction = {                                                                                                                                                                  
      transaction_id:int,                                                                                                                                                                      
      tran_owner: address,                                                                                                                                                                     
      amount:int                                                                                                                                                                               
    }                                                                                                                                                                                          
  record user_account = {                                                                                                                                                                      
      id :int,                                                                                                                                                                                 
      acc_name:string,                                                                                                                                                                         
      history: list(user_transaction)                                                                                                                                                          
    }                                                                                                                                                                                          
  function get_transaction_by_id(id : int, transactions : list(user_transaction)) =                                                                                                            
    List.find((t) => t.transaction_id == id, transactions)                                                                                                                                     
                                                                                                                                                                                               
  function update_amount_by_id(id : int, amount : int, transactions : list(user_transaction)) =                                                                                                
    let (my_ts, rest) = List.partition((t) => t.transaction_id == id, transactions)                                                                                                            
    switch(my_ts)                                                                                                                                                                              
      [] => abort("no such id!")                                                                                                                                                               
      [my_t] => my_t{amount = amount}::rest                                                                                                                                                    
      _ => abort("id not unique!")
1 Like

Big up i like your solution , sorry!! And how if i want to delete such transaction by ID

Maybe the filter function will help? The usage is similar to partition. The code would be something like List.filter((t) => id /= t.transaction_id, transactions)

ok let me try to implement it