Zoho FSM | Automate creating and sending Invoice on work order creation

Automate creating and sending Invoice on work order creation

Usecase: When creating a work order, based on an user input, automatically create and send invoice for the work order.
 
Follow the steps below to implement this usecase:
Step 1: Create a custom field
Step 2: Create a connection for Zoho FSM
Step 3: Create a custom function
Step 4: Create a workflow rule

Step 1: Create a custom field

Create a custom field Create and Send Invoice of type checkbox in the Work Orders module. If the user selects this checkbox, then the invoice should be automatically created and sent for the work order.
 

Step 2: Create a connection for Zoho FSM

To use the Zoho FSM APIs via the invokeURL task in the custom function, you need to create a connection for Zoho FSM. To do so:
  1. Navigate to Setup > Developer Space > Connections and click Create Connection.



  2. Under the Default Services tab, search for and click Zoho FSM.



  3. Enter the following details and click Create And Connect.
    1. Connection Name: FSMCon
      The Connection Link Name will be populated automatically.
    2. Disable Use Credentials Of Login User.
    3. Select the Scope ZohoFSM.modules.all
  4. Click Connect in the authentication page.



  5. Click Accept in the Authorization page.


 In the custom function, where the FSM API is used via the invokeURL task, use this Connection Link Name.

Step 3: Create a custom function

Create a custom function in Zoho FSM to create and send invoice for a work order
  1. Navigate to Setup > Automation > Functions and click Create Function.
  2. Enter the following details and click Save:
    1. Function Name: CreateAndSendInvoice
    2. Module: Work Order
    3. In the Deluge Script Editor, enter the following script:

      Work_Order_Id = work_order.get("id");
      Service_Line_Items = work_order.get("Service_Line_Items");
      line_item_id_list = list();
      for each  Service_Line_Item in Service_Line_Items
      {
       line_item_id = Service_Line_Item.toMap().get("id");
       line_item_id_list.add(line_item_id);
      }
      data = {"data":{{"Work_Order":Work_Order_Id,"$Service_Line_Items":line_item_id_list,"$finance_data":{"discount_preference":{"Adjustment":0}}}}};
      invoice_response = invokeurl
      [
       type :POST
       parameters:data.toString()
       connection:"fsmcon"
      ];
      info "invoice_response: " + invoice_response;
      invoice_id = invoice_response.get("data").toMap().get("Invoices").toMap().get("id");
      contact_id = work_order.get("Contact").toMap().get("id");
      contactResp = zoho.fsm.getRecordById("Contacts",contact_id);
      contactData = contactResp.get("data").toMap();
      email = contactData.get("Email");
      email_list = list();
      email_list.add(email);
      invoice_data = {"data":{"send_invoice_pdf":true,"to_mail_ids":email_list,"send_from_org_email_id":true}};
      send_invoice_response = invokeurl
      [
       url :"https://fsm.zoho.com/fsm/v1/Invoices/" + invoice_id + "/actions/send_invoice"
       type :POST
       parameters:invoice_data.toString()
       connection:"fsmcon"
      ];
      info send_invoice_response;

Step 4: Create a workflow rule

Create a workflow rule in Zoho FSM to automatically create and send invoice for a work order when the user selects the checkbox Create and Send Invoice in the work order.
  1. Go to Setup > Automation > Workflow Rules and click Create Workflow.
  2. Enter the following details, then click Next:
    1. Module: Work Orders
    2. Rule Name: CreateAndSendInvoice
    3. Description: When the user selects the checkbox Create and Send Invoice in the work order, automatically create and send invoice.



  3. Select the rule trigger as Created or Edited and click Next.



  4. Select the rule criteria as Only to the Work Order matching certain conditions and add the condition Create and Send Invoice is Selected. Click Next.



  5. Click +Action and select Function.



  6. Select Existing Functions and click Next.



  7. Select the custom function created in the previous step.



  8. Click Save.

Testing the usecase

Create a Work Order. Ensure the checkbox Create and Send Invoice is selected.
 

 
The invoice will be automatically created for the work order and sent to the customer.