This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.
Workforce Management in PeopleHR
The second use case in the PeopleHR business scenario is used for workforce management. This page describes the related tasks and the operations you use in the PeopleHR connector and the other ESB connectors.Â
Overview
The flow for workforce management is illustrated in the following diagram. The ESB connectors for ZohoPeople and Deputy will be used to connect to each service.Â
- In this use case, an absence record is created. Employee records are retrieved from ZohoPeople using the getRecord operation. A leave record is created in ZohoPeople using the createLeave operation, and in the Deputy API using the createLeave operation. ZohoPeople is used as an Attendance and Time Tracking system which enables the tracking of employees’ time-in and time-out details. Deputy is an Employee Scheduling and Workforce Management Software which can be integrated with PeopleHR to track timesheets and rosters. The updateObject operation will be used to update a leave record in Deputy. The leave type ID which is necessary for the creation of leave in ZohoPeople will be retrieved using the getLeaveTypes operation.Â
- Leave records can be retrieved from Deputy on a monthly basis using the queryObject operation while the getObject operation will be used to retrieve the employee details from the Deputy API. Once these are completed, an absence record can be created in PeopleHR using the createAbsenceRecord operation. Â
Deputy operations
ZohoPeople operations
PeopleHR operations
SamplesÂ
Sample Template for Enabling Create Leave in ZohoPeople and Deputy by Verifying the Employee Existence
<?xml version="1.0" encoding="UTF-8"?> <!-- This template enables creating leave in ZohoPeople & Deputy by verifying the employee's existence. --> <template name="peoplehr-zpCreateLeaveAnddeputyCreateLeave" xmlns="http://ws.apache.org/ns/synapse"> <!-- ZohoPeople parameters --> <parameter name="zohoPeopleApiUrl" description="The apiUrl of ZohoPeople." /> <parameter name="zohoPeopleAuthToken" description="Encrypted alphanumeric string to authenticate the Zoho credentials." /> <parameter name="zohoPeopleEmpRecordId" description="The employee record identifier to retrieve the employee details." /> <parameter name="zohoPeopleLeaveType" description="Specifies the leave type." /> <parameter name="zohoPeopleLeaveFrom" description="Starting date of leave." /> <parameter name="zohoPeopleLeaveTo" description="Ending date of leave." /> <parameter name="leaveDuration" description="No. of leave days." /> <!-- Deputy parameters --> <parameter name="deputyApiUrl" description="The URL of the Deputy API." /> <parameter name="deputyAccessToken" description="The OAuth 2.0 access token Deputy API call." /> <parameter name="deputyLeaveStatus" description="The status of the leave." /> <parameter name="deputyLeaveApprovalComment" description="The approval comment for the leave." /> <parameter name="deputyLeaveComment" description="Deputy leave comment." /> Â <sequence> <!-- ZohoPeople properties --> <property name="uri.var.zohoPeopleApiUrl" expression="$func:zohoPeopleApiUrl" /> <property name="uri.var.zohoPeopleAuthToken" expression="$func:zohoPeopleAuthToken" /> <property name="uri.var.zohoPeopleEmpRecordId" expression="$func:zohoPeopleEmpRecordId" /> <property name="uri.var.zohoPeopleLeaveType" expression="$func:zohoPeopleLeaveType" /> <property name="uri.var.zohoPeopleLeaveFrom" expression="$func:zohoPeopleLeaveFrom" /> <property name="uri.var.zohoPeopleLeaveTo" expression="$func:zohoPeopleLeaveTo" /> <property name="uri.var.leaveDuration" expression="$func:leaveDuration" /> <!-- Deputy properties --> <property name="uri.var.deputyApiUrl" expression="$func:deputyApiUrl" /> <property name="uri.var.deputyAccessToken" expression="$func:deputyAccessToken" /> <property name="uri.var.deputyLeaveStatus" expression="$func:deputyLeaveStatus" /> <property name="uri.var.deputyLeaveApprovalComment" expression="$func:deputyLeaveApprovalComment" /> <property name="uri.var.deputyLeaveComment" expression="$func:deputyLeaveComment" />Â Â Â Â Â Â Â Â Â Â Â <!-- Calling the ZohoPeople getRecord method. --> <zohopeople.init> <apiUrl>{$ctx:uri.var.zohoPeopleApiUrl}</apiUrl> <authToken>{$ctx:uri.var.zohoPeopleAuthToken}</authToken> <responseType>application/json</responseType> </zohopeople.init> <zohopeople.getRecord> <recordId>{$ctx:uri.var.zohoPeopleEmpRecordId}</recordId> <formLinkName>P_Employee</formLinkName> </zohopeople.getRecord> Â Â Â Â Â Â Â Â <!-- Retrieving the employee id from the employee record. --> <property name="uri.var.zohoPeopleEmployeeId" expression="json-eval($.response.result..EmployeeID[0])" /> Â <!-- The ZohoPeople employee record parameter to be used in responseHandlerTemplate. --> <property name="uri.var.zohoEmpPeopleId" expression="fn:concat('employee_record_id:',get-property('uri.var.zohoPeopleEmpRecordId'))" /> Â <filter source="boolean(get-property('uri.var.zohoPeopleEmployeeId'))" regex="false"> <then> <!-- The ZohoPeople Query employee record failure response. --> <property name="uri.var.message" value="Retrieving the ZohoPeople employee record was unsuccessful for the given ID." /> <call-template target="responseHandlerTemplate"> <with-param name="activity" value="zohopeople_getRecord" /> <with-param name="id" value="{$ctx:uri.var.zohoEmpPeopleId}" /> <with-param name="status" value="Error" /> <with-param name="message" value="{$ctx:uri.var.message}" /> </call-template> </then> <else> <!-- Creating the payload content for the deputyQuery tag element in the queryObject method. --> <payloadFactory media-type="json"> <format> {"query":{"search":{"f2":{"field":"OtherName","type": "eq","data":"$1"}}}} </format> <args> <arg expression="get-property('uri.var.zohoPeopleEmployeeId')" /> </args> </payloadFactory> <property name="uri.var.deputyQuery" expression="json-eval($.query)"/> Â <!-- Calling the Deputy queryObject method. --> <deputy.init> <accessToken>{$ctx:uri.var.deputyAccessToken}</accessToken> <apiUrl>{$ctx:uri.var.deputyApiUrl}</apiUrl> </deputy.init> <deputy.queryObject> <objectName>Employee</objectName> <query>{$ctx:uri.var.deputyQuery}</query> </deputy.queryObject> Â <!-- Retrieving the employee id from the Deputy employee record. --> <property name="uri.var.deputyEmployeeId" expression="json-eval($[0].Id)"/> Â <!-- The Employee ID parameter to be used in responseHandlerTemplate. --> <property name="uri.var.deputyId" expression="fn:concat('employee_id:',get-property('uri.var.zohoPeopleEmployeeId'))" /> Â <!-- Check for the existence of the employee record in Deputy. --> <filter source="boolean(get-property('uri.var.deputyEmployeeId'))" regex="false"> <then> <!-- Deputy query employee record failure response --> <property name="uri.var.message" value="Could not retrieve the employee record for the given ID. Therefore, leave was not created in ZohoPeople and Deputy." /> <call-template target="responseHandlerTemplate"> <with-param name="activity" value="deputy_queryObject" /> <with-param name="id" value="{$ctx:uri.var.deputyId}" /> <with-param name="status" value="Error" /> <with-param name="message" value="{$ctx:uri.var.message}" /> </call-template> </then> <else> Â <!-- Calling the ZohoPeople getLeaveTypes method to retrieve the leave type ID to create the leave record in ZohoPeople. --> <zohopeople.init> <apiUrl>{$ctx:uri.var.zohoPeopleApiUrl}</apiUrl> <authToken>{$ctx:uri.var.zohoPeopleAuthToken}</authToken> <responseType>application/json</responseType> </zohopeople.init> <zohopeople.getLeaveTypes /> Â <property name="messageType" value="application/xml" scope="axis2" /> <!-- The variable to store the leave type ID in ZohoPeople. --> <property name="uri.var.leaveTypeId" value="" scope="operation"/> <!-- FOR EACH leave type check : BEGIN --> <iterate continueParent="true" id="leaveTypes" expression="//response/result" sequential="true" > <target> <sequence> <property name="uri.var.zpLeaveTypeName" expression="//result/LeaveTypeName/text()" /> <property name="uri.var.zpLeaveTypeId" expression="//result/LeaveTypeId/text()" /> <filter xpath="get-property('uri.var.zohoPeopleLeaveType' ) = get-property('uri.var.zpLeaveTypeName')"> <property name="uri.var.leaveTypeId" expression="get-property('uri.var.zpLeaveTypeId')" scope="operation" /> </filter> </sequence> </target> </iterate> Â <property name="uri.var.zohoPeopleLeaveTypeId" expression="get-property('operation','uri.var.leaveTypeId')" /> <!-- The leave type parameter to be used in responseHandlerTemplate. --> <property name="uri.var.leaveType" expression="fn:concat('leave_type:',get-property('uri.var.zohoPeopleLeaveType'))" /> <property name="messageType" value="application/json" scope="axis2" /> <!-- Check for the existence of the leave type ID in ZohoPeople. --> <filter source="boolean(get-property('uri.var.zohoPeopleLeaveTypeId'))" regex="false"> <then> <!-- ZohoPeople get leave type failure response --> <property name="uri.var.message" value="Sorry, unable to locate the given leave type in ZohoPeople." /> <call-template target="responseHandlerTemplate"> <with-param name="activity" value="zohopeople_getLeaveTypes" /> <with-param name="id" value="{$ctx:uri.var.leaveType}" /> <with-param name="status" value="Error" /> <with-param name="message" value="{$ctx:uri.var.message}" /> </call-template> </then> <else> <!-- Creating xmlData for leave creation in ZohoPeople --> <script language="js"> <![CDATA[ var xmlData = '<Request><Record><field name="Employee_ID">' + mc.getProperty('uri.var.zohoPeopleEmpRecordId') + '</field><field name="From">' + mc.getProperty('uri.var.zohoPeopleLeaveFrom') + '</field><field name="To">' + mc.getProperty('uri.var.zohoPeopleLeaveTo') + '</field><field name="Daystaken">' + mc.getProperty('uri.var.leaveDuration') + '</field><field name="Leavetype">' + mc.getProperty('uri.var.zohoPeopleLeaveTypeId') + '</field></Record></Request>'; mc.setProperty('uri.var.zohoPeopleXmlData', xmlData); ]]> </script> <!-- Adding the leave record into ZohoPeople. --> <zohopeople.init> <apiUrl>{$ctx:uri.var.zohoPeopleApiUrl}</apiUrl> <authToken>{$ctx:uri.var.zohoPeopleAuthToken}</authToken> <responseType>application/json</responseType> </zohopeople.init> <zohopeople.createLeave> <xmlData>{$ctx:uri.var.zohoPeopleXmlData}</xmlData> </zohopeople.createLeave> Â <property name="uri.var.zohoPeopleErrorCode" expression="json-eval($[0].code)"/> Â <!-- The employee record ID parameter to be used in responseHandlerTemplate. --> <property name="uri.var.zohoEmployeeRecordId" expression="fn:concat('employee_record_id:',getproperty'uri.var.zohoPeopleEmpRecordId'))" /> Â !-- Check whether an error code is present. --> <filter source="boolean(get-property('uri.var.zohoPeopleErrorCode'))" regex="true"> <then> <!-- The ZohoPeople createLeave failure response. --> <property name="uri.var.message" expression="json-eval($[0].message[0].From)" /> <call-template target="responseHandlerTemplate"> <with-param name="activity" value="zohopeople_createLeave" /> <with-param name="id" value="{$ctx:uri.var.zohoEmployeeRecordId}" /> <with-param name="status" value="Error" /> <with-param name="message" value="{$ctx:uri.var.message}" /> </call-template> </then> <else> <!-- ZohoPeople Create Leave Successful Response --> <property name="uri.var.status" value="Success" /> <property name="uri.var.message" expression="fn:concat('Leave successfully added to employee. ID: ',get-property('uri.var.zohoPeopleEmployeeId'))" /> <!-- Generate the ZohoPeople Create Leave Successful Response. --> <call-template target="responseHandlerTemplate"> <with-param name="activity" value="zohopeople_createLeave" /> <with-param name="id" value="{$ctx:uri.var.zohoPeopleEmployeeId}" /> <with-param name="status" value="{$ctx:uri.var.status}" /> <with-param name="message" value="{$ctx:uri.var.message}" /> </call-template> Â <!-- Adding leave record into Deputy. --> <deputy.init> <accessToken>{$ctx:uri.var.deputyAccessToken}</accessToken> <apiUrl>{$ctx:uri.var.deputyApiUrl}</apiUrl> </deputy.init> <deputy.createLeave> <startDate>{$ctx:uri.var.zohoPeopleLeaveFrom}</startDate> <status>{$ctx:uri.var.deputyLeaveStatus}</status> <endDate>{$ctx:uri.var.zohoPeopleLeaveTo}</endDate> <employee>{$ctx:uri.var.deputyEmployeeId}</employee> <approvalComment>{$ctx:uri.var.deputyLeaveApprovalComment}</approvalComment> </deputy.createLeave> Â <property name="uri.var.deputyEmployeeReturnId" expression="json-eval($.Employee)"/> <property name="uri.var.deputyObjectId" expression="json-eval($.Id)"/> <property name="uri.var.deputyErrorCode" expression="json-eval($.error.code)"/> Â <!-- The employee record id parameter to be used in responseHandlerTemplate. --> <property name="uri.var.deputyEmployeeRecordId" expression="fn:concat('deputy_employee_record_id:',get-property('uri.var.deputyEmployeeReturnId'))" /> Â <filter source="boolean(get-property('uri.var.deputyErrorCode'))" regex="true"> <then> <!-- Deputy Create Leave Failure Response --> <property name="uri.var.message" expression="json-eval($.error.message)" /> <call-template target="responseHandlerTemplate"> <with-param name="activity" value="deputy_createLeave" /> <with-param name="id" value="{$ctx:uri.var.deputyId}" /> <with-param name="status" value="Error" /> <with-param name="message" value="{$ctx:uri.var.message}" /> </call-template> </then> <else> <!-- Deputy Leave Added Successful Response --> <property name="uri.var.status" value="Success" /> <property name="uri.var.message" expression="fn:concat('Leave successfully added to employee. ID: ',get-property('uri.var.deputyEmployeeReturnId'))" /> Â <!-- Creating the payload for the parameter object in the updateObject method. --> <payloadFactory media-type="json"> <format> {"deputyObject":{"Comment":"$1","Days": "$2"}} </format> <args> <arg expression="get-property('uri.var.deputyLeaveComment')" /> <arg expression="get-property('uri.var.leaveDuration')" /> </args> </payloadFactory> <property name="uri.var.deputyObject" expression="json-eval($.deputyObject)"/> Â <!-- Update the leave record in Deputy. --> <deputy.init> <accessToken>{$ctx:uri.var.deputyAccessToken}</accessToken> <apiUrl>{$ctx:uri.var.deputyApiUrl}</apiUrl> </deputy.init> <deputy.updateObject> <objectName>Leave</objectName> <objectId>{$ctx:uri.var.deputyObjectId}</objectId> <object>{$ctx:uri.var.deputyObject}</object> </deputy.updateObject> Â <property name="uri.var.deputyUpdatedObjectId" expression="json-eval($.Id)"/> <!-- The employee record id parameter to be used in responseHandlerTemplate. --> <property name="uri.var.objectId" expression="fn:concat('deputy_object_id:',get-property('uri.var.deputyUpdatedObjectId'))" /> Â <filter source="boolean(get-property('uri.var.deputyUpdatedObjectId'))" regex="false"> <then> <!-- ZohoPeople getLeaveTypes Records Failure Response --> <property name="uri.var.message" expression="fn:concat('Leave successfully added to employee. Please update (deputy leave comment : ', get-property('uri.var.deputyLeaveComment'), ', and deputy leave duration: ', get-property('uri.var.leaveDuration'), ')')" /> Â <call-template target="responseHandlerTemplate"> <with-param name="activity" value="deputy_createLeave" /> <with-param name="id" value="{$ctx:uri.var.objectId}" /> <with-param name="status" value="Error" /> <with-param name="message" value="{$ctx:uri.var.message}" /> </call-template> </then> </filter> </else> </filter> </else> </filter> </else> </filter> </else> </filter> </else> </filter> </sequence> </template>
Sample Proxy for Creating Absence Record
<?xml version="1.0" encoding="UTF-8"?> <proxy xmlns="http://ws.apache.org/ns/synapse" name="peoplehr_zpCreateLeave_deputyCreateLeave" transports="https,http" statistics="disable" trace="disable" startOnLoad="true"> <target> <inSequence onError="faultHandlerSeq"> <!-- The ZohoPeople createLeave parameters. --> <property name="zohoPeopleApiUrl" expression="json-eval($.zohoPeopleApiUrl)" /> <property name="zohoPeopleAuthToken" expression="json-eval($.zohoPeopleAuthToken)" /> <property name="zohoPeopleEmpRecordId" expression="json-eval($.zohoPeopleEmpRecordId)" /> <property name="zohoPeopleLeaveType" expression="json-eval($.zohoPeopleLeaveType)" /> <property name="zohoPeopleLeaveFrom" expression="json-eval($.zohoPeopleLeaveFrom)" /> <property name="zohoPeopleLeaveTo" expression="json-eval($.zohoPeopleLeaveTo)" /> <property name="leaveDuration" expression="json-eval($.leaveDuration)" /> <!-- The Deputy createLeave parameters --> <property name="deputyAccessToken" expression="json-eval($.deputyAccessToken)"/> <property name="deputyApiUrl" expression="json-eval($.deputyApiUrl)"/> <property name="deputyLeaveStatus" expression="json-eval($.deputyLeaveStatus)"/> <property name="deputyLeaveApprovalComment" expression="json-eval($.deputyLeaveApprovalComment)"/> <property name="deputyLeaveComment" expression="json-eval($.deputyLeaveComment)"/> <script language="js"> <![CDATA[ var fromDate = mc.getProperty('zohoPeopleLeaveFrom'); var toDate = mc.getProperty('zohoPeopleLeaveTo'); var leaveDuration = mc.getProperty('leaveDuration'); var cal1 = java.util.Calendar.getInstance(); var cal2 = java.util.Calendar.getInstance(); cal1.setTime(new java.text.SimpleDateFormat("dd-MMM-yyyy").parse(fromDate)); cal2.setTime(new java.text.SimpleDateFormat("dd-MMM-yyyy").parse(toDate)); var numberOfDays = 0; var valid = "false"; while (cal1.before(cal2) || cal1.equals(cal2)) { if ((java.util.Calendar.SATURDAY != cal1.get(java.util.Calendar.DAY_OF_WEEK)) &&(java.util.Calendar.SUNDAY != cal1.get(java.util.Calendar.DAY_OF_WEEK))) { numberOfDays++; cal1.add(java.util.Calendar.DATE,1); }else { cal1.add(java.util.Calendar.DATE,1); } } print("Number of days : " + numberOfDays); if((parseFloat(numberOfDays) == 1) && (parseFloat(leaveDuration) == 0.25 || parseFloat(leaveDuration) == 0.50)) { valid = "true"; } else if (parseFloat(leaveDuration) == parseFloat(numberOfDays)) { valid = "true"; } mc.setProperty('status' , valid); mc.setProperty('maxLeaveDays' , numberOfDays); ]]> </script> <filter source="boolean(get-property('zohoPeopleEmpRecordId'))" regex="false"> <then> <property name="message" value="Please enter an employee record Id." /> <call-template target="responseHandlerTemplate"> <with-param name="id" value=" " /> <with-param name="status" value="Error" /> <with-param name="message" value="{$ctx:message}" /> </call-template> <loopback/> </then> </filter> <filter source="get-property('status')" regex="false"> <then> <property name="message" value="Sorry, the duration does not match with the date range." /> <call-template target="responseHandlerTemplate"> <with-param name="id" value=" " /> <with-param name="status" value="Error" /> <with-param name="message" value="{$ctx:message}" /> </call-template> </then> <else> <call-template target="peoplehr-zpCreateLeaveAnddeputyCreateLeave"> <!-- Parameter values will be passed on to a sequence template for getRecord and createLeave methods for ZohoPeople. --> ( <with-param name="zohoPeopleApiUrl" value="{$ctx:zohoPeopleApiUrl}" /> | <with-param name="zohoPeopleAuthToken" value="{$ctx:zohoPeopleAuthToken}" /> | <with-param name="zohoPeopleEmpRecordId" value="{$ctx:zohoPeopleEmpRecordId}" /> | <with-param name="zohoPeopleLeaveType" value="{$ctx:zohoPeopleLeaveType}" /> | <with-param name="zohoPeopleLeaveFrom" value="{$ctx:zohoPeopleLeaveFrom}" /> | <with-param name="zohoPeopleLeaveTo" value="{$ctx:zohoPeopleLeaveTo}" /> | <with-param name="leaveDuration" value="{$ctx:leaveDuration}" /> | <!-- The parameter values will be passed on to a sequence template for the deputyQuery and createLeave methods for Deputy. --> <with-param name="deputyAccessToken" value="{$ctx:deputyAccessToken}" /> | <with-param name="deputyApiUrl" value="{$ctx:deputyApiUrl}" /> | <with-param name="deputyLeaveStatus" value="{$ctx:deputyLeaveStatus}" /> | <with-param name="deputyLeaveApprovalComment" value="{$ctx:deputyLeaveApprovalComment}" /> | <with-param name="deputyLeaveComment" value="{$ctx:deputyLeaveComment}" /> | ) * </call-template> </else> </filter> <loopback/> </inSequence> <outSequence> <log/> <payloadFactory media-type="json"> <format> { "Response":{ "process":"peoplehr_createLeaveInZohoPeopleAndDeputy", "activityResponse":[$1] } } </format> <args> <arg expression="get-property('uri.var.responseString')" /> </args> </payloadFactory> <send/> </outSequence> </target> <description/> </proxy>
Sample Request for Creating Absence Record
{ "zohoPeopleApiUrl" : "https://people.zoho.com", "zohoPeopleAuthToken" : "462f5f36b8fb6c95e7b4d279894021b9", "zohoPeopleEmpRecordId" : "281860000000054003", "zohoPeopleLeaveType" : "Vacation", "zohoPeopleLeaveFrom" : "24-Nov-2014", "zohoPeopleLeaveTo" : "25-Nov-2014", "leaveDuration" : "2", "deputyAccessToken" : "97f6fdaa9d87945766aab7c7dd5dae2a", "deputyApiUrl": "https://virtusa12.as.deputy.com", "deputyLeaveStatus" : "1", "deputyLeaveApprovalComment" : "Awating Approval", "deputyLeaveComment": "Personnel commitment" }
Sample Proxy for Creating Absence Record
<?xml version="1.0" encoding="UTF-8"?> <proxy xmlns="http://ws.apache.org/ns/synapse" name="peoplehr_retrieveAndCreateMonthlyLeaves" transports="https,http" statistics="disable" trace="disable" startOnLoad="true"> <target> <inSequence onError="faultHandlerSeq"> <!-- The Deputy API call related properties. --> <property name="deputyAccessToken" expression="json-eval($.deputyAccessToken)"/> <property name="deputyApiUrl" expression="json-eval($.deputyApiUrl)"/> <property name="year" expression="json-eval($.year)"/> <property name="month" expression="json-eval($.month)"/> <!-- The PeopleHR API call related properties. --> <property name="peoplehrApiUrl" expression="json-eval($.peoplehrApiUrl)"/> <property name="peoplehrApiKey" expression="json-eval($.peoplehrApiKey)"/> <property name="reason" expression="json-eval($.reason)"/> <property name="index" value="0" scope="operation" /> <property name="responseString" value="" scope="operation"/> <!-- Generate the query string for retrieving leave for the given year and month. --> <payloadFactory media-type="json"> <format> { "query": { "search": { "f2": { "field": "DateStart", "type": "le", "data": "$1-$2-31" }, "f3": { "field": "DateStart", "type": "ge", "data": "$1-$2-01" } } } } </format> <args> <arg expression="get-property('year')" /> <arg expression="get-property('month')" /> </args> </payloadFactory> <property name="query" expression="json-eval($.query)"/> <!-- Retrieve monthly leave from Deputy. --> <deputy.init> <accessToken>{$ctx:deputyAccessToken}</accessToken> <apiUrl>{$ctx:deputyApiUrl}</apiUrl> </deputy.init> <deputy.queryObject> <objectName>Leave</objectName> <query>{$ctx:query}</query> </deputy.queryObject> <property name="messageType" value="application/xml" scope="axis2" /> <property name="responseStatus" expression="$axis2:HTTP_SC" /> <filter xpath="get-property('responseStatus') != 200"> <then> <property name="errorMessage" expression="fn:concat('(',$axis2:HTTP_SC,'), Cannot retrieve leaves')" /> <call-template target="responseHandlerTemplate"> <with-param name="activity" value="deputy_getLeaves" /> <with-param name="status" value="Error"/> <with-param name="message" value="{$ctx:errorMessage}"/> </call-template> <loopback /> </then> </filter> <property name="leaveCount" expression="count(//jsonElement)" scope="operation" /> <filter xpath="0 = get-property('operation', 'leaveCount')"> <then> <call-template target="responseHandlerTemplate"> <with-param name="activity" value="deputy_getLeaves" /> <with-param name="status" value="Error"/> <with-param name="message" value="No leave was found for the given year and month."/> </call-template> <loopback /> </then> </filter> <iterate id="leaveIterator" expression="//jsonElement" sequential="true" continueParent="true" preservePayload="true"> <target> <sequence> <property name="employeeId" expression="/jsonElement/Employee/text()" /> <property name="startDate" expression="//DateStart/text()" /> <property name="endDate" expression="//DateEnd/text()" /> <property name="comment" expression="//Comment/text()" /> <property name="leaveDuration" expression="//Days/text()" /> <property name="deputyLeaveId" expression="/jsonElement/Id/text()" /> <property name="deputyLeaveIdParam" expression="fn:concat('deputyLeaveId:',get-property('deputyLeaveId'))" /> <!-- Get PeopleHR employee ID from the Other Name field of Deputy --> <deputy.init> <accessToken>{$ctx:deputyAccessToken}</accessToken> <apiUrl>{$ctx:deputyApiUrl}</apiUrl> </deputy.init> <deputy.getObject> <objectName>Employee</objectName> <objectId>{$ctx:employeeId}</objectId> </deputy.getObject> <property name="otherName" expression="json-eval($.OtherName)" /> <filter source="number(get-property('otherName'))" regex="NaN"> <then> <property name="message" expression="fn:concat('Cannot fetch the PeopleHR employee ID from the Other Name field of the Deputy employee (Deputy Employee ID: ',get-property('employeeId'),')')"/> <call-template target="responseHandlerTemplate"> <with-param name="activity" value="deputy_getLeaves" /> <with-param name="id" value="{$ctx:deputyLeaveIdParam}"/> <with-param name="status" value="Error"/> <with-param name="message" value="{$ctx:message}"/> </call-template> </then> <else> <script language="js"> <![CDATA[ var startDate = mc.getProperty('startDate'); var endDate = mc.getProperty('endDate'); var leaveDuration = mc.getProperty('leaveDuration'); var startDateShort = startDate.substring(0, startDate.indexOf("T")); var endDateShort = endDate.substring(0, endDate.indexOf("T")); // Setting the start date and the end date of the leave duration. mc.setProperty('startDate', startDateShort); mc.setProperty('endDate', endDateShort); // Setting the PeopleHR leave duration type (DAY_OR_MORE = 1 , HALF_DAY = 2, QUARTER_DAY = 3). var peoplehrDurationType = "1"; if(leaveDuration == 0.25) { peoplehrDurationType = "3"; } else if(leaveDuration == 0.5) { peoplehrDurationType = "2"; } else if ((leaveDuration >= 1) && (leaveDuration % 1 === 0)) { peoplehrDurationType = "1"; } else { mc.setProperty('durationError','true'); } mc.setProperty('peoplehrDurationType', peoplehrDurationType); ]]> </script> <filter source="get-property('durationError')" regex="true"> <then> <property name="message" expression="fn:concat('Invalid leave duration: ',get-property('leaveDuration'))" /> <call-template target="responseHandlerTemplate"> <with-param name="activity" value="peoplehr_createAbsenceRecord" /> <with-param name="id" value="{$ctx:deputyLeaveIdParam}"/> <with-param name="status" value="Error"/> <with-param name="message" value="{$ctx:message}"/> </call-template> </then> <else> <peoplehr.init> <employeeId>{$ctx:otherName}</employeeId> <apiUrl>{$ctx:peoplehrApiUrl}</apiUrl> <apiKey>{$ctx:peoplehrApiKey}</apiKey> </peoplehr.init> <peoplehr.createAbsenceRecord> <startDate>{$ctx:startDate}</startDate> <duration>{$ctx:leaveDuration}</duration> <reason>{$ctx:comment}</reason> <endDate>{$ctx:endDate}</endDate> <durationType>{$ctx:peoplehrDurationType}</durationType> </peoplehr.createAbsenceRecord> <filter source="$axis2:HTTP_SC" regex="200"> <then> <property name="isError" expression="//isError/text()" /> <property name="message" expression="//Message/text()" /> <filter source="get-property('isError')" regex="false"> <then> <property name="status" value="Success" /> </then> <else> <property name="status" value="Error" /> </else> </filter> </then> <else> <property name="message" expression="fn:concat('(',$axis2:HTTP_SC,'), Cannot create leave')" /> <property name="status" value="Error" /> </else> </filter> <call-template target="responseHandlerTemplate"> <with-param name="activity" value="peoplehr_createAbsenceRecord" /> <with-param name="id" value="{$ctx:deputyLeaveIdParam}"/> <with-param name="status" value="{$ctx:status}"/> <with-param name="message" value="{$ctx:message}"/> </call-template> </else> </filter> </else> </filter> <property name="index" expression="get-property('operation','index') + 1" scope="operation" /> </sequence> </target> </iterate> <filter xpath="get-property('operation', 'index') = get-property('operation', 'leaveCount')"> <then> <loopback /> </then> </filter> </inSequence> <outSequence> <property name="messageType" value="application/json" scope="axis2" /> <!-- Generate the chained response of all the API calls in createLeads. --> <payloadFactory media-type="json"> <format> { "Response":{ "process":"peoplehr_retrieveAndCreateMonthlyLeaves", "activityResponse":[$1] } } </format> <args> <arg expression="get-property('operation', 'responseString')"/> </args> </payloadFactory> <send /> </outSequence> </target> <description /> </proxy>
Sample Request for Creating Absence Record
{ "deputyAccessToken":"556815d55648e341aab1e07eb6496027", "deputyApiUrl":"https://virasoft.as.deputy.com", "peoplehrApiUrl":"https://api.peoplehr.net", "peoplehrApiKey":"c85c2570-0602-4930-a0c7-847c1a26550c", "year": "2014", "month": "12" }