Trigger a javascript function from a custom action in Glide

Yes i have tried it before and unfortunatly:
Function Error
TypeError: Failed to fetch

If it failed to fetch… probably had the wrong parameters, or parameters were empty when the request was made… remember that it is hard to determine when the script will start running. The columns with the parameters might not be filled yet… it would be best if you made sure to wait for that.

  1. For testing - all rarametrs are seting inside code column/
  2. I tried to return url and then use it to fetch by glide fetch column and it works. It mean code column have right url but cant fetch and i dont know why

Hola Alexey!

Let me understand the problem with your code:

let myTrigger = p1;
if (myTrigger == 1) {
  const title = “Запись на тренировку Хатха-Йога”;
  const phone = “+70000000000”;
  const name = “p2”;
  const comments = “Тестовый комментарий”;
  const url = `https://vektor-systems.bitrix24.ru/rest/13/1f1331sb41fa9r09/crm.lead.add.json?FIELDS[TITLE]=${encodeURIComponent(title)}&FIELDS[PHONE][0][VALUE]=${encodeURIComponent(phone)}&FIELDS[NAME]=${encodeURIComponent(name)}&FIELDS[COMMENTS]=${encodeURIComponent(comments)}`;
  const data = await fetch(url);
  const json = await data.json();
  return json;
}

Can you give 1-2 valid URLs to test them via Glide fetch column and then using this JS?

BTW… Was the myTrigger set to 1? (this kind of things happens) :rofl:

If myTrigger is a Boolean column, you must check it as true or false

let myTrigger = p1;
if (myTrigger == true) {
    ///  your code
}

Saludos!

Hi, gvalero! Thanks for income!
Yes trigger is working also for true
Thank you for correcting!
This url gives response
https://vektor-systems.bitrix24.ru/rest/13/4825o4an22hrm2o4/crm.contact.list.json?FILTER[PHONE]=79605463333&SELECT[ID]=
Response
{“result”:[{“ID”:“19525”},{“ID”:“19527”},{“ID”:“19529”},{“ID”:“19531”},{“ID”:“19533”},{“ID”:“19535”}],“total”:6,“time”:{“start”:1681078043.07305,“finish”:1681078043.2366631,“duration”:0.16361308097839355,“processing”:0.10522699356079102,“date_start”:“2023-04-10T01:07:23+03:00”,“date_finish”:“2023-04-10T01:07:23+03:00”,“operating_reset_at”:1681078643,“operating”:0.10519099235534668}}

but not in code column, just in browser or Glide fetch column

Ah, I know what is happening, we need a simple but mandatory detail :wink:

Your original JS code returns a JSON structure and this kind of data/structure is not supported by JavaScript column (an array either!). The code must return a string as value therefore, we need to find out and select the JSON objects to assemble our string later.

So a useful code can be something like this:

 const url = `https://vektor-systems.bitrix24.ru/rest/13/4825o4an22hrm2o4/crm.contact.list.json?FILTER[PHONE]=79605463333&SELECT[ID]=`;
  const data = await fetch(url);
  const json = await data.json();   

  return json.result[0].ID +", " +
  json.result[1].ID +", " +
  json.result[2].ID +", " +
  json.total

Instead, if you want to receive the entire JSON payload from your fetch call as a text, you just need to change:

data.json() → data.text()

and the new code will be:

 const url = `https://vektor-systems.bitrix24.ru/rest/13/4825o4an22hrm2o4/crm.contact.list.json?FILTER[PHONE]=79605463333&SELECT[ID]=`;
  const data = await fetch(url);
  const json = await data.text();     //    data.json(); 

  return json 

I hope this helps fix your problem as of now.

Feliz noche!

2 Likes

Thank you very much, gvalero!
It so simple, and so useful! And its works!
Thanks!

1 Like

Hi
I have tried to make use of the above but for some reason the JavaScript is not executed.

In Glide I have a tabel with the following colums RunScript = boolean Trigger = If → then → else JavaScript = Javascript JavaScriptCode = Template

When RunScript is Checked it calls JavaScript that points to JavaScriptCode that holds this code:

let myTrigger = p1;        //  p1 is the 1st parameter
if (myTrigger == true)  
{

// Funktion til at sende status og besked
async function sendStatus(statusText, messageText) {
  const url = 'https://www.amck.dk/wp-json/amck-status/v1/update';

  // Data som application/x-www-form-urlencoded
  const body = new URLSearchParams({
    Status: 'Open',
    Message: 'This is a test 33333333'
  });

  try {
    const response = await fetch(url, {
      method: 'POST',
      headers: {
        'API-key': '670efc9736bb7ea43ab22baed1006cd4',
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      body: body.toString()
    });

    // Forventer JSON-svar som f.eks. { ""success"": true }
    const result = await response.json();
    console.log('Server svar:', result);
    return result;
  } catch (error) {
    console.error('Fejl ved kaldet:', error);
    throw error;
  }
}

sendStatus('', '');
}

I have created a button that sets Runscript True, waits i 3 sec and set Runscript false

If I run the JavaScript from VSCode it works fine.

What have I done wrong or misunderstund ?

Thanks

Br.

Peter

Two things I’m seeing initially…

  • To call an async function, you will need an await on the function call. Otherwise it will return before the async call can finish. Async calls run in completely different threads so they can run asynchronously alongside other code at the same time. That’s why you need to “await” so it can wait for a response before it continues running the code after the parent call.
  • Also, even though you have a return in the sendStatus function, you will also need a return on the call to the sendStatus function because something needs to “return” the result back to the column itself as a value you can see in the column cell.
1 Like

Thanks Jeff - I tried but I could not make it work. I have now implemented a webhook that calls a service on Make.com and this calls the post request that I tried to call through JavaScript. It works perfectly

Thanks
Peter