📜  ZOHO API 上传到子表单 - 任何代码示例

📅  最后修改于: 2022-03-11 14:59:04.109000             🧑  作者: Mango

代码示例1
/////***********************File upload to a SUBFORM***************************
btn_upload.addEventListener("click", () => {
    console.log("Upload button clicked");
    create_blank_rec();

});
// In order to upload a file to a subform one has to have the 19 Digit ID for the record in the main form, and link it to the 19 Digit ID in the subform's record. However, the ID in the subform record is NOT CREATED up until successful form submission. That means that we have to take a short detour. We have to create a record in the subform, and store the Parent record's ID in a field. We then return the new record as an object, and then extract the record in the subform's ID. Now that we have BOTH the Parent AND the subform's 19 Digit ID's, we can return to that record using the subform record's ID as a reference. Now that we have both ID's in a row (pun intended) we can upload the file, which will now be linked to the record in the parent form, as well as the subform
function create_blank_rec() {

    ZOHO.CREATOR.init().then(function (data) {
        console.log("Creating an empty record in the subform...");
        let var_newParent_ID = inp_ID_nr.value;//The parent record's ID
        let form_n = 'Documents';//The name of the subform


        formData = {
            data: {
                Visitors: var_newParent_ID,
            }
        };
        let config = {
            formName: form_n,//Name of the subform
            data: formData
        };
        ZOHO.CREATOR.API
            .addRecord(config)//
            .then(function (response) {
                console.log('Creating a new record...');
                console.log(response);
                if (response.code == 3000) {
                    console.log('4. Record added successfully');
                    new_ID = response.data.ID;
                    console.log(" 7. Uploading file...");
                    console.log("Global new_ID=")
                    console.log(new_ID);

                    var reportLinkName = "All_Docs";
                    var fieldName = "Sub_Upload_Field";
                    var var_parentID = inp_ID_nr.value;
                    var fileEle = upload_field.value[0];
                    let config = {
                        reportName: reportLinkName,
                        id: new_ID,
                        fieldName: "Sub_Upload_Field",
                        file: fileEle,

                    }
                    //checks to see if there is a file selected
                    if (upload_field.files.length > 0) {
                        config["file"] = upload_field.files[0];
                    }

                    console.log("Config to be sent: ");
                    console.log(config);
                    ZOHO.CREATOR.API.uploadFile(config)
                        .then(function (jsonData) {
                            console.log("The file was successfully uploaded to the subform");
                            console.log("Response: json: ");
                            console.log(jsonData);

                        })
                }
            })
            .catch(function (data) {
                alert(data.responseText);
                console.log('Boss, we have a little problem');
                console.log(data.responseText);
            });
    });
}