Help

Re: Avoid 30s limit script

2465 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Ludo_Figueiredo
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello, I am creating a json object from my airtable data to send it to a google script:

 

//Table Bail --> xxxxx
//Table Locataire --> xxxxx
//Table Facturation --> xxxxx
//Table Roof --> xxxx
//Table Chambre --> xxxxx


let url = "https://script.google.com/macros/s/XXXXXX";

const id_bail = input.config().id_bail; //Recupere l'id du bail

let table_bail = base.getTable("xxxxxx");
let query_bail = await table_bail.selectRecordsAsync();
let record_bail = query_bail.getRecord(id_bail); //on filtre la base en fonction de l'id du client


//////////////////////////////////////////////////////////////////////
let error_log = record_bail.getCellValue("Error_Log")
let bail_statut = record_bail.getCellValue("Statut_bail")

if (error_log !== null){
    console.error(` Oups, une erreur est survenue: \n${error_log}`)
} else if (bail_statut !== "Fini"){
  
    //Table Roof
    let id_roof = record_bail.getCellValue("Roof"); //id_roof associé au bail
    let table_roof = base.getTable("xxxxxxx");
    let query_roof = await table_roof.selectRecordsAsync();
    let record_roof = query_roof.getRecord(id_roof[0]); //on filtre la base en fonction de l'id_roof

    //Table Chambre
    let id_room = record_bail.getCellValue("Chambre")[0].id; //id_chambre associé au bail
    let table_room = base.getTable("xxxxxxx");
    let query_room = await table_room.selectRecordsAsync();
    let record_room = query_room.getRecord(id_room); //on filtre la base en fonction de l'id_roof

     //Table Locataire
    let id_locataire = record_bail.getCellValue("Locataire")[0].id;
    let table_locataire = base.getTable("xxxxxxx");
    let query_locataire = await table_locataire.selectRecordsAsync();
    let record_locataire = query_locataire.getRecord(id_locataire); //on filtre la base en fonction de l'id du locataire

    //Table Garant
    let id_garant = record_bail.getCellValue("Garant_infos")[0];
    let table_garant = base.getTable("xxxxxxxx");
    let query_garant = await table_garant.selectRecordsAsync();
    let record_garant = query_garant.getRecord(id_garant); //on filtre la base en fonction de l'id du garant

    let payload = {
        "Data_locataire": [
            {
                "locataire_data_bail": record_bail,
                "locataire_data_locataire": record_bail.getCellValue("Locataire"),
                "locataire_start_bail": record_bail.getCellValue("Start_bail"),
                "locataire_first_name": record_locataire.getCellValue("Prenom"),
                "locataire_last_name": record_locataire.getCellValue("Nom"),
                "locataire_birthday": record_bail.getCellValue("Birthday_Locataire"),
                "locataire_birthplace": record_bail.getCellValue("Birthplace_Locataire"),
                "locataire_email": record_bail.getCellValue("Email_Locataire"),
                "locataire_telephone": record_bail.getCellValue("Telephone_Locataire"),
                "locataire_adresse": record_bail.getCellValue("Adresse_Locataire"),
                "locataire_nationalite": record_bail.getCellValue("Nationalite_Locataire"),
            }
        ],
        "Data_roof": [
            {
                "roof_name": record_roof.getCellValue("Residences"),
                "roof_adresse": record_roof.getCellValue("Adresse"),
                "roof_total_superficie": record_roof.getCellValue("Total superficie (m2)"),
                "roof_commun_superficie": record_roof.getCellValue("Espace partage (m2)"),
                "roof_piece": record_roof.getCellValue("Nombre de piece"),
                "roof_piece_desc": record_roof.getCellValue("Pieces desc"),
                "roof_all_room": record_roof.getCellValue("Chambres"),
                "roof_proprietaire": record_roof.getCellValue("Infos proprietaire"),
                "roof_espaces_desc": record_roof.getCellValue("Espaces partages desc"),
                "roof_surface_privee": record_roof.getCellValue("Espace prive (m2)"),
                "roof_complement_loyer_desc": record_roof.getCellValue("Complement loyer desc"),
                "roof_annee_construction": record_roof.getCellValue("Annee construction"),
                "roof_inventaire": record_roof.getCellValue("Inventaire"),
                "roof_assurance_annexe": record_roof.getCellValue("Assurance_annexe"),
                "roof_plan_folder": record_roof.getCellValue("Plan_folder")
            }
        ],
        "Data_room": [
            {
                "room_floor": record_room.getCellValue("Etage"),
                "room_number": record_room.getCellValue("Numero"),
                "room_ville": record_room.getCellValue("Ville")[0],
                "room_price_HC": record_room.getCellValue("Loyer (HC)"),
                "room_charges": record_room.getCellValue("Charges"),
                "room_price_TCC": record_room.getCellValue("Loyer TCC"),
                "room_espace_prive": record_room.getCellValue("Espace prive (m2)"),
                "room_espace_partage": record_room.getCellValue("Espace partage (m2)"),
                "room_loyer_minore": record_room.getCellValue("Loyer_minore"),
                "room_loyer_majore": record_room.getCellValue("Loyer_majore"),
                "room_loyer_ref": record_room.getCellValue("Loyer_ref"),
                "room_complement": record_room.getCellValue("Complement"),
                "room_service": record_room.getCellValue("Service"),
                "room_communaute": record_room.getCellValue("Communaute"),
                "room_infos": record_room.getCellValue("Info_chambre_4")
            }
        ],
        "Data_garant": [
            {
                "garant_type": record_garant.getCellValue("Type").name,
                "garant_prenom": record_garant.getCellValue("Prenom"),
                "garant_nom": record_garant.getCellValue("Nom"),
                "garant_adresse": record_garant.getCellValue("Adresse"),
                "garant_birthday": record_garant.getCellValue("Date de naissance"),
                "garant_birthplace": record_garant.getCellValue("Lieu de naissance"),
                "garant_email": record_garant.getCellValue("Email"),
            }
        ],
        "Doc_type": "Bail"
    }

    let postOptions = {
    method: "post",
    headers: {
        'Accept' : 'application/json',
    },
    body: JSON.stringify(payload)
    }

    const postResults = await fetch(url, postOptions);
    //console.log("Génération réussie . Un dossier client a été généré sur le Drive");
    //console.log("Répoonse serveur: ", postResults)
    //console.log(payload)
    
} else {
    console.error(` Oups, ce client ne fait plus partie des roofers`)
}

 

 

