Hello! We have the following custom function run every time a sales order is executed or edited to calculate the gross profit. However, we get the error "Custom Function calculategrossprofit failed to execute for the workflow calculateGP".
I've created many workflows and everything with the workflow setup looks correct. Any advice would be greatly appreciated, thank you in advance!
salesorderID = salesorder.get("salesorder_id");
salesorderdate = salesorder.get("date").toDate();
organizationID = organization.get("organization_id");
customerID = salesorder.get("customer_id");
soDetails = invokeurl
[
url :"https://books.zoho.com/api/v3/salesorders/editpage?salesorder_id=" + salesorderID + "&organization_id=" + organizationID
type :GET
connection:"zbookscon1"
];
soDetails = soDetails.get("salesorder");
lineitems = soDetails.get("line_items").toList();
totalProfit = 0;
totalPurchase = 0;
iList = List();
for each find in lineitems
{
imap = Map();
rate = find.get("rate");
itemTotal = find.get("item_total").toDecimal();
purchaseRate = find.get("purchase_rate");
itemCFList = List();
itemCF = find.get("item_custom_fields");
for each findCF in itemCF
{
itemLab = findCF.get("label");
itemVal = findCF.get("value");
itemMap = Map();
itemMap.put("label",itemLab);
itemMap.put("value",itemVal);
itemCFList.add(itemMap);
}
if(purchaseRate.isNull())
{
purchaseRate = 0;
}
else
{
purchaseRate = purchaseRate.toDecimal();
}
info purchaseRate;
quantity = find.get("quantity");
newRate = purchaseRate
* quantity;
margin = itemTotal - newRate;
cMap = Map();
cMap.put("label","Gross Profit");
//Replace Gross Profit with item custom field
cMap.put("value",margin);
itemCFList.add(cMap);
cMap = Map();
cMap.put("label","Cost Price");
//Replace Cost Price with item custom field
cMap.put("value",newRate);
itemCFList.add(cMap);
find.remove("item_custom_fields");
find.put("item_custom_fields",itemCFList);
iList.add(find);
profit = itemTotal - newRate;
totalPurchase = totalPurchase + newRate;
totalProfit = totalProfit + profit;
}
json = Map();
json.put("customer_id",customerID);
json.put("line_items",iList);
params = Map();
params.put("JSONString",json);
update = invokeurl
[
url :"https://books.zoho.com/api/v3/salesorders/" + salesorderID + "?organization_id=" + organizationID
type :PUT
parameters:params
connection:"zbookscon1"
];
info update.get("message");