0

I'm trying to receive some data being sent to a webhook and work with it. I'm able to receive the data and convert it to a json String however I want to assign the json String to a global variable so I can use it elsewhere, and in other functions.

I'm declaring the variable first and then trying to assign the json string to it when i receive it but it doesn't seem to be working - the variable is still 'undefined'

var jsonData;

function doPost(e){
    try{
        var jsonString = e.postData.getDataAsString();
        setLog("***json String = " + jsonString + " ***");
        jsonData = JSON.parse(jsonString);
    }
    catch(e){           
        setLog("***Exception occured = "+JSON.stringify(e) + " ***");  
    }
}

I'm quite new to Javascript and I'm not sure where I'm going wrong with this.

When looking elsewhere I've found that global variables will be changed whenever the script runs as it's Google Apps Script, but I don't need these variables to remain the same after each time the script runs. I just need the global variable to use within other functions.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
Adam Roberts
  • 562
  • 1
  • 5
  • 20

1 Answers1

0

You want to use jsonData at other functions. I think that there are 3 patterns for your situation.

Pattern 1

This is a simple solution. You redeclare jsonData in doPost(e). By this, undefined occurs. So modify as follows. In this case, jsonData is cleared after the running script was finished.

From :
var jsonString = e.postData.getDataAsString();
To :
jsonString = e.postData.getDataAsString(); // Remove "var"

Pattern 2

This is a solution that even if the running script is finished and it is run again, jsonData can be used. But there is a time limit. In this case, it uses Cache Service. When Cache Service is used, the value is required to be the string data.

function doPost(e){
    try{
        var jsonString = e.postData.getDataAsString();
        setLog("***json String = " + jsonString + " ***");
//        jsonData = JSON.parse(jsonString);
        var cache = CacheService.getScriptCache();
        cache.put('jsonString', jsonString, 1500); // cache for 25 minutes
    }
    catch(e){           
        setLog("***Exception occured = "+JSON.stringify(e) + " ***");  
    }
}

// When you use jsonData, please JSON.parse() like this function.
function myFunction() {
  var cache = CacheService.getScriptCache();
  var jsonString = cache.get('jsonString');
  var jsonData = JSON.parse(jsonString);

  // do something
}

Pattern 3

This is a solution that even if the running script is finished and it is run again, jsonData can be used. In this case, it uses Properties Service. When Properties Service is used, the value is required to be the string data and there is no time limit.

function doPost(e){
    try{
        var jsonString = e.postData.getDataAsString();
        setLog("***json String = " + jsonString + " ***");
//        jsonData = JSON.parse(jsonString);
        var scriptProperties = PropertiesService.getScriptProperties();
        scriptProperties.setProperty('jsonString', jsonString); // Save jsonString to the property.
    }
    catch(e){           
        setLog("***Exception occured = "+JSON.stringify(e) + " ***");  
    }
}

// When you use jsonData, please JSON.parse() like this function.
function myFunction() {
  var scriptProperties = PropertiesService.getScriptProperties();
  var jsonString = scriptProperties.getProperty('jsonString');
  var jsonData = JSON.parse(jsonString);

  // do something
}

If I misunderstand your situation, I'm sorry.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Sorry if I didn't explain the question correctly. The webhook is receiving objects from a unified email inbox, so I can collate the data and send an SMS via an API based on specific email addresses. Would using the Cache Service or Properties Service allow this? i.e. if 1 email was received and the data stored to the Cache Service, would a second email overwrite this or would it cause an issue? – Adam Roberts Apr 20 '18 at 08:05
  • @Adam Roberts I'm sorry for the inconvenience. I couldn't understand your situation. Can you show us the detail information? I'm really sorry for my poor English skill. – Tanaike Apr 20 '18 at 08:13
  • No problem at all. I'm using the webhook mentioned here https://developer.helpscout.com/help-desk-api/webhooks/ to send an SMS whenever an email is received. So if I store this information in the Cache Service and another email is received before the cache clears - would it overwrite the previous data? – Adam Roberts Apr 20 '18 at 08:32
  • @Adam Roberts Thank you for the additional information. Yes. When the same key is used, the value is overwrote. – Tanaike Apr 20 '18 at 08:34
  • Thanks very much for your help! This is exactly what I needed – Adam Roberts Apr 20 '18 at 08:47
  • @Adam Roberts I'm glad your issue was solved. Thank you, too. – Tanaike Apr 20 '18 at 08:56