Automatically creates and assigns a Reporting Tag to an Invoice Line Item, if there is an associated Project. Can be adapted to work on other modules.
This function relies heavily on the Zoho Books API. To access this API, we need to create a Custom Connection to Zoho Books. Here are the steps you need to take to create this Custom Connection:
Zoho Books API
https://books.zoho.com
https://deluge.zoho.com/delugeauth/callback
Zoho Books
zoho_books
oAuth2
Header
Authorization Code
[YOUR_CLIENT_ID]
[YOUR_CLIENT_SECRET]
https://accounts.zoho.com/oauth/v2/auth?
https://accounts.zoho.com/oauth/v2/token?
https://accounts.zoho.com/oauth/v2/token?
Zoho Books
zoho_books
ZohoBooks.fullaccess.all
(Note: Be sure to click the ‘+’ for the scope)UNCHECKED
1. Go to the custom function you previously created.
2. Input the following:
organizationId = organization.get("organization_id"); response = invokeUrl [ url : "https://books.zoho.com/api/v3/invoices?organization_id=" + organizationId type : GET connection : "zoho_books" ]; info response;
3. Run the script and make sure you receive a response that is not an error.
// Setup Invoice variables INVOICE_ID = invoice.get("invoice_id"); ORG_ID = organization.get("organization_id"); searchFullInvoice = zoho.books.getRecordsByID("Invoices",ORG_ID,INVOICE_ID); fullInvoice = searchFullInvoice.get("invoice"); lineItems = fullInvoice.get("line_items"); // // Get all reporting tags in organization via Zoho Books API reportingTagsResponse = invokeurl [ url :"https://books.zoho.com/api/v3/settings/tags?organization_id=" + ORG_ID type :GET connection:"zoho_books" ]; reportingTags = reportingTagsResponse.get("reporting_tags"); // // Create map of Reporting Tag IDs, keyed by tag name for lookup reportingTagsMap = Map(); for each reportingTag in reportingTags { tagName = reportingTag.get("tag_name"); tagId = reportingTag.get("tag_id"); reportingTagsMap.put(tagName,tagId); } taggedLineItemsMap = Map(); // // Loop over Line Items for each lineItem in lineItems { lineItemId = lineItem.get("line_item_id"); projectName = lineItem.get("project_name"); // // Check if item has project if(!isNull(projectName)) { // Check if tag currently exists, if not, create a new one if(!reportingTagsMap.containKey(projectName)) { // Create new tag newTagMap = {"JSONString":{"tag_name":projectName,"tag_options":{{"tag_option_name":projectName,"is_active":true}}}}; createTag = invokeurl [ url :"https://books.zoho.com/api/v3/settings/tags?organization_id=" + ORG_ID type :POST parameters:newTagMap connection:"zoho_books" ]; info createTag; responseCode = createTag.get('code'); // // If Create Tag is sucessfull, add tag data to the reportingTagsmap if(responseCode == 0) { reportingTag = createTag.get("reporting_tag"); tagName = reportingTag.get("tag_name"); tagId = reportingTag.get("tag_id"); reportingTagsMap.put(tagName,tagId); } } tagName = projectName; tagId = reportingTagsMap.get(tagName); // // Get full reporting tag reportingTagResponse = invokeurl [ url :"https://books.zoho.com/api/v3/settings/tags/" + tagId + "?organization_id=" + ORG_ID type :GET connection:"zoho_books" ]; // // Get Tag Option ID. This assumes there is only one tag option per tag. reportingTag = reportingTagResponse.get("reporting_tag"); tagOptions = reportingTag.get("tag_options"); defaultTagOption = tagOptions.get(0); defaultTagOptionId = defaultTagOption.get("tag_option_id"); // // Construct line item update map lineItemUpdateMap = Map(); lineItemUpdateMap.put("line_item_id",lineItemId); lineItemUpdateMap.put("tags",{{"tag_id":tagId,"tag_option_id":defaultTagOptionId}}); json = {"JSONString":{"line_items":{lineItemUpdateMap}}}; info json; // // Update Invoice updateInvoice = invokeurl [ url :"https://books.zoho.com/api/v3/invoices/" + INVOICE_ID + "?organization_id=" + ORG_ID type :PUT parameters:json connection:"zoho_books" ]; info updateInvoice; } }
Click here to copy these scripts. For more Zoho-wizardry, check out our GitHub page.
When scripting, ask yourself this question – will this value ever be null? If it’s a yes, that’s a place for a null check! Here are 3 tips and best practices to help you kickstart the habit....
Learn to create a customized inventory report with Zoho Analytics. This tutorial contains a link to our GitHub page for SQL code that will help with your table creations....
How nice would it be if you could, at the press of a button, send clients an email w/a Zoho Books invoice? Replete with “Pay Now” buttons that link to PayPal/Stripe/other payment gateways?...
Whether you work primarily out of CRM or the Zoho Finance Suite, you can use Analytics to build commissions dashboards. This involves some fairly simple SQL code....
If you are an inventory manager, this blog post could change your life. If you are not, it will at least teach you how to build some wicked inventory tracking for your business....
Convert fields, related activities, attachments, notes and more from one record to another across modules via custom function....