-
Notifications
You must be signed in to change notification settings - Fork 13
/
subscription_refer_and_earn_workflow.ds
129 lines (101 loc) · 3.54 KB
/
subscription_refer_and_earn_workflow.ds
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
/*
This is a sample Custom Function written to be used in Workflow automation of Zoho Subscriptions,
a recurring billing and subscriptions management software.
Module - Subscription
Predefined Event - New Subscription.
Place your {{connection name}} in "YOUR_CONNECTION_NAME" variables.
* This custom function raises a credit note for the referrer with a fixed incentive amount.
* Search the customer with the email address provided by the referred customer.
* Raise a credit note for the searched customer.
*/
organizationID = organization.get("organization_id");
domain = "https://www.zohoapis.com/billing/v1";
customer = subscriptions.get("customer");
customerCustomerFields = customer.get("custom_fields");
// -- Paste the label of your Custom Field shown in Hosted Page.
customFieldLabel = "";
referredCustomerName = customer.get("display_name");
for each customField in customerCustomerFields
{
if(customField.get("label") == customFieldLabel)
{
referrerEmail = customField.get("value");
break;
}
}
if(referrerEmail != null && !isBlank(referrerEmail))
{
params = "&response_option=0" + "&email_contains=" + referrerEmail + "&organization_id=" + organizationID;
request = "Searching referrer Customer";
response = invokeUrl [
url : domain + "/api/v1/customers?page=1&per_page=10" + params
type : GET
connection : "YOUR_CONNECTION_NAME"
];
if(response.get("code") == 0)
{
customers = response.get("customers");
paramsMap = Map();
if(customers.size() > 0)
{
customer = customers.get(0);
customerID = customer.get("customer_id");
// -- Fill the incentive which you would like to award, say "10.00"
referrerBonus = "";
// -- Paste the Account ID, to which this transaction is to be mapped, say Discount Account ID.
accountID = "";
description = "This is a incentive for referring the customer " + referredCustomerName;
creditNoteDate = zoho.currentdate.toString("yyyy-MM-dd");
creditNoteItemList = list();
creditNoteItem = Map();
creditNoteItem.put("quantity","1");
creditNoteItem.put("price",referrerBonus);
creditNoteItem.put("description",description);
creditNoteItem.put("account_id",accountID);
creditNoteItemList.add(creditNoteItem);
paramsMap.put("customer_id",customerID);
paramsMap.put("date",creditNoteDate);
paramsMap.put("creditnote_items",creditNoteItemList);
url = domain + "/api/v1/creditnotes?organization_id=" + organizationID;
request = "Creating Credit Note";
response = invokeUrl [
url : url
type : POST
parameters : paramsMap.toString()
connection : "YOUR_CONNECTION_NAME"
];
if(response.get("code") != 0)
{
errorMessage = response.get("message");
sendmail
[
from :zoho.adminuserid
to :zoho.adminuserid
subject :"Error occured in Referrer custom function while " + request
message :"<b>Affected url :</b><br>" + url + "<br><b>Error Message</b><br>" + errorMessage
]
}
}
else
{
sendmail
[
from :zoho.adminuserid
to :zoho.adminuserid
subject :"Error occured in Referrer custom function."
message :"Referreral Customer with Email ID " + referrerEmail + " is not found."
]
}
}
else
{
errorMessage = response.get("message");
sendmail
[
from :zoho.adminuserid
to :zoho.adminuserid
subject :"Error occured in Referrer custom function while " + request
message :"<b>Affected url :</b><br>" + url + "<br><b>Error Message</b><br>" + errorMessage
]
}
}