Building a CSV-upload SendGrid email validation frontend

Hi everyone!

I'm building a simple CSV-upload driven frontend for SendGrid's email validation API tool.

I was following this example documentation, but I can't seem to get the script to make the query run for each row.

This is the application:

This is the script:

var rows = fileInput1.parsedValue;
var errors = '';
var total = table1.displayedData.length;

function runQuery (i) {
  // Update the statusText with the current progress
  statusText.setValue(i.toString() + '/' + total.toString())
  
  if (i >= rows.length) {
    console.log('Finished running all queries');
    return;
  }

  console.log('Running query for row', i);

  query1.trigger({
    additionalScope: { i: i }, // This is where we override the `i` variable from step 2!
    // You can use the argument to get the data with the onSuccess function
    onSuccess: function(data) {
      runQuery(i + 1);
    },
    onFailure: function(error) {
      // Update the errorsText with all the errors encountered
      errors += 'Found error at line ' + i.toString() + ':  ' + error +  '\n\n';
      errorText.setValue(errors);
      runQuery(i + 1);
    }
  });
}

runQuery(0);

And finally, this is the configuration for the SendGrid query:

Any help would be much appreciated!

(P.S. - I'm a relative javascript novice. I only know enough to be dangerous. Sorry if this is an easy fix that I'm just unaware of!)

Hey @samantha, welcome to the community :hugs:

Try this for your definitions

var rows = fileInput1.parsedValue[0]
var total = rows.length;

In the additionalScope for your query I would use the email variable rather then the index number.

additionalScope: {
  email: rows[i].Email
}

In your sendgrid query you can access that variable with {{ email }}.

Does that help?

@minijohn Thank you very much!

Aha - progress. Now we're iterating through rows, but getting the error:

query1: {"status":400,"message":"","errors":[{"message":"email is required"}]}

So I presume this means the variable isn't quite working. Just {{ email }} doesn't seem to want to validate, so I tried this:
image

But it doesn't actually work when the script is run. Any thoughts?

Hey @samantha,

seems like either the parameter you're sending to the API is not called body.email or the rows[i].Email is not accessing the data correctly.

I would still use the additionalScope with the {{email}} param as reading data from a table could introduce problems later on.

Add a console log right under console.log('Running query for row', i); like this:

console.log(rows[i])

to check the variable's output in your browsers console.