Calculate a sum column and save property

Hi I'm new to javascript in ARAS and unfamiliar with the syntax. I have created a new item type called 'purchase order lines' containing some information of manufacturer parts and some additional information. The information is entered manually by the user. It's quantity, price and price_per_unit and should result in a separate column called value as the total value = quanity * (price / price_per_unit). My PO line tab can contain more than one item and the whole column should be calculated whenever there is a change in one of the columns (quantity, price or price_per_unit) and the value property should be updated. I found some tipps in the forum but nothing that works for me. Also I didn't find the OnChange event in the client events tab of the item type. What am I doing wrong? Thanks for the support. Chris
  • Hi Chris, So there's two ways you can go about this. The easiest way would be to add an onBeforeUpdate server event that calculates the total each time that the item is saved. However, this means that the total you want to calculate won't appear until after the user saves the item. If you'd prefer the total to be updated dynamically, you could use code like the sample below from an onEditFinish event on the Properties themselves.
    // Get the item
    var po = this.item.querySelector("#" + relatedID);
    // Get the properties from the item
    var quantity = parseInt(aras.getItemProperty(po, "quantity"));
    var price = parseFloat(aras.getItemProperty(po, "price"));
    var ppu = parseFloat(aras.getItemProperty(po, "price_per_unit"));
    // make sure we have all the pieces to calculate the total
    if (!!quantity && !!price && !!ppu) {
    // Calculate the total
    var total = quantity * (price / ppu);
    // Set the total
    aras.setItemProperty(po, "total", total);
    // Refresh the form so this new value appears
    aras.uiReShowItemEx(this.item.id, this.item);
    }
    Note that this sample code assumes that these properties are being updated from a relationship grid and not from a Form. Chris
    Christopher Gillis Aras Labs Software Engineer
  • Hi Chris Thanks for the sample code. There is only one question. If I try to add the method to the client event in the item type 'purchase order lines' I cannot find the event 'onEditFinish'. All I see is 'onBeforeNew', 'OnNew',  'OnAfterNew' and 'OnShowItem'.  
  • Hi Chris, The onEditFinish event exists on the Properties themselves. You can open the property directly by navigating to the Properties relationship tab of the ItemType, right-clicking on the property you want to open, and selecting View "Properties". Chris
    Christopher Gillis Aras Labs Software Engineer
  • Hi Chris I've modifyied your code to meet my specific filed names and tried both ways - as a client event 'onAfterNew' or 'onShowItem' and as a property event 'onEditFinish'. Neither works. This is what I have:
    // get item ID
    var po_pos_id       = this.item.querySelector("#" + relatedID);
    
    // get item properties
    var po_pos_quantity = parseFloat(aras.getItemProperty(po_pos_id, "pr_pos_quantity"));
    var po_pos_price    = parseFloat(aras.getItemProperty(po_pos_id, "pr_pos_price"));
    var po_pos_ppu      = parseFloat(aras.getItemProperty(po_pos_id, "pr_po_priceper"));
    
    // make sure we have all the pieces to calculate the total
    if (po_pos_quantity !== 0 && !=isNaN(po_pos_quantity) && po_pos_price !== 0 && !=isNaN(po_pos_price) && po_pos_ppu !== 0 && !=isNaN(po_pos_ppu)) {
        // Calculate the total
        var total = po_pos_quantity * (po_pos_price / po_pos_ppu);
        
        // Set the total
        aras.setItemProperty(po_pos_id, "pr_pos_value", total);
        
        // Refresh the form so this new value appears
        aras.uiReShowItemEx(this.item.id, this.item);
    }
    When I added the code to the property event I couldn't use the input grid anymore. Whenever I tried to jump from one field to another the form seemed busy and didn't allow me to change the information once something was entered, mostly 'NaN' for number fields. I have an item type called purchase_order. This item contains a related item type called po_lines which should contain single items with a description, quantity and so on. Therefore I think your suggested solution should work as this is in a relationship grid, shouldn't it? I'm doing something wrong, but I have no idea what it is. Chris
  • Hi Chris, Could you let us know what version of Aras Innovator you are using and whether po_lines is the name of the RelationshipType or the related ItemType? Chris
    Christopher Gillis Aras Labs Software Engineer
  • Hi Chris We're using Version 11.0 SP12 Build: 6920 My item type is called pr_PO_position and the respecive relationship item type is called pr_PO_pos. At least, that's how I understood ARAS. Chris