Zoho FSM | Create an Expense record for a Work Order

Create an Expense record for a Work Order

Use case: In a work order, if the user adds service line items to record expenses, then create an Expense record in Zoho Expense for an amount equivalent to the sum of all such service Line Item Amount. A service called Expense will be used for this purpose. Also, update a custom field in the work order with the ID of the Expense record created.
 
Step 1: Add a service named Expense
Step 2: Create a custom field
Step 3: Create a connection for Zoho Expense
Step 4: Create a custom function
Step 5: Create a workflow rule

Step 1: Add a service named Expense 

In Zoho FSM, create a service called Expense. Add a Unit Price that you want to charge for your expenses. Use this service in your work order to add the expenses for your work order.
 

Step 2: Create a custom field

Create a custom field Zoho Expense Record ID of type single line in the Work Orders module. This field will be used to store the ID of the Expense record created for the work order.  Use the Api name in the custom function.


Step 3: Create a connection for Zoho Expense 

To create a connection for Zoho Expense that can be used in Zoho FSM for invoking the Zoho Expense APIs, do the following:
  1. Navigate to Setup > Developer Space > Connections and under My Connections, click Create Connection.



  2. Under Default Services, select Zoho Expense.



  3. Perform the following and click Create And Connect:
    1. Connection Name: ZExpense
      The Connection Link Name will be populated automatically.
    2. Disable Use Credentials Of Login User. If this is disabled, the connection will utilize the credentials of the connection owner.

    3. Select the Scopes ZohoExpense.FullAccess.all

  4. Click Connect.



  5. Click Accept.

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

Additionally ensure that the status of the internal connection for Zoho FSM integration task is Connected. The green dot [] indicates that the connection is Connected.
 

Step 4: Create a custom function 

Add a custom function to create an Expense record in Zoho Expense for a work order in Zoho FSM. An Expense record should be created in Zoho Expense for an Amount equivalent to the sum of all the service Line Item Amount corresponding to the line items added in the work order for the service Expense. Also update a custom field in the work order with the ID of the expense record created.
  1. Navigate to Setup > Automation > Functions and click Create Function.
  2. Enter the following details and click Save:
    1. Function Name: CreateExpenseForWO
    2. Module: Work Orders
    3. In the Deluge Script Editor, enter the following script:

      work_order_id = work_order.get("id");
      Amount = 0;
      Service_Line_Items_List = work_order.get("Service_Line_Items").toList();
      for each  Service_Line_Items in Service_Line_Items_List
      {
       Service_Line_Items_Map = Service_Line_Items.toMap();
       if(Service_Line_Items_Map.get("Service").toMap().get("name") == "Expense")
       {
        //Get Currency Id
        headers_data = Map();
        headers_data.put("X-com-zoho-expense-organizationid","<orgId>");
        response = invokeurl
        [
        type :GET
        headers:headers_data
        connection:"zexpense"
        ];
        currencyList = response.get("currencies").toList();
        for each  eachCurrencyList in currencyList
        {
        CurrencyMap = eachCurrencyList.toMap();
        if(CurrencyMap.get("currency_code") == "USD")
        {
        currency_id = CurrencyMap.get("currency_id");
        }
        }
        //Get Category Id
        headers_data_1 = Map();
        headers_data_1.put("X-com-zoho-expense-organizationid","<orgId>");
        response_1 = invokeurl
        [
        type :GET
        headers:headers_data_1
        connection:"zexpense"
        ];
        expense_categories_list = response_1.get("expense_accounts").toList();
        for each  each_expense_categories_list in expense_categories_list
        {
        expense_categories_map = each_expense_categories_list.toMap();
        //Use the Expense Category - Travel Expense
        if(expense_categories_map.get("category_name") == "Travel Expense")
        {
        category_id = expense_categories_map.get("category_id");
        }
        }
       
        Amount = Amount + Service_Line_Items_Map.get("Line_Item_Amount");
        Created_Time = Service_Line_Items_Map.get("Created_Time").toString();
        Create_Date = Created_Time.getPrefix("T");
        lineItemList = list();
        lineItemMap = Map();
        lineItemMap.put("category_id",category_id);
        lineItemMap.put("amount",Amount);
        lineItemMap.put("description","From Zoho FSM");
        lineItemList.add(lineItemMap);
        createMap = Map();
        createMap.put("currency_id",currency_id);
        createMap.put("date",Create_Date);
        createMap.put("is_reimbursable",true);
        createMap.put("line_items",lineItemList);
        headers_data_2 = Map();
        headers_data_2.put("X-com-zoho-expense-organizationid","<orgId>");
       
        response_2 = invokeurl
        [
        type :POST
        parameters:createMap.toString()
        headers:headers_data_2
        connection:"zexpense"
        content-type:"application/json"
        ];
       
        expense_id = response_2.get("expenses").toMap().get("expense_id").toString();
        updateResp = zoho.fsm.updateRecord("Work_Orders",work_order_id,{"<custom_field_Api_name>":expense_id});
       }
      }
      Note: In this custom function, the Expense Category used is Travel Expense.

  3. Replace <orgId> with the actual value. To obtain the Organization ID, in Zoho Expense, click the organization name at the top right corner and copy the Expense Organization ID.



  4. Replace <custom_field_Api_name> with the Api name of the custom field.

Step 5: Create a workflow rule 

Create a workflow rule in Zoho FSM to automatically create an Expense record in Zoho Expense for a 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: CreateExpenseForWO
    3. Description: Create an expense record for a work order



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



  4. Select the rule criteria as To all Work Orders and 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 in Zoho FSM. Add a service line item with the service Expense. Enter a Quantity of your choice.
 

 
An expense record with an Amount equivalent to the Line Item Amount for the Expense service line item will be added.
 

In the work order, the ID of the expense record created will be saved to the Zoho Expense Record ID field.