// Declare a global variable to store the record ID
var recordId;
var accessToken = "1000.xxxxxxxxxxxxxxxxx"; // Use the provided access token
function initializeWidget() {
console.log("Initialize called");
ZOHO.CREATOR.init()
.then(function () {
console.log("ZOHO.CREATOR.init successful");
fetchRecordUsingPageVariable(); // Call function to fetch the record
})
.catch(function (error) {
console.error("Error initializing Zoho Creator:", error);
});
}
function fetchRecordUsingPageVariable() {
console.log("fetchRecordUsingPageVariable called");
// Fetch the query parameters (e.g., recordID from URL)
var queryParams = ZOHO.CREATOR.UTIL.getQueryParams();
recordId = queryParams["recordID"]; // Store in global variable for later use in submitForm()
if (recordId) {
console.log("Record ID fetched from query parameters: " + recordId);
fetchRecord("Harvest_Records_for_Approval", recordId); // Fetch the record using the record ID
} else {
console.error("Record ID not found in query parameters.");
}
}
// Function to fetch the record from Zoho Creator form
function fetchRecord(formName, recordId) {
ZOHO.CREATOR.API.getRecordById({
reportName: formName,
id: recordId
}).then(function (response) {
//console.log("response:", response);
if (response.data) {
var harvestRecord = response.data; // Directly access the data object
console.log("Fetched harvest record:", harvestRecord);
// Update the UI with the record details
document.getElementById("ID").innerText = harvestRecord.ID || "No ID";
document.getElementById("approverNotes").value = harvestRecord.Harvest_Notes_Changes || "No Notes";
} else {
console.log("No data returned from Zoho Creator.");
}
}).catch(function (error) {
console.log("Error fetching harvest record:", error);
});
}
function submitForm() {
// Re-initialize Zoho Creator to ensure the session is active
ZOHO.CREATOR.init()
.then(function () {
console.log("ZOHO.CREATOR session refreshed");
// Check if recordId is available globally
if (!recordId) {
console.log("Record ID is not defined. Cannot submit form.");
return;
}
// Get the value from the textarea
var approverNotes = document.getElementById("approverNotes").value;
console.log("approverNotes:", approverNotes);
// Ensure the form data has the correct key and value
var formData = {
"Harvest_Notes_Changes": approverNotes // Ensure this key matches exactly with the field in Zoho Creator
};
// Use Zoho Creator's API to update the record
ZOHO.CREATOR.API.updateRecord({
appName: "farm-manager-2-0", // Ensure the app name is correct
reportName: "Harvest_Records_for_Approval", // Ensure the report name is correct
id: recordId,
data: formData // Send only valid keys for updating
}).then(function (response) {
if (response.code == 3000) {
console.log("Record updated successfully");
alert("Record updated successfully!");
} else {
console.log("Error updating record:", response);
}
}).catch(function (error) {
console.log("Error updating record:", error);
});
})
.catch(function (error) {
console.error("Error refreshing session in Zoho Creator:", error);
});
}
// Initialize the widget
initializeWidget();