Link asset to sales order based on customer

Hello, I would like to add a field in sales order for asset name. When I enter the customer name on a new sales order, I would like the asset name field to act as a select with only the assets that are owned by the customer I entered. It should be listed by the entered asset name on the assets module. I have tried with fetch but it displays all assets and the select is by the naming series of the assets and NOT asset name. I assume it also needs a ‘Depends On’ for the customer field==asset owner but not sure how to go about it.

For example, customer “Bob” owns 3 different assets; “Asset1”, “Asset2”, and “Asset4”.
“Asset3” is owned by customer “Dave”.
When I enter Bob on a new sales order, the Asset Name field should become a select with only options for Asset1, Asset2, and Asset4.
If I enter Dave, only Asset3 should be available for select.

In the end I would like to link the location to the sales order as well and update the selected asset’s location when a sales order is submitted

Thanks in advance!

1 Like

you need to add a filter to the said asset link field, with customer name as the filter criteria,

below is a reference post on how to do that

Thank you I used that code and swapped in my field names and it worked!

frappe.ui.form.on("Sales Order", "refresh", function(frm) {
    cur_frm.set_query("asset_name", function() {
        return {
            "filters": {
                "customer": cur_frm.doc.customer
            }
        };
    });
});

As for the second part of the task, how would I link the location from a sales order to update the asset’s location? The idea is to skip the “Asset Movement” step and have the asset’s location automatically update when a sales order is submitted.

not clear about the second part since i never worked with sales order/assets in erpnext.

where is the location saved which you want to update into sales order? inside the linked asset doc?
if yes, you can simply set that fields “fetch from” to asset link field or code in a add_fetch

There is a “Location List” and an “Asset List”. Our Location List is filled with cities and postal codes. It is already linked to asset and asset movement. I have a link field in “Sales Order” to select a location which works as it should.

From the original example, if I create a sales order for “Bob” and select “Asset1” from the custom field I used the above code for, I would like the “location” field on “Asset1” in the Asset List to automatically update to the location entered on the new sales order.

You can do this currently by clicking “Transfer Asset” which is linked to Asset Movement.
The plan is to skip this step and have some type of trigger in Assets to update the location of an asset when a new sales order is submitted.

You can use add_fetch on the location field in the sales order to get this done and use the asset field change as trigger. So whenever u change the asset field, the location will get updated in the sales order. Remember to refresh the field to see the updated value right away.

I understand what you are saying but the idea is to update the location field in Asset, not Sales Order. The location field in Sales Order should be blank. It is a link field to select the NEW location for the Asset. I would like for the NEW location in the Sales Order to automatically update the location field in the Asset module when the sales order is submitted (“on submit”)

I still have not been able to do it successfully. I have tried to add this code to the “update_stock_movement” and “transfer_asset” portions of the “asset.py” file.

frappe.db.sql(""" select delivery_location
    from `tabSales Order`
    inner join `tabAsset`
    on `tabSales Order`.truck_number = `tabAsset`.naming_series
    order by `tabSales Order`.delivery_date desc limit 1""")

But I am not sure exactly where to put it and if I need to change anything else that conflicts with that statement