Test Out Your Subscriptions Custom Function

Check out our code below!
// Get the subscription ID for populating the URL in the CRM Record
subscriptionId = subscriptions.get("subscription_id");
subscriptionUrl = "https://subscriptions.zoho.com/app#/subscriptions/" + subscriptionId;
// We need the Customer email to find the Customer's record in Zoho CRM, if it exists.
customerEmail = subscriptions.get("customer").get("email");
// If the Customer DOESN'T exist in CRM, we'll need their name and address to create a new Contact.
customerFirstName = subscriptions.get("customer").get("first_name");
customerLastName = subscriptions.get("customer").get("last_name");
customerStreet = subscriptions.get("customer").get("billing_address").get("street");
customerCity = subscriptions.get("customer").get("billing_address").get("city");
customerZip = subscriptions.get("customer").get("billing_address").get("zip");
customerState = subscriptions.get("customer").get("billing_address").get("state");
// We need the Next Billing Date to be able to give the "Expiry" date of the subscription in CRM, so that the staff can see "Hey, your membership is good until..."
nextBillingDate = subscriptions.get("next_billing_at");
planName = subscriptions.get("plan").get("name");
info "Next Billing At: " + nextBillingDate;
// We populate the Contact map with details about the subscription. 
contactMap = Map();
// We put the address in no matter if the Contact exists or is new, because it'll give us a chance to update their Address
// Next, we'll need to create Search parameters to find the CRM Contact
searchParam = "(Email:equals:" + customerEmail + ")";
findCrmContact = zoho.crm.searchRecords("Contacts",searchParam);
// It's possible that the Contact will already exist. This "If" statement effectively says "If the "findCrmContact" is NOT empty, then...
 contactId = findCrmContact.get("0").get("id");
 updateContact = zoho.crm.updateRecord("Contacts",contactId,contactMap);
 info "Updated Contact: " + updateContact;
// This "else" says "Oh, the above condition is wrong? You didn't find any matching Contacts with that email? Well then let's create a new Contact!"
 createContact = zoho.crm.createRecord("Contacts",contactMap);
 contactId = createContact.get("id");
 info "Created Contact: " + createContact;
Back to Lesson