Zoho FSM | Send Service Report To Customer

Send Service Report To Customer

Usecase: Email the service report to the customer on service appointment completion.
 
Follow the steps below to implement this usecase:
Step 1: Create a connection for Zoho FSM
Step 2: Create a custom function
Step 3: Create a workflow rule

Step 1: 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 Default Services, choose Zoho FSM.



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

    3. Select the Scopes ZohoFSM.files.READ



  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 2: Create a custom function  

Create a custom function in Zoho FSM to email the service report to the customer on service appointment completion.
  1. Navigate to Setup > Automation > Functions and click Create Function.
  2. Enter the following details and click Save:
    1. Function Name: SendServiceReport
    2. Module: Service Appointments
    3. In the Deluge Script Editor, enter the following script:

      try 
      {
      file_list = list();
      Appointment_id = service_appointment.get("id");
      service_appointment_resp = zoho.fsm.getRecordById("Service_Appointments",Appointment_id);
      service_appointment_map = service_appointment_resp.get("data").toMap();
      contactId = service_appointment_map.get("Contact").get("id");
      contactResp = zoho.fsm.getRecordById("Contacts",contactId);
      email = contactResp.get("data").toMap().get("Email");
      service_related_resp = zoho.fsm.getRelatedRecords("Service_Reports","Service_Appointments",Appointment_id);
      //info service_related_resp;
      if(service_related_resp.isEmpty())
      {
      info "No Service_Report added !!!";
      }
      else
      {
      related_resp_list = service_related_resp.get("data");
      for each  each_related_resp in related_resp_list
      {
      service_report_map = each_related_resp.toMap();
      Status = service_report_map.get("Status");
      if(Status.equalsIgnoreCase("Draft"))
      {
      Service_Report_PDF = service_report_map.get("Service_Report_PDF").toMap();
      Report_file_Id = Service_Report_PDF.get("file_Id");
      fileResp = invokeurl
      [
      url :"https://fsm.zoho.com/fsm/v1/files?file_id=" + Report_file_Id
      type :GET
      connection:"fsmcon"
      ];
      filename = service_report_map.get("Name") + ".pdf";
      fileResp.setFileType("pdf");
      fileResp.setFileName(filename);
      // info fileResp;
      file_list.add(fileResp);
      }
      }
      }
      if(!file_list.isEmpty())
      {
      sendmail
      [
      from :zoho.adminuserid
      to :email
      subject :"Service Report with attachment"
      message :"PFA"
      Attachments :file:file_list
      ]
      }
      }
      catch (err)
      {
      info err;
      sendmail
      [
      from :zoho.adminuserid
      to :zoho.adminuserid
      subject :"Error in executing the code"
      message :err
      ]
      }

Step 3: Create a workflow rule  

Create a workflow rule in Zoho FSM to automatically email the service report to the customer on service appointment completion.
  1. Go to Setup > Automation > Workflow Rules and click Create Workflow.
  2. Enter the following details, then click Next:
    1. Module: Service Appointments
    2. Rule Name: Send Service Report
    3. Description: Email the service report to the customer on service appointment completion.



  3. Select the rule trigger as Edited with the condition When Status is modified to the value Completed. Select the checkbox Repeat this workflow whenever a Service Appointment is edited and click Next.



  4. Select the rule criteria as To all Service Appointments 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  

  1. Create a Service Appointment.
  2. Create a Service Report.
  3. Click Complete Work for the service appointment.