Overwriting descriptions from items on sales order with default descriptions + not updating rate?

222 Views Asked by At

Have a deluge script that runs in Zoho Books/Inventory that calculates some fees based on custom fields in the customer/item modules and a creator table.

Script is working as intended (although a little slower than hoped) but has two final hurdles I'm struggling with.

Issue 1) The script iterates over each line item on a sales order to check to see if it has an entry in the related custom field in the item module then it effectively refreshes the sales order with the line-items and a new line item "recycling fee" with the calculated rate. This would be fine but there is a very specific niche item that requires updated descriptions on every sales order. In this case, the script runs over this and replaces the user-updated description with the default description for the item.

Issue 2) The calculations the script performs work as intended, but, if line item "Recycling Fees" exists on the sales order, nothing happens. If a user deletes the line and saves the sales order it will update as intended.

Fix for 1) I've tried pulling and adding the description from the sales order to 'pdlist' and while this does preserve the user-edited description it strips the item_ids from the sales order so the items on the sales order are just descriptions.

Fix for 2) I tried to assign a variable to false and then check the line items to see if "recycling fee" exists and if so update the rate. This fixed the issue of having multiple "recycling fee" line items added but still won't update the rate if it exists.

I've truncated the script a bit below, should be the relevant portions.

// Fetch SO and customer records
resp = zoho.inventory.getRecordsByID("salesorders",organizationID,salesorderID,"inventory1");
//info resp;
salesorder = resp.get("salesorder");
if(true)
{
    info salesorder;
    //  return;
}
...
//GETTING CUSTOMER/SO SHIPPING INFO HERE//
...
for each  custom_field in customer_custom_fields
{
    if(custom_field.get("label") == "Remitter ID")
    {
        cf_remitter_id = custom_field.get("value");
    }
}
// Set Recycling Fee to 0
Fee = 0;
// Check if Remitter ID is null
if(cf_remitter_id == "")
{
    line_items = salesorder.get("line_items");
    pdlist = List();
    for each  ele1 in line_items
    {
        item_id = ele1.get("item_id");
        itemresp = zoho.inventory.getRecordsByID("items",organizationID,item_id,"inventory1");
        //info itemresp;
        item = itemresp.get("item");
        custom_fields1 = item.get("custom_fields");
        //info custom_fields1;
        cf_recycling_category = "";
        flag = false;
        pro2 = Map();
        pro2.put("item_id",ele1.get("item_id"));
                //pro2.put("description",ele1.get("description"));
        pro2.put("quantity",ele1.get("quantity"));
        pro2.put("rate",ele1.get("rate"));
        //pro.put("total",item_total.toDecimal());
        //pro.put("net_total",item_total.toDecimal());
        pdlist.add(pro2);
        info pro2;
        for each  custom_field1 in custom_fields1
        {
...
//LOOP TO CHECK FEES GOES HERE//
...
    }
    found = false;
    for each  ele1 in line_items
    {
        if(ele1.get("item_id") == "2015797000015488030")
        {
            ele1.put("rate",Fee.toDecimal());
            ele1.put("quantity",1);
            found = true;
            break;
        }
    }
    // Add new line item if it does not exist
    if(found = false)
    {
        pro = Map();
        pro.put("item_id",2015797000015488030);
        pro.put("rate",Fee.toDecimal());
        pro.put("quantity",1);
        // pro.put("description");
        pdlist.add(pro);
        info pro;
    }
    mp = Map();
    mp.put("line_items",pdlist);
    up = zoho.inventory.updateRecord("salesorders",organizationID,salesorderID,mp,"inventory1");
    info up;
}

2

There are 2 best solutions below

5
On

Edit on 2023-01-13:

I have modified the function in the first looping of the line items. Basically, we preserve all the properties/keys of the line item, and remove only line_item_id.

...
if(cf_remitter_id == "")
{
    line_items = salesorder.get("line_items");
    pdlist = List();
    for each  ele1 in line_items
    {
        item_id = ele1.get("item_id");
        itemresp = zoho.inventory.getRecordsByID("items",organizationID,item_id,"inventory1");
        //info itemresp;
        item = itemresp.get("item");
        custom_fields1 = item.get("custom_fields");
        //info custom_fields1;
        cf_recycling_category = "";
        flag = false;
        pro2 = Map();
        /*
        // Instead of manually setting up each key of a line item
        // We will just copy from ele1 to pro2 variable
        // Then remove the line_item_id, as currently we are not able to override line items with that
        pro2.put("item_id",ele1.get("item_id"));
                //pro2.put("description",ele1.get("description"));
        pro2.put("quantity",ele1.get("quantity"));
        pro2.put("rate",ele1.get("rate"));
        //pro.put("total",item_total.toDecimal());
        //pro.put("net_total",item_total.toDecimal());
        */
        pro2 = ele1;
        pro2.remove("line_item_id"); // remove the key
        pdlist.add(pro2);
        info pro2;
        for each  custom_field1 in custom_fields1
        {
...
//LOOP TO CHECK FEES GOES HERE//
...
    }
    found = false;
    for each  ele1 in line_items
    {
...

I am not quite clear with issue #1. Why would you still need the item_id of each line items? Is there any information that you need to preserve but lost while using that function?

For issue #2, you would need to remove the rate first before adding it.

....
}
    found = false;
    for each  ele1 in line_items
    {
        if(ele1.get("item_id") == "2015797000015488030")
        {
            // remove rate
            ele1.remove("rate");
            // then add again
            ele1.put("rate",Fee.toDecimal());
            ele1.put("quantity",1);
            found = true;
            break;
        }
    }
    // Add new line item if it does not exist
    if(found = false)
    {
        pro = Map()
....
0
On

Turns out the issue was that when passing the "description" value it breaks the item link.

In order for this to work without overwriting description / warehouse, had to pass the "warehouse_id" and "description" fields AND include the "line_item_id" as well as the "item_id".

Ended up looking like this:

products = Map();
    products.put("item_id",salesorderitem.get("item_id"));
    products.put("line_item_id",salesorderitem.get("line_item_id"));
    products.put("quantity",salesorderitem.get("quantity"));
    products.put("rate",salesorderitem.get("rate"));
    products.put("description",salesorderitem.get("description"));
    products.put("warehouse_id",salesorderitem.get("warehouse_id"));