App Scripts: Attempting to grab a specific form response, get a URL, and email that URL - email

I'm attempting to capture a directly link or the response ID to create a link and forward it into a ticketing queue but every function seems to give me the wrong ID.
function onFormSubmit()
{
//email content creation
var formID = '(copy/pasted from form url)';
var emailBody = "You have received a form submission: ";
var emailAddress = 'TestEmail';
var emailSubject = 'Your form has a new response';
//find the form
var form = FormApp.openById(formID)
var formResponses = form.getResponses();
//find the last response
var lastResponse = formResponses[formResponses.length-1];
var responseID = lastResponse.getID();
//build link
var url = https://docs.google.com/forms/d/ + formID + "/edit#response=" + responseID;
//send email
MailApp.sendEmail(emailAddress, emailSubject, emailBody + url);
}
Hopefully this comes off as coherent on what I'm trying to do. I've attempted to use the
onFormSubmit(e)
responseID = e.getResponses[e.length-1].getID();
but that doesn't work either.
I've found numerous posts on how to do this, but even copy/pasting the test code throws errors of getID being undefined.
If anyone can help figure this out, I would appreciate it!

Related

Send email to the new google form submission only

I'm new to the Google apps script. I wrote a script to send emails when there is a new submission from google forms using data and template from a spreadsheet. However, it sends an email to not just the new submission but also to all of the previous submissions. The whole script is quite long, so I only copy a short part of it. Is there any way to fix it?
Here is the link to the spreadsheet https://docs.google.com/spreadsheets/d/1fhuwEndIS3khg3W19jpQnBAaCp_MMrD_bfATrdf2-4I/edit?usp=sharing
Thank you.
function sendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Calculation");
var lr = ss.getLastRow();
for (var i = 3; i<=lr;i++){
var currentEmail = ss.getRange(i, 1).getValue();
var currentName = ss.getRange(i, 3).getValue();
var currentScore1 = ss.getRange(i, 4).getValue();
MailApp.sendEmail(
currentEmail,
subjectline,
"HTML",
{ htmlBody: messageBody }
);
}
}
Instead of reading the values from the spreadsheet take advantage of the form submit event object. This event object has two properties including the form submission values, one is an Array of form submission values in the same order than the sheet columns, the other is an object having a property for each question each of them having an Array of values. Ref. https://developers.google.com/apps-script/guides/triggers/events
This shows the changes that need to done to your script:
function sendEmail(e) {
var currentEmail = e.values[0];
var currentName = e.values[2];
var currentScore1 = e.values[3];
MailApp.sendEmail(
currentEmail,
subjectline,
"HTML",
{ htmlBody: messageBody }
);
}
Note: In order to make the event object work, the function should be called by a Google Sheets form submit trigger.
Related
Event values and offset in Google spreadsheet
How to send an email only to the last Google form submission?

NetSuite SuiteScript 2.0 - Attach File Object to Email Being Sent Via SuiteScript

I am writing a script where I am trying to print a transaction and then email it. I keep getting the following error.
"Wrong parameter type: options.attachments is expected as file.File[]."
I can't tell what is wrong with my code. Here is the snipped of code that is causing the issues. The email sends just fine if I don't include the attachments parameter. Any help would be greatly appreciated.
var recID = 1213;
var userID = 1478;
var emailSender = 1478;
var emailSubject = "Test Email";
var emailBody = "This is a test.";
var mainRecipient = "test#test.com";
var additionalRecipients = "";
var recPDF = render.transaction({
entityId: recID,
printMode: render.PrintMode.PDF,
});
email.send({
author: author,
recipients: mainRecipient,
subject: emailSubject,
cc: additionalRecipients,
body: emailBody,
attachments: [recPDF],
relatedRecords: {
transactionId: recID
}
});
Figured out my issue. There was a conflict attachment for some reason with the library file I had attached to the Suitelet running the script. By removing the library file, it resolved the issue.

How to send a form to my e-mail by googlescript every tuesday and every thrusday?

First of all I'm completly new at programming so I think my question is really simple but i couldn't find an answer that really matches my issue.
I created a form in googleforms and I want to send this form to my e-mail every tuesday and every thursday by googlescript. (the embed form, not the link)
Firstly, I'm not beeing successful not even sending it to my e-mail.
I tried two methods that I found here but thats what I got:
this is the code im using:
function sendForm(form,email) {
var form = FormApp.getActiveForm();
var email = "myemail#gmail.com"
var formUrl = form.getPublishedUrl();
var url = form.getPublishedUrl();
var response = UrlFetchApp.fetch(url);
var htmlBody = HtmlService
.createHtmlOutput(response)
.getContent()
;
MailApp.sendEmail({
to: email,
subject: "subject",
htmlBody: htmlBody,
});
}
The other code i tried just give me the e-mail message "HtmlOutput" and not the form.
Can someone help me?
Thanks in advance.

Facebook Id temporarily locked while fetching data from different location

I am trying to fetch comments from Facebook and trying reply that from the application using the access token again a facebook id.
When I am using that from one location continuously it works well even on dynamic IP of my system for days .. but when I tries to use the same at my home.. it is working for 2 times hardly and then blocks the ID... please help me resolve this.
Code here :
string AccessToken = "Acess token here";
GET_POST_LINK posts;
string url = "https://graph.facebook.com/v2.11/mefields=posts%7Bmessage%2Cpermalink_url%7D&access_token=" + AccessToken + "";
HttpWebRequest feedRequest = (HttpWebRequest)WebRequest.Create(url);
feedRequest.Method = "GET";
feedRequest.Accept = "application/json";
feedRequest.ContentType = "application/json; charset=utf-8";
WebResponse feedResponse = (HttpWebResponse)feedRequest.GetResponse();
using (feedResponse)
{
using (var reader = new StreamReader(feedResponse.GetResponseStream()))
{
posts = JsonConvert.DeserializeObject<GET_POST_LINK>(reader.ReadToEnd());
int count = posts.posts.data.Count;
}
}

Error while trying to use the "reply-to" in google apps script

I'm using google apps script to get the responses of a specific form in an specific e-mail,
What I'm trying to do is use a google form to open support tickets, so people need fill some fields like, title, description and e-mail,
And when they submit the form, it will automatically open a ticket, but the e-mail will be always from the owner of the form, and this was a problem because we want that the person who opened the ticket receives email updates, so what I'm trying to do is this:
I put a field in the form asking the persons email, and I'm trying to put that e-mail into the reply-to...
And apparently I'm in the right way to catch that e-mail but the reply-to don't show the email that the persons filled the box, it appears an error: [Ljava.lang.Object;#34dfe075
Does any one can help me?
Here is my script:
function Initialize() {
var triggers = ScriptApp.getProjectTriggers();
for(var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger("SendGoogleForm")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}
function SendGoogleForm(e)
{
try
{
var email = "support#email.com";
var form = e.namedValues;
var subject = form["Title"];
var s = SpreadsheetApp.getActiveSheet();
var columns = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
var message = "";
for ( var keys in columns ) {
var key = columns[keys];
if ( e.namedValues[key] && (e.namedValues[key] != "") ) {
message += key + ' :: '+ e.namedValues[key] + "\n\n";
}
}
GmailApp.sendEmail(email, subject, message, {replyTo: form["E-mail"], from: "support#email.com"});
} catch (e) {
Logger.log(e.toString());
}
}
And here is the output of this:
from: support#email.com
reply-to: [Ljava.lang.Object;#34dfe075
to: support#email.com
date: Fri, Oct 17, 2014 at 10:55 AM
subject: New Test
The reply to, is broken :(
The values returned in the e.namedValues property are arrays, so you must access them as such.
Modify your sendEmail line as follows:
GmailApp.sendEmail(email, subject, message, {replyTo: form["E-mail"][0], from: "support#email.com"});
Note the [0] array index on the form["E-Mail"] field, indicating you want the first value in that array, which will be the email address entered.
See the example next to "e.namedValues" here: https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events