-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathconvert_estimate_to_salesorder_automatically.ds
More file actions
168 lines (142 loc) · 4.57 KB
/
Copy pathconvert_estimate_to_salesorder_automatically.ds
File metadata and controls
168 lines (142 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
This is a sample Custom Function written to be used in Workflow automation of Zoho Books.
This custom function converts accepted Estimate to Sales order automatically
Create a connection by the name "zbooks" for Zoho Books service.
Module - Estimate
Workflow Type - Event Based
When Estimate is - Created or Edited
Filter the triggers - When Estimate Status is Accepted
Actions - Type: Custom function; Name: Choose the custom function that you have created
*/
estimateID = estimate.get("estimate_id");
organizationID = organization.get("organization_id");
customerID = estimate.get("customer_id");
dc = organization.get("data_center_extension");
apiUrl = "https://www.zohoapis" + dc + "/books/v3/";
estimateDetails = zoho.books.getRecordsByID("Estimates", organizationID, estimateID, "zbooks").get("estimate");
isDiscountBeforeTax = estimateDetails.get("is_discount_before_tax");
cpList = estimateDetails.get("contact_persons").toList();
branchId = estimateDetails.get("branch_id");
estimateNumber = estimateDetails.get("estimate_number");
discountType = estimateDetails.get("discount_type");
discount = estimateDetails.get("discount");
shippingCharge = estimateDetails.get("shipping_charge");
shippingChargeTaxId = estimateDetails.get("shipping_charge_tax_id");
adjustment = estimateDetails.get("adjustment");
salespersonID = estimateDetails.get("salesperson_id");
lineItems = estimateDetails.get("line_items").toList();
lineItemsList = List();
for each item in lineItems
{
itemCustomFields = item.get("item_custom_fields").toList();
itemCF = List();
for each itemField in itemCustomFields
{
itemCFMap = Map();
itemCFMap.put("label", itemField.get("label"));
itemCFMap.put("value", itemField.get("value"));
itemCF.add(itemCFMap);
}
item.remove("item_custom_fields");
item.remove("custom_field_hash");
item.remove("line_item_id");
item.put("item_custom_fields", itemCF);
lineItemsList.add(item);
}
salesOrderCustomFields = invokeurl
[
url: apiUrl + "settings/fields?entity=salesorder&organization_id=" + organizationID
type: GET
connection: "zbooks"
];
salesOrderCustomFields = salesOrderCustomFields.get("fields").toList();
estimateCustomFields = estimateDetails.get("custom_fields").toList();
cfList = List();
for each estimateField in estimateCustomFields
{
estimateCF_isActive = estimateField.get("is_active");
estimateCF_dataType = estimateField.get("data_type");
estimateCF_label = estimateField.get("label");
estimateCF_value = estimateField.get("value");
for each salesOrderField in salesOrderCustomFields
{
salesOrderCF_isActive = salesOrderField.get("is_active");
salesOrderCF_dataType = salesOrderField.get("data_type");
salesOrderCF_label = salesOrderField.get("field_name_formatted");
if(estimateCF_isActive && salesOrderCF_isActive
&& estimateCF_dataType == salesOrderCF_dataType
&& estimateCF_label == salesOrderCF_label
&& !estimateCF_value.isNull()
)
{
cMap = Map();
cMap.put("label", salesOrderCF_label);
cMap.put("value", estimateCF_value);
cfList.add(cMap);
}
}
}
json = Map();
json.put("customer_id", customerID);
json.put("estimate_id", estimateID);
json.put("branch_id", branchId);
if(discountType == "entity_level")
{
json.put("discount", discount);
json.put("is_discount_before_tax", isDiscountBeforeTax);
json.put("discount_type", "entity_level");
}
else if(discountType == "item_level")
{
json.put("is_discount_before_tax", isDiscountBeforeTax);
json.put("discount_type", "item_level");
}
json.put("reference_number", estimateNumber);
json.put("line_items", lineItemsList);
if(!salespersonID.isNull())
{
json.put("salesperson_id", salespersonID);
}
if(!cpList.isEmpty())
{
json.put("contact_persons", cpList);
}
if(!cfList.isEmpty())
{
json.put("custom_fields", cfList);
}
if(shippingCharge != 0)
{
json.put("shipping_charge", shippingCharge);
json.put("shipping_charge_tax_id", shippingChargeTaxId);
}
if(adjustment != 0)
{
json.put("adjustment", adjustment);
}
params = Map();
params.put("JSONString", json);
createSO = invokeurl
[
url: apiUrl + "salesorders?organization_id=" + organizationID
type: POST
parameters: params
connection: "zbooks"
];
info createSO.get("message");
soCode = createSO.get("code");
if(soCode != 0)
{
cmt = Map();
cmt.put("description", "Failed to create a salesorder");
cmtMap = Map();
cmtMap.put("JSONString", cmt);
comment = invokeurl
[
url: apiUrl + "estimates/" + estimateID + "/comments?organization_id=" + organizationID
type: POST
parameters: cmtMap
connection: "zbooks"
];
info "cmt==" + comment;
}