Backend Workflow Not Updating Database with API Transformed Data

I am working on integrating an API that returns employee attendance data, but I’m having trouble saving the transformed data into my Bubble database. I’m using a server script to transform the API response and extract specific fields: date, mail_id, status, and total_hours. After that, I want to store the transformed data in my Bubble database using a backend workflow.

Here’s an overview of my setup:

  1. API Workflow:
  • I’m calling an external API to fetch attendance data.
  • The API response structure is complex, so I’m transforming it in the next step to only get the required fields.

function transformApiResponse(apiResponse) {
let transformedResult = ;

// Loop through the result array in the API response
apiResponse.result.forEach(employeeData => {
const employeeDetails = employeeData.employeeDetails;
const attendanceDetails = employeeData.attendanceDetails;

// Loop through the attendanceDetails (each date)
for (let date in attendanceDetails) {
  const attendance = attendanceDetails[date];

  // Create the new structure with only the required fields
  let transformedData = {
    date: date,
    mail_id: employeeDetails["mail id"],
    status: attendance.Status,
    total_hours: attendance.TotalHours
  };

  // Add the transformed data to the result array
  transformedResult.push(transformedData);
}

});

// Return the transformed result
return {
result: transformedResult,
};
}

// Example usage
var apiResponse = {
“result”: [
{
“attendanceDetails”: {
“2024-08-26”: {
“ShiftStartTime”: “12:00 AM”,
“Status”: “Janmashtami(Holiday)”,
“FirstIn_Building”: “-”,
“OverTime”: “00:00”,
“LastOut_Location”: “-”,
“ShiftName”: “General”,
“FirstIn”: “-”,
“FirstIn_Location”: “-”,
“TotalHours”: “00:00”,
“WorkingHours”: “08:00”,
“LastOut_Building”: “-”,
“LastOut”: “-”,
“ShiftEndTime”: “12:00 AM”
},
“2024-08-27”: {
“ShiftStartTime”: “12:00 AM”,
“Status”: “Absent”,
“FirstIn_Building”: “-”,
“LastOut_Location”: “-”,
“ShiftName”: “General”,
“FirstIn”: “-”,
“FirstIn_Location”: “-”,
“TotalHours”: “00:00”,
“WorkingHours”: “00:00”,
“LastOut_Building”: “-”,
“LastOut”: “-”,
“DeviationTime”: “08:00”,
“ShiftEndTime”: “12:00 AM”
}
},
“employeeDetails”: {
“mail id”: “user1@gmail.com”,
“erecno”: “131572000000234001”,
“last name”: “user”,
“first name”: “1”,
“id”: “AI001”
}
},
{
“attendanceDetails”: {
“2024-08-26”: {
“ShiftStartTime”: “12:00 AM”,
“Status”: “Janmashtami(Holiday)”,
“FirstIn_Building”: “-”,
“OverTime”: “00:00”,
“LastOut_Location”: “-”,
“ShiftName”: “General”,
“FirstIn”: “-”,
“FirstIn_Location”: “-”,
“TotalHours”: “00:00”,
“WorkingHours”: “08:00”,
“LastOut_Building”: “-”,
“LastOut”: “-”,
“ShiftEndTime”: “12:00 AM”
},
“2024-08-27”: {
“ShiftStartTime”: “12:00 AM”,
“Status”: “Present”,
“FirstIn_Building”: “-”,
“OverTime”: “02:00”,
“LastOut_Location”: " India",
“ShiftName”: “General”,
“FirstIn”: “27-Aug-2024 10:51 AM”,
“FirstIn_Location”: “India”,
“TotalHours”: “10:00”,
“Late_In”: “10:51”,
“WorkingHours”: “08:00”,
“LastOut_Building”: “-”,
“LastOut”: “27-Aug-2024 10:27 PM”,
“Early_Out”: “01:33”,
“ShiftEndTime”: “12:00 AM”
}
},
“employeeDetails”: {
“mail id”: “user2@gmail.com”,
“erecno”: “131572000000242001”,
“last name”: “User”,
“first name”: “2”,
“id”: “5”
}
} ],
“message”: “Success”,
“uri”: “/api/attendance/getUserReport”,
“status”: 0
}

const transformed = transformApiResponse(apiResponse);

console.log(transformed)





  1. Next Step - Calling Backend Workflow:
  • I’m passing the transformed data to the next workflow step to process each item in the list individually.
  • For each transformed record, I call a backend workflow that’s supposed to save these fields into the database.

The issue:

  • The data is not being saved in the Bubble database, even though the server script runs without errors and transforms the data correctly (based on my debugging).
  • When I check the logs, it seems like the backend workflow is not triggering correctly after the transformation step.

Questions:

  1. How can I ensure that the transformed data is correctly passed and saved in the database?
  2. Is there something wrong with how I’m using the backend workflow to loop over the list of transformed data?
  3. Are there any specific best practices for passing and saving lists of transformed data from APIs?

Attached are screenshots of my workflow setup and the transformation script I’m using.

Thank you for your help!

This topic was automatically closed after 70 days. New replies are no longer allowed.