I am getting the 30s limit but my Google script works well. I have the impression that my airtable script is waiting for the response from my google script app. I could also connect my google script to Airtable but the work has already been done so I would like to keep that like that.

 

Best regards

7 Replies 7
Airbid
6 - Interface Innovator
6 - Interface Innovator

when querying records from airtable, make sure you are specifying specific fields you need in "await", for the information you need , for example

await table_room.selectRecordsAsync({fields: ["Name"]})

otherwise you may be querying a lot of information, which slows down the script 

Sho
11 - Venus
11 - Venus

Hello @Ludo_Figueiredo,

How about looking up all the necessary values to Bail and storing them in one field?

make sure you are specifying specific fields you need in "await", 
await table_room.selectRecordsAsync({fields: ["Name"]})

You could even do better, query just a single record by id

Instead of 

    let id_room = record_bail.getCellValue("Chambre")[0].id; //id_chambre associé au bail
    let table_room = base.getTable("xxxxxxx");
    let query_room = await table_room.selectRecordsAsync();
    let record_room = query_room.getRecord(id_room);


just do: 

 let id_room = record_bail.getCellValue("Chambre")[0].id; //id_chambre associé au bail
    let table_room = base.getTable("xxxxxxx");
    let record_room = await table_room.selectRecordAsync(id_room);

 
But still, it's a lot of repeating code, I would compact it by creating a single function and pass array of field/table names to it.

Ludo_Figueiredo
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello, 

Thank you for your answer. My script runs faster but I think the problem comes from my google script. All the data I send to my google script from my airtable has to be processed. My airtable script is waiting response from my google script and my google script takes 45 seconds to accomplish. So maybe I would have to retrieve my airtable data directly from my google script ? 

would another solution be to prevent my airtable script from waiting for an answer from my google script ? even I wouldn't be able to know if my google script has worked (I can check if a bug happened directly in the google console)

Best regards

Hello,
I didn't want to pollute my main table with lookup fields. 

It’s not advisable to wait for a response is within an Airtable scripting block because of the limitations. If you need to wait for a response from the Google apps script, and are not able to write the script in Airtable entirely, then you would use web hooks from the apps script to a separate Airtable automation with the web hook trigger.

Hello, 
yes I was new to airtable so I had no idea of a server response. Now I have fairly short deadlines and I was wondering if it was possible to tell my airtable script not to wait for the fetch response from the URL?

and I plan to make the modification later when I have a little more time.