Create Timesheets in Zoho People from Zoho FSM Timesheets

Create Timesheets in Zoho People from Zoho FSM Timesheets

Use case: Whenever a timesheet is created in Zoho FSM, add a timesheet in Zoho People.

Follow the steps below to implement this use case:
Step 1: Create a connection for Zoho People
Step 2: Create a connection for Zoho FSM
Step 3: Create a custom field
Step 4: Create a custom function
Step 5: Create a workflow rule

Step 1: Create a connection for Zoho People  

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



  2. Under the Default Services tab, select Zoho People.



  3. Perform the following and click Create And Connect:
    1. Enter the Connection Name as ZohoPeopleCon
      The Connection Link Name will be automatically populated.
    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 ZOHOPEOPLE.employee.ALL,  ZOHOPEOPLE.employee.READ, ZOHOPEOPLE.forms.ALL, and ZOHOPEOPLE.timetracker.all



  4. Click Connect.



  5. Click Connect.



  6. Click Accept in the Permissions page.


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


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, select Zoho FSM.



  3. Perform the following and click Create And Connect:
    1. Connection Name: FSMConnection
      The Connection Link Name will be populated automatically.
    2. Disable Use Credentials Of Login User.
    3. Select the Scopes ZohoFSM.users.READ.



  4. Click Connect.



  5. Click Accept in the Permissions 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 field

Create a custom field Zoho People Job ID of type single line in the Service Appointments module. Add this field to a new section. This field will be used to store the ID of the job in Zoho People for which the timesheet will be created.  Use the Api name in the custom function.


Step 4: Create a custom function   

Add a custom function that will create a timesheet in Zoho People from a timesheet in Zoho FSM.
  1. Navigate to Setup > Automation > Actions. Select the Deluge Functions tab and click Create Function.
  2. Enter the following details and click Save:
    1. Function Name: CreateTimesheetsInPeople
    2. Module: Time Sheets
    3. In the Deluge Script Editor, enter the following script: 

try

{

info time_sheet;

Assignees = "";

Sr_idlist = list();

sr_emailid_list = list();

time = toNumber(60);

Start_Date = time_sheet.get("Start_Date_Time").getPrefix("T");

End_Date = time_sheet.get("End_Date_Time");

if(!End_Date.isNull() && !End_Date.isEmpty())

{

End_Date = End_Date.getPrefix("T");

}

Job_name = time_sheet.get("Service_Appointment").get("name");

duration = time_sheet.get("Duration");

if(!duration.isNull() && !duration.isEmpty())

{

duration = duration.toNumber();

var = duration / time;

info var;

                                var=var.round(2);

time_sheetname = time_sheet.get("Name");

Sr_id = time_sheet.get("Service_Resource").toMap().get("id");

info Sr_id;

response = invokeurl

[

url :"https://fsm.zoho.com/fsm/v1/users"

type :GET

connection:"fsmconnection"

];

// info Sr_idlist;

usr_li = response.get("users").toList();

for each  sample in usr_li

{

usr_sr_id = sample.get("Service_Resources").get("id");

info usr_sr_id;

if(Sr_id == usr_sr_id)

{

sr_emailid = sample.get("email");

break;

}

}

//info time_sheet;

Service_appointment_id = time_sheet.get("Service_Appointment").get("id");

ser_app_resp = zoho.fsm.getRecordById("Service_Appointments",Service_appointment_id);

// info ser_app_resp;

job_id = ser_app_resp.get("data").toMap().get("zoho_People_job_Id__C");

if(job_id == null || job_id == "")

{

Service_Appointment_X_Service_Resources = ser_app_resp.get("data").toMap().get("$Service_Resources");

// info Service_Appointment_X_Service_Resources;

for each  Service_Resources in Service_Appointment_X_Service_Resources

{

Service_Resource_id = Service_Resources.toMap().get("id");

Sr_idlist.add(Service_Resource_id);

}

for each  sample in usr_li

{

usr_sr_id = sample.get("Service_Resources").get("id");

if(Sr_idlist.contains(usr_sr_id))

{

sr_emailid_list.add(sample.get("email"));

}

}

// info sr_emailid_list;

for each  email in sr_emailid_list

{

searchMap = Map();

searchMap.put("searchField","EmailID");

searchMap.put("searchOperator","Is");

searchMap.put("searchText",email);

response = zoho.people.getRecords("P_Employee",0,50,searchMap).toMap();

// info response;

if(response.contains("EmployeeID"))

{

Zoho_ID = response.get("Zoho_ID");

// info Zoho_ID;

if(Assignees != null && Assignees != "")

{

Assignees = Assignees + ";" + Zoho_ID;

}

else

{

Assignees = Zoho_ID;

}

}

}

// info Assignees;

if(Assignees != null && Assignees != "")

{

inputData = {"Job_Name":Job_name,"Assignees":Assignees};

info inputData;

response = zoho.people.create("P_TimesheetJob",inputData);

info response;

job_id = response.toMap().get("result").get("pkId");

info zoho.fsm.updateRecord("Service_Appointments",Service_appointment_id,{"zoho_People_job_Id__C":job_id});

}

}

info sr_emailid;

event = Map();

event.put("user",sr_emailid);

event.put("workDate",Start_Date);

event.put("jobId",job_id);

event.put("hours",var);

event.put("billingStatus","Billable");

info event;

response = invokeurl

[

url :"https://people.zoho.com/people/api/timetracker/addtimelog"

type :post

parameters:event

connection:"zohopeoplecon"

];

info response;

data = Map();

data.put("user",sr_emailid);

data.put("timesheetName",time_sheetname);

data.put("fromDate",Start_Date);

data.put("toDate",End_Date);

data.put("jobId",job_id);

info data;

response = invokeurl

[

url :"https://people.zoho.com/people/api/timetracker/createtimesheet"

type :post

parameters:data

connection:"zohopeoplecon"

];

info response;

}

}

catch (error)

{

info error;

// sendmail

// [

// from :zoho.adminuserid

// to :zoho.adminuserid

// subject :"Timesheet creation failed"

// message :error

// ]

}

Step 5: Create a workflow rule  

Create a workflow rule to create timesheets in Zoho People whenever timesheets are created in Zoho FSM.
  1. Navigate to Setup > Automation > Workflow Rules and click Create Workflow.
  2. Enter the following details, then click Next:
    1. Module: Time Sheets
    2. Rule Name: CreateTimesheetsInPeople
    3. Description: Create timesheets in Zoho People from timesheets in Zoho FSM



  3. Select the rule trigger as Created or Edited and click Next. Select the checkbox Repeat this workflow whenever a Timesheet is edited.



  4. Select the rule criteria as To all Time Sheets. Click Next.



  5. Click +Action and select Function.



  6. Select Existing Functions and click Next.



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



  8. Click Save.


Testing the use case  

Create a timesheet in Zoho FSM. A timesheet will be created in Zoho People for the user provided the user is present in Zoho People.