1

What I'm trying to do, is to fill a Data Extension using SSJS on weather column. (For the test I'm using nodeJS)

Data Extension : "Weather API"

weather  |     city name       |  country code

(empty)  |     Bordeaux        |     FR
(empty)  |      Bréval         |     FR
(empty)  |     Brussels        |     BE
(empty)  |    Flers, Orne      |     FR
(empty)  |     Frankfurt       |     DE
(empty)  |  Grandville, Aube   |     FR
(empty)  |      Le Havre       |     FR
(empty)  |      London         |     GB
(empty)  |    Los Angeles      |     US

I'm using the following API : https://openweathermap.org/api

Here is my code :

const https = require('https');
var cities = [
    {cityName: 'Bordeaux',countryCode: 'FR'},
    {cityName: 'Breval',countryCode: 'FR'},
    {cityName: 'Brussels',countryCode: 'BE'},
    {cityName: 'Flers, Orne',countryCode: 'FR'},
    {cityName: 'Frankfurt',countryCode: 'DE'},
    {cityName: 'Grandville, Aube',countryCode: 'FR'},
    {cityName: 'Le Havre',countryCode: 'FR'},
    {cityName: 'London',countryCode: 'GB'},
    {cityName: 'Los Angeles',countryCode: 'US'},
]
let APIKey = '...........................'
var timesRun = 0;
var tmpDataObj = []
const targetDataExtension = DataExtension.Init("Weather API");
const interval = setInterval(function(){
    https.get("https://api.openweathermap.org/data/2.5/forecast?q="+cities[timesRun].cityName+","+cities[timesRun].countryCode+"&units=metric&APPID="+APIKey, (resp) => {
        let data = '';
        // A chunk of data has been recieved.
        resp.on('data', (chunk) => {
            data += chunk;
        });

        // The whole response has been received. Print out the result.
        resp.on('end', () => {
            let global = JSON.parse(data)
            let weather = global.list[global.list.length - 1].weather[0].main
            let weatherObj = {
                cityName: global.city.name,
                weather: weather
            }
            tmpDataObj.push(weatherObj)
            console.log(tmpDataObj);
            timesRun += 1;
            if(timesRun === cities.length){
                clearInterval(interval);
                for (let i = 0; i < tmpDataObj.length; i++) {
                    targetDataExtension.Rows.Update({"weather":tmpDataObj[i].weather}, ["city name"],[tmpDataObj[i].cityName]);
                }
            }
        });
    })
}, 2000);

Here is what I retrieve on my terminal :

➜  test node aaaa.js
{ cityName: 'Bordeaux', weather: 'Rain' }
{ cityName: 'Breval', weather: 'Rain' }
{ cityName: 'Brussels', weather: 'Clouds' }
{ cityName: 'Flers', weather: 'Clear' }
{ cityName: 'Frankfurt', weather: 'Clouds' }
{ cityName: 'Grandville', weather: 'Rain' }
{ cityName: 'Le Havre', weather: 'Clear' }
{ cityName: 'London', weather: 'Clear' }
{ cityName: 'Los Angeles', weather: 'Clear' }
➜  test |

So at this point everything is OK for me. But when run this code on Script Activity in Automation Studio nothing append ... I have no idea why. This is my first time using Script Activity.

Thanks for your help !!!

  • 1
    SFMC's SSJS is pretty ancient (ES3) so while the test code works in Node.js, it most likely won't translate over to SFMC. I would try getting your code to run successfully first in a one-off landing page (CloudPages) or test email preview before copying/pasting the code directly into a script activity. – Mark G May 31 '19 at 12:40
  • Thanks for your comment ! Do you know any documentation on ES3 ? thxx – UnknownZSH Jun 3 '19 at 8:00
  • It's pretty dense, so your best bet would be to go through the SFMC-specific SSJS docs instead: developer.salesforce.com/docs/… – Mark G Jun 3 '19 at 16:17
0

I agree with @MarkG, but one thing I noticed is that you are using JSON.parse(). With SSJS, you should use Platform.Function.ParseJSON()

https://developer.salesforce.com/docs/atlas.en-us.mc-programmatic-content.meta/mc-programmatic-content/ssjs_platformUtilityParseJSON.htm

| improve this answer | |
  • Thanks for your comment ! Platform.Function.ParseJSON() is specific to Salesforce ? – UnknownZSH Jun 3 '19 at 7:59
  • Yes the Platform library provides functions to access Salesforce Marketing Cloud features. As mentioned previously Node != Salesforce SSJS, you will need to use only ES3 features. You can't use let, const, and JSON methods aren't ES3. Review the Salesforce SSJS documentation – Josh de Blank Jun 4 '19 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.