📅  最后修改于: 2022-03-11 14:58:49.206000             🧑  作者: Mango
//Searching the database by Criteria
function criteria_search() {
console.log('Search by Criteria Function invoked');
//ZOHO SDK Magic
ZOHO.CREATOR.init()
.then(function (data) {
//Variables
//Literal string definitions
let crit_begin = "("
let string_name = "f_Name==";
let string_surname = "l_Name==";
let string_email = "Email==";
let string_ampersand = "&&";
let quotes = `\"`;
let crit_end = ")";
let crit_string = "";
//These are the fields that will be searched for with specified criteria. Each application will be different. You might have many more fields to be searched
//This form just searches by name/email/surname
let crit_name = field_name.name.value;
let crit_email = field_name.email.value;
let crit_surname = field_name.surname.value;
//There may or may not be if statements. I wanted to be able to search the database for 3 matching fields, your search requirements might be different
//If there are values in Name AND Surname AND email, this will be the search criteria
if (field_name.name.value && field_name.surname.value && field_name.email.value) {
console.log("nse");
console.log(crit_name);
}
//If there are values in Name AND Surname, this will be the search criteria
if (field_name.name.value && field_name.surname.value && !field_name.email_value) {
console.log("ns");
crit_string = crit_begin + string_name + quotes + crit_name + quotes + string_ampersand + string_surname + quotes + crit_surname + quotes + crit_end;
}
//If only the name field has a value, this will be the search criteria
if (field_name.name.value && !field_name.surname.value && !field_name.email.value) {
console.log("n");
crit_string = crit_begin + string_name + quotes + crit_name + quotes + crit_end;
}
//Building of the ZOHO config file
let config = {
reportName: "All_Visitors",//The Report that will be searched
criteria: crit_string//The criteria string that will be sent
};
//Strings that work
//`(f_Name==\"${crit_name}\")`
// '(f_Name =="Pieter" || Email=="ferplie@gmail.com")'
//'(f_Name =="Maria" && l_Name=="von Trapp" && Email=="sound@gmail.com")'
console.log('Config to be sent:');
console.log(config);
//Connecting with the Zoho API
ZOHO.CREATOR.API
.getAllRecords(config)//get all the records that match the criteria string
.then(function (response) {//response is the object received back from Zoho
if (response.code == 3000) {//code 3000 means success
console.log('All records fetched, see table');//You might not have a table
//const array_data = response.data;
let counter = 0;
OBJECT_GLOBAL = response.data;
CreateTableFromJSON();
return OBJECT_GLOBAL;
}
if (response.code == 3330) {
console.log("Invalid criteria specified")
}
})
.catch(function (data) {
alert(data.responseText);
console.log('Boss, we have a little problem');
console.log(data.responseText);
});
});
}