Code service returning values from google sheets

(Ytawk) #1

I’m puzzled with an error while trying to fetch data from google sheets from a Code Service output.

When I used Seach Rows and a random number generator to randomly fetch a message from Google Sheets, and returned the result in a variable using resolve in the Code Service,

botReply = "{{google_sheets_search_rows_fetch_data.results_+_gsx:replyorders}};

then used the Result output of the code service to display the random messages in a Bot - Send a Message Service

{{code_generate_msgs.result__msg}}

the following error would occure at run time:

Script Error: SyntaxError: Invalid or unexpected token

But when using Seach Rowsdirectly in the Bot - Send a Message Service the random messages would be displayed without any problems.

{{google_sheets_search_rows_fetch_data.results_+_gsx:replyorders}}

Is there a bug somewhere or might I be doing something wrong?

Thank you,

(Kellsey Shaw) #2

Hi there,

Collection Outputs can’t be used in the Code service. To get data into the code service you need to use inputs. You have the options on the settings screen for the code service to set the name of the input and then give it the value.

{{google_sheets_search_rows_fetch_data.results_+_gsx:replyorders}} would be the value.

So if you called it GoogleData you would reference in the code botReply = inputs.GoogleData;

Hopefully, this all makes sense :slight_smile:

(Ytawk) #3

Hi Kellsey,

Thanks, the input method worked perfectly :slight_smile: , but I am having another issue, the Code Service output is returning a blank message, and causing the following error:

Message can’t be blank

I am referencing the inputs to the variable:

botReply = inputs.replyorders;

And then referencing it and a bunch of other variables into a JSON format:

var result = {“msg”: botReply, “type”:msgType,“priority”: priority};

But as I mentioned the botReply variable is returning a blank value.

EDIT

Please note, that the output of the Code Service is as below, the botReply variable has been ommited.

Result
{
“type”: 1,
“priority”: “a”
}

:white_check_mark: Solution by Kellsey Shaw post # 8

(Kellsey Shaw) #4

Hi,

Probably best to just double check that your input has been done correctly. You should be able to see the input values in your logs to confirm that a value was inserted.

(Ytawk) #5

Hi Kellsey,

The inputs are being successfully inserted, below is part of the log report:

Inputs
{
“Sure, take a look at our menu”: “replyorders”,
“How can I help you?”: “assistance”,
“Hey there”: “greetings”
}

Output Data:
Result
{
“type”: 1,
“priority”: “a”
}

(Kellsey Shaw) #6

Hi,

I’ll need to take a look at this further for you.

Are you able to share your flow or code with me? You can send it to support@flowxo.com to keep it private if you prefer.

(Ytawk) #7

Sure, I’ll send you the code by mail.

Thanks.

(Kellsey Shaw) #8

Well, your code works fine for me but I did find two issues.

In your code you have var userMsg = "{{bot_catchall.message}}";

This will be taken as a string rather than getting the message from the bot. That will need to moved to an input same as your other values.

Your inputs appear to be set up wrong. Yours are showing the value on the left side but that should be the name.

My logs show:

Inputs
{
“replyorders”: “Sure, take a look at our menu”,
“greetings”: “How can I help you?”,
“assistance”: “Hey there”
}

Which is the opposite way round to yours. When I ran your code as it is I got this output:

Result
{
“msg”: “How can I help you?”,
“type”: 3,
“priority”: “c”
}

(Ytawk) #9

Hello Kellsey,

Thank you :slight_smile: the issue has been resolved, it’s my mistake, I inverted the values in the Input fields, and did not use "{{bot_catchall.message}}" through an input :flushed:

Anyway thank you for the amazing support :smiley:

(Kellsey Shaw) #10

Ah no problem :slight_smile:

Glad you got it sorted :thumbsup:

1 Like