Unable to create custom function - Please check the screenshot for the details
HERE IS THE FUNCTION!!
try
{
// 1. Fetch the details of the approved Bill using its ID
billDetails = zoho.books.getRecordById("Bills", organization.get("organization_id"), billId);
// Check if bill details were successfully fetched and if it contains line items
if (billDetails.get("code") == 0 && billDetails.get("bill") != null && billDetails.get("bill").get("line_items") != null)
{
lineItems = billDetails.get("bill").get("line_items");
// 2. Iterate through each line item in the Bill
for each lineItem in lineItems
{
itemID = lineItem.get("item_id");
itemRate = lineItem.get("rate"); // This is the purchase rate for the item on this specific bill line
itemType = lineItem.get("item_type"); // To ensure we only update 'goods' (inventory) items
// Ensure itemID and itemRate are valid and it's an inventory item ("goods")
if (itemID != null && itemRate != null && itemRate > 0 && itemType == "goods")
{
// 3. Prepare a map with the new purchase_price for the item
itemUpdateMap = Map();
// 'purchase_price' is the API field name for the 'Cost Price' field in the Zoho Books UI
itemUpdateMap.put("purchase_price", itemRate);
// 4. Update the item's master record using the Zoho Books API
updateResponse = zoho.books.updateRecord("Items", organization.get("organization_id"), itemID, itemUpdateMap);
// Check if the update was successful
if (updateResponse.get("code") == 0)
{
info "Successfully updated Purchase Price for Item ID " + itemID + " to " + itemRate + ".";
}
else
{
errorMessage = "Failed to update Purchase Price for Item ID " + itemID + ": " + updateResponse.get("message");
// error errorMessage;
}
}
else if (itemType != "goods")
{
info "Skipping non-goods item (ID: " + itemID + ", Type: " + itemType + ") from Bill ID " + billId;
}
else
{
info "Skipping line item with invalid itemID or rate (ID: " + itemID + ", Rate: " + itemRate + ") from Bill ID " + billId;
}
}
}
else
{
info "No line items found or bill details could not be fetched for Bill ID: " + billId;
}
}
catch (e)
{
//error "An error occurred in updateItemPurchasePriceOnBillApproval function for Bill ID " + billId + ": " + e;
}