FSM work order creation on books quote approval

FSM work order creation on books quote approval

in order to create a work order in FSM based on a books quote being approved.  

I have changed the connection name to the one that connects my FSM and books but get the following error:

Check and update the code in line 27 as there is a Exception : Invalid JSON Format String



estimateID = estimate.get("estimate_id");
estimatedate = estimate.get("date").toDate();
organizationID = organization.get("organization_id");
resp = zoho.books.getRecordsByID("Estimates",organizationID,estimateID);
estimateResp = resp.get("estimate").toMap();
bl_address = estimateResp.get("billing_address").toMap().get("address");
bl_city = estimateResp.get("billing_address").toMap().get("city");
bl_country = estimateResp.get("billing_address").toMap().get("country");
bl_state = estimateResp.get("billing_address").toMap().get("state");
bl_zip = estimateResp.get("billing_address").toMap().get("zip");
sh_address = estimateResp.get("shipping_address").toMap().get("address");
sh_city = estimateResp.get("shipping_address").toMap().get("city");
sh_country = estimateResp.get("shipping_address").toMap().get("country");
sh_state = estimateResp.get("shipping_address").toMap().get("state");
sh_zip = estimateResp.get("shipping_address").toMap().get("zip");
summary = estimateResp.get("estimate_number");
due_date = estimateResp.get("date");
books_customer_id = estimateResp.get("contact_persons_details").toMap().get("contact_person_id");
con_response = invokeurl
[
type :GET
connection:"fsmconnection"
];
ContatsRespMap = con_response.get("data").toMap();
id = ContatsRespMap.get("id");
conDetails = zoho.fsm.getRecordById("Contacts",id);
data = conDetails.get("data");
Service_Address = data.toMap().get("Service_Address");
Billing_Address = data.toMap().get("Billing_Address");
Billing_Address_Id = Billing_Address.toMap().get("id");
Service_Address_Id = Service_Address.toMap().get("id");
service_Address_map = {"id":Service_Address_Id,"Service_Street_1":sh_address,"Service_Street_2":null,"Service_City":sh_city,"Service_State":sh_state,"Service_Country":sh_country,"Service_Zip_Code":sh_zip};
Billing_Address_map = {"id":Billing_Address_Id,"Billing_Street_1":bl_address,"Billing_Street_2":null,"Billing_City":bl_city,"Billing_State":bl_state,"Billing_Country":bl_country,"Billing_Zip_Code":bl_zip};
line_items = estimateResp.get("line_items").toList();
i = 1;
j = 1;
Service_Line_Items_List = list();
Part_Line_Items_List = list();
Sub_total = 0;
Tax_Amount = 0;
Grand_Total = 0;
tax = Map();
for each  line_item in line_items
{
item_id = line_item.toMap().get("item_id");
type = zoho.books.getRecordsByID("Items",organizationID,item_id).get("item").toMap().get("product_type");
if(type == "service")
{
Service_Line_Items_Map = Map();
line_item_name = line_item.toMap().get("name");
quantity = line_item.toMap().get("quantity");
tax_name = line_item.toMap().get("tax_name");
tax_id = line_item.toMap().get("tax_id");
tax_percentage = line_item.toMap().get("tax_percentage");
tax = {"Tax_Name":tax_name,"Tax_Exemption_Code":null,"Tax_Id":tax_id,"Tax_Percentage":tax_percentage,"Tax_Exemption_Id":null};
rate = line_item.toMap().get("rate");
ServiceResp = invokeurl
[
type :GET
connection:"fsmconnection"
];
ServiceRespMap = ServiceResp.toMap().get("data").toMap();
service_id = ServiceRespMap.get("id");
Service_Line_Items_Map.put("Service",service_id);
Service_Line_Items_Map.put("Quantity",quantity);
Service_Line_Items_Map.put("List_Price",rate);
Line_Item_Amount = quantity.toDecimal() * rate.toDecimal();
Sub_total = Sub_total + Line_Item_Amount;
Service_Line_Items_Map.put("Tax",tax);
Service_Line_Items_Map.put("Line_Item_Amount",Line_Item_Amount);
Service_Line_Items_Map.put("Sequence",i);
Service_Line_Items_Map.put("Contact",id);
i = i + 1;
Service_Line_Items_List.add(Service_Line_Items_Map);
}
else
{
Part_Line_Items_Map = Map();
line_item_name = line_item.toMap().get("name");
quantity = line_item.toMap().get("quantity");
tax_name = line_item.toMap().get("tax_name");
tax_id = line_item.toMap().get("tax_id");
tax_percentage = line_item.toMap().get("tax_percentage");
tax = {"Tax_Name":tax_name,"Tax_Exemption_Code":null,"Tax_Id":tax_id,"Tax_Percentage":tax_percentage,"Tax_Exemption_Id":null};
rate = line_item.toMap().get("rate");
ServiceResp = invokeurl
[
type :GET
connection:"fsmconnection"
];
ServiceRespMap = ServiceResp.toMap().get("data").toMap();
service_id = ServiceRespMap.get("id");
Part_Line_Items_Map.put("Part",service_id);
Part_Line_Items_Map.put("Quantity",quantity);
Part_Line_Items_Map.put("List_Price",rate);
Line_Item_Amount = quantity.toDecimal() * rate.toDecimal();
Sub_total = Sub_total + Line_Item_Amount;
Part_Line_Items_Map.put("Line_Item_Amount",Line_Item_Amount);
Part_Line_Items_Map.put("Tax",tax);
Part_Line_Items_Map.put("Sequence",j);
j = j + 1;
Part_Line_Items_List.add(Part_Line_Items_Map);
}
}
info Service_Line_Items_List;
temp_ser_line_item = Service_Line_Items_List.get(0).toMap();
temp_ser_line_item.put("Part_Line_Items",Part_Line_Items_List);
new_serv_list = list();
k = 0;
for each  serv_list_merge in Service_Line_Items_List
{
if(k == 0)
{
new_serv_list.add(temp_ser_line_item);
k = k + 1;
}
else
{
new_serv_list.add(serv_list_merge);
}
}
Tax_Amount = Sub_total * tax_percentage * 0.01;
Grand_Total = Sub_total + Tax_Amount;
terrResp = zoho.fsm.getRecords("Territories");
terrId = terrResp.get("data").toMap().get("id");
//Create work order
createMap = Map();
createMap.put("Summary",summary);
createMap.put("Due_Date",due_date);
createMap.put("Contact",id);
createMap.put("Type","Service");
createMap.put("Billing_Address",Billing_Address_map);
createMap.put("Service_Address",service_Address_map);
createMap.put("Territory",terrId);
createMap.put("Service_Line_Items",new_serv_list);
createMap.put("Tax_Amount",Tax_Amount);
createMap.put("Grand_Total",Grand_Total);
response = zoho.fsm.createRecord("Work_Orders",createMap);
info response;