How to setup a schedule function to create Invoices in Zoho Books?

How to setup a schedule function to create Invoices in Zoho Books?

Zoho CRM offers a wide range of functionalities that can be performed in a set interval. One of these is scheduled functions. Schedule functions are a very powerful tool that helps users set up specific tasks that are repetitive and need automation.


Today we will be creating a schedule function to create invoices in Zoho books every week. The specific problem we are solving is as follows:


The user has two different business that uses the same Zoho Books account, as he wants all his finance at the same place. He handles both his business inside the same CRM environment. He needs a secondary invoice type created in Zoho CRM. In addition, he wants a different template to be selected for the invoices and want all weekly work item of the customer to be invoiced at the end of each week.


Things we will be doing are:


·       Creating a custom module

·       Creating custom fields inside the module to control the processing logic (Status Field and Sent Out Status)

·       Create the schedule function to create invoice inside Zoho Books on Weekly Interval


Let’s GET STARTED …..


To create a custom module inside the CRM head to the drop-down on the Modules page and click on the option Create New Module.




Name the Module and put 2 Dropdown fields as Status Field and Sent Out Status.




After saving head to the setting tab and search for the Schedule function.




Hit create a new Schedule.  




Name the schedule and choose the function. If you have already written the function, choose the existing function or choose the writing function.




If you choose a new function you will see this prompt. Name the function, Display Name, and Description.




// The following line of code is searching through records of the customer module for all the records that have Status Field equals complete and sent out field equals yes.


var =zoho.CRM.searchRecords("CUSTOMMODULE","(Status:equals:Complete)and(SentOut:equals:yes)");


// At this stage we are defining two collection variables so we can create our custom collection to make things easier.

XRec = Collection();

Cust = Collection();


// The response we got from our Step 1 of Code was a collection of values so we initiated a loop and extracted all required fields and pushed them to our custom collection that we defined above.


for each  vr in var

{

              vaf = vr.getJSON("id");

              total = vr.getJson("Total"); // Total is the custom currency field inside that we defined in the CUSTOMMODULE.

              Customer = vr.getJSON("Customer").getJSON("id"); // The customer is the custom lookup field  that we defined in the CUSTOMMODULE.

              type = vr.getJSON("Type"); // The Type is the custom currency field inside that we defined in the CUSTOMMODULE.

              subtype = vr.getJSON("Sub_Type"); // The Sub_Type is the custom currency field inside that we defined in the CUSTOMMODULE.

              Name = vr.getJSON("Name"); // Total is the custom currency field inside that we defined in the CUSTOMMODULE.

// A this stage we are adding rec into our custom collection that we defined earlier.

XRec.add({"id":vaf,"ItemName":Name,"Rate":total,"Project":type + " "+subtype,"customer_Id":Customer});

              Cust.add({"customerids":Customer});

}


// info is the command that we use to see the response when to do a test run of the code. It’s important to debug the problems in the code.

info cust;

info XRec;

 


// A this stage we are getting only the unique Ids from the customer list. Why we are doing this? Because there might be multiple jobs for the same customer so instead of creating separate invoices for each line item we are getting a list of unique customers so we create one Invoice per customer.

gif = cust.distinct();

info gif;


// Now we are using the unique collection and looping through it to get the Zoho Books Id of the customer so we can create an Invoice against that customer.

for each  ffv in gif

{

              tg = ffv.get("customerids");

              cc = zoho.crm.getRecordById("Contacts",tg);


// We are getting Zoho Books Customer ID from the Customer Module inside Zoho CRM.

              customercrmid = cc.get("GMRBooksID");

              univ = Collection();


// We are running a loop here to go through the custom collection we created above as XRec and creating line items of all the records for the CustomModule.

              for each  vfg in XRec

              {

                             tt = vfg.get("customer_Id");

                             if(tt = tg)

                             {

                                           Name = vfg.get("ItemName");

                                           total = vfg.get("Rate");

                                           Project = vfg.get("Project");

                                           cfvg = vfg.get("customer_Id");

                                           vafd = vfg.getJSON("id");

                             univ.add({"ids":vafd,"ItemName":Name,"Rate":total,"Project":Project,"customer":cfvg});

                                           ma = Map();

                                           ma.put("Status","Billed Client");

                                           ma.put("SentOut","Done");

                                           fv = zoho.crm.updateRecord("ItemName",vafd,ma);

                             }

              }

              info univ;

              listVar = List();

              for each  line in univ

              {

                             name = line.get("Project");

                             rate = line.get("Rate");

                             mapVar = Map();

                             mapVar.put("name",name);

                             mapVar.put("rate",rate);

                             mapVar.put("quantity",1);

                             listVar.add(mapVar);

              }


// We are creating a MAP list to send all details into the Zoho Books create record function.

              values = Map();

              values.put("customer_id", customercrmid);


// We got the template id from Zoho Books.

              values.put("template_id",269164300XXXXX2894);

              values.put("line_items",listVar);

              response = zoho.books.createRecord("Invoices","752XXXX6",values,"zbooks");


// To check either our function did work we are running an info response command to view the system response.

              info response;

}


Once you are done with the function you can select the scheduled date and time to run.



Conclusion:


We covered how to create a custom module and define workflow controlling fields. Then we covered how to create a schedule function to run a job every week.


There are some of the concepts or processes I believe you might be confused about or not sure how they happened:


·       How does the Zoho Books ID get into the Zoho CRM Customer Module in the first place?

·       Why are we changing the status to Customer Billed from Yes?

·       And who changes the value of the Status field to Yes in the first place?


Well, the simple answer to these questions is:


These are the different scopes and involve workflows on both Zoho Books and Zoho CRM. So if you want to learn more about them do check other discussions I have shared or If I have not done them yet you can reach out to me on my LinkedIn https://www.linkedin.com/in/waheed-qadir


To stay tuned on more content you can subscribe to our Youtube channel 


    Access your files securely from anywhere

        Zoho Developer Community




                                  Zoho Desk Resources

                                  • Desk Community Learning Series


                                  • Digest


                                  • Functions


                                  • Meetups


                                  • Kbase


                                  • Resources


                                  • Glossary


                                  • Desk Marketplace


                                  • MVP Corner


                                  • Word of the Day



                                      Zoho Marketing Automation
                                              • Sticky Posts

                                              • How to Add Users to your Organization in ZohoMail?

                                                A better clarity so you can create other users to start using Zoho Mail. You can directly Add Users from the Control Panel to your Organization. You can invite users with the existing email address. If the person (user) already uses ZohoCRM, then you can import users from Zoho CRM. You can also import them using a .csv file. (if you are planning to add them in Bulk)  In this topic, We will be discussing on how to Add and Invite users only.  The Import options are self explanatory. ____________________________________________________________________________________________________________


                                              Manage your brands on social media



                                                    Zoho TeamInbox Resources

                                                      Zoho DataPrep Resources



                                                        Zoho CRM Plus Resources

                                                          Zoho Books Resources


                                                            Zoho Subscriptions Resources

                                                              Zoho Projects Resources


                                                                Zoho Sprints Resources


                                                                  Qntrl Resources


                                                                    Zoho Creator Resources



                                                                        Zoho Campaigns Resources


                                                                          Zoho CRM Resources

                                                                          • CRM Community Learning Series

                                                                            CRM Community Learning Series


                                                                          • Kaizen

                                                                            Kaizen

                                                                          • Functions

                                                                            Functions

                                                                          • Meetups

                                                                            Meetups

                                                                          • Kbase

                                                                            Kbase

                                                                          • Resources

                                                                            Resources

                                                                          • Digest

                                                                            Digest

                                                                          • CRM Marketplace

                                                                            CRM Marketplace

                                                                          • MVP Corner

                                                                            MVP Corner





                                                                              Design. Discuss. Deliver.

                                                                              Create visually engaging stories with Zoho Show.

                                                                              Get Started Now