Updating total in List and library control

Hi I would like to multiply a numeric field and lookup field upon adding the record. i can do this with normal record (as you see in the case below, where QTY * Item Value is the total. But i would like to multiply the QTY field with Hardware:Price.

Please see the code below
window.myFunction = function() {
//if (confirm('Are you sure you wish to add this item to Cart?')) {
var myItem = fd.field('Item').value.LookupValue;
var myValue = fd.field('ItemValue').value;
var myQty = fd.field('QTY').value;
var myId = fd.field('Item').value.LookupId;
var myItemCost = myValue * myQty;
var list = pnp.sp.web.lists.getByTitle("ITD-EFS-030A Hardware Details");
var id = list.items.add({
HardwareId: myId,
QTY: myQty,
EstimatedCost: myValue,
Total: myItemCost
}).then(function(){
//console.log("Updated!");
// alert(id);
fd.control('SPDataTable1').refresh();
// list.items.
});
}

Hello @Rinu,

As I understand, Item field is a lookup field, and the source list stores the price of the item. Is that correct?

If so, you can retrieve the value of Price field from the source list when selecting the item. For this, you need to specify the internal name of Price field in Item field Extra Fields property:
image

And to retrieve the value of the extra field, use this code:

fd.field('Item').value.Price

You can find more information in Configure cascading lookups article and in this post.

1 Like

exactly what i was after thanks.

1 Like

Hi Mnikitina,

When i attempt to edit rows throws an error. "TypeError: Cannot read property 'value' of undefined".
What I am trying to do is when you edit the "QTY" column from the list and library control. The "Total" should be updated by multiplying with the "Price". (ie QTY * Hardware:Price, where price is a lookup column and i use editData.field('Hardware').value.Price; to get the value, not sure if this is the right way)

now if you edit Total will become null.

please see the code on edit event.

fd.field('Item').$on('change', Reset);

  fd.control('SPDataTable1').$on('edit', function(editData) {
    
    editData.field('QTY').$on('change', function () {
    if (editData.field('QTY').value != 0) {
        editData.field('Total').value = editData.field('QTY').value * editData.field('Hardware').value.Price;
    }
  });

  editData.field('EstimatedCost').$on('change', function () {// fd.field('Item').value.Price
    if (editData.field('EstimatedCost').value != 0) {
        editData.field('Total').value = editData.field('QTY').value * editData.field('Hardware').value.Price;
    }
});

Hello @Rinu,

If you want to get extra values of the lookup field in inline editing mode in List or Library control, you need to use PnPjs. Like this:

fd.control('SPDataTable1').$on('edit', function(editData) {

    editData.field('QTY').$on('change', function () {
        if (editData.field('QTY').value != 0) {
            var hardwareID = editData.field('Hardware').value.LookupId;
             //getting the price of the hardware
             pnp.sp.web.lists.getByTitle("Agent list").items.getById(hardwareID).get().then(function(item){
                //calcilate total
                editData.field('Total').value = editData.field('QTY').value * item.Price;
             });
        }
    });

});
1 Like