Skip to main content
formatting edits
Source Link
zachelrath
  • 9.6k
  • 3
  • 41
  • 62
  1. To create CSV fields in JavaSCript, here's some a method that will work:

    // Escapes a String such that it can be used in a CSV cell // Based on Apache Commons StringEscapeUtils library // (see Wikipedia and RFC 4180) // Parameters: // input - the input CSV column String, may be null // Returns: // the input String, enclosed in double quotes // if the value contains a comma, newline or double quote, // '' if null string input. // Also, any double quote characters in the value are escaped with another double quote.

    var QUOTE = '"', ESCAPED_QUOTE = """", CHARS_THAT_MUST_BE_QUOTED = [ ',' , '"' , '\n' ];

     // Escapes a String such that it can be used in a CSV cell
     // Based on Apache Commons StringEscapeUtils library
     // (see Wikipedia and RFC 4180)
     // Parameters:
     //  input - the input CSV column String, may be null
     //  Returns:
     //  the input String, enclosed in double quotes 
     //      if the value contains a comma, newline or double quote, 
     //      '' if null string input.
     //  Also, any double quote characters in the value
     // are escaped with another double quote.
    
     var QUOTE = '"',
         ESCAPED_QUOTE = "\"\"",
         CHARS_THAT_MUST_BE_QUOTED = [ ',' , '"' , '\n' ];
    
     var escapeCSVCell = function(c) {
        if ( c.indexOf( QUOTE ) > -1 ) c = c.replace(/"/g, ESCAPED_QUOTE );
           $.each(CHARS_THAT_MUST_BE_QUOTED,function(i,char){
              if (c.indexOf(char) > -1) {
                 c = QUOTE + c + QUOTE;
                 return false;
              }
           });
        return cc;
     }
    
     // Call escapeCSVCell on each CSV field
    

    }

    // Call escapeCSVCell on each CSV field

  1. To create CSV fields in JavaSCript, here's some a method that will work:

    // Escapes a String such that it can be used in a CSV cell // Based on Apache Commons StringEscapeUtils library // (see Wikipedia and RFC 4180) // Parameters: // input - the input CSV column String, may be null // Returns: // the input String, enclosed in double quotes // if the value contains a comma, newline or double quote, // '' if null string input. // Also, any double quote characters in the value are escaped with another double quote.

    var QUOTE = '"', ESCAPED_QUOTE = """", CHARS_THAT_MUST_BE_QUOTED = [ ',' , '"' , '\n' ];

     var escapeCSVCell = function(c) {
     if ( c.indexOf( QUOTE ) > -1 ) c = c.replace(/"/g, ESCAPED_QUOTE );
     $.each(CHARS_THAT_MUST_BE_QUOTED,function(i,char){
         if (c.indexOf(char) > -1) {
             c = QUOTE + c + QUOTE;
             return false;
         }
     });
     return c
    

    }

    // Call escapeCSVCell on each CSV field

  1. To create CSV fields in JavaSCript, here's some a method that will work:

     // Escapes a String such that it can be used in a CSV cell
     // Based on Apache Commons StringEscapeUtils library
     // (see Wikipedia and RFC 4180)
     // Parameters:
     //  input - the input CSV column String, may be null
     //  Returns:
     //  the input String, enclosed in double quotes 
     //      if the value contains a comma, newline or double quote, 
     //      '' if null string input.
     //  Also, any double quote characters in the value
     // are escaped with another double quote.
    
     var QUOTE = '"',
         ESCAPED_QUOTE = "\"\"",
         CHARS_THAT_MUST_BE_QUOTED = [ ',' , '"' , '\n' ];
    
     var escapeCSVCell = function(c) {
        if ( c.indexOf( QUOTE ) > -1 ) c = c.replace(/"/g, ESCAPED_QUOTE );
           $.each(CHARS_THAT_MUST_BE_QUOTED,function(i,char){
              if (c.indexOf(char) > -1) {
                 c = QUOTE + c + QUOTE;
                 return false;
              }
           });
        return c;
     }
    
     // Call escapeCSVCell on each CSV field
    
changed description of 1st solution
Source Link
zachelrath
  • 9.6k
  • 3
  • 41
  • 62
  1. use Batch Apex, passing in the initial Lists as stateful variables using Database.Stateful, and then creating a CSV row out of each SObject during the Batch Apex execute method. In the finish method of your Batch Apex, send an email containing the generated CSV. Using Batch Apex to do the CSV row generation will avoid the Too Many Script Statements error, since Batch Apex limits are reset for each Batch Apex execute method invocation. The resultant CSV file can be stored as a String instance variable, and converted to a Blob in the finish method, then emailed to the user requesting the export.
  2. Create the blob client-side using JavaScript (no script statement limits there!), and sent to Apex using one of the following

Example of Batch Apex

  1. use Batch Apex, passing in the initial Lists as stateful variables using Database.Stateful
  2. Create the blob client-side using JavaScript (no script statement limits there!), and sent to Apex using one of the following
  1. use Batch Apex, passing in the initial Lists as stateful variables using Database.Stateful, and then creating a CSV row out of each SObject during the Batch Apex execute method. In the finish method of your Batch Apex, send an email containing the generated CSV. Using Batch Apex to do the CSV row generation will avoid the Too Many Script Statements error, since Batch Apex limits are reset for each Batch Apex execute method invocation. The resultant CSV file can be stored as a String instance variable, and converted to a Blob in the finish method, then emailed to the user requesting the export.
  2. Create the blob client-side using JavaScript (no script statement limits there!), and sent to Apex using one of the following

Example of Batch Apex

changed some section headings
Source Link
zachelrath
  • 9.6k
  • 3
  • 41
  • 62

To create CSV fields in JavaSCript, here'sHere is some asample code for the JavaScript method that will work:

// Escapes a String such that it can be used in a CSV cell
// Based on Apache Commons StringEscapeUtils library
// (see Wikipedia and RFC 4180)
// Parameters:
//  input - the input CSV column String, may be null
//  Returns:
//  the input String, enclosed in double quotes 
//      if the value contains a comma, newline or double quote, 
//      '' if null string input.
//  Also, any double quote characters in the value are escaped with another double quote.

var QUOTE = '"',
    ESCAPED_QUOTE = "\"\"",
    CHARS_THAT_MUST_BE_QUOTED = [ ',' , '"' , '\n' ];

    var escapeCSVCell = function(c) {
    if ( c.indexOf( QUOTE ) > -1 ) c = c.replace(/"/g, ESCAPED_QUOTE );
    $.each(CHARS_THAT_MUST_BE_QUOTED,function(i,char){
        if (c.indexOf(char) > -1) {
            c = QUOTE + c + QUOTE;
            return false;
        }
    });
    return c
}

// Call escapeCSVCell on each CSV field
  1. To create CSV fields in JavaSCript, here's some a method that will work:

    // Escapes a String such that it can be used in a CSV cell // Based on Apache Commons StringEscapeUtils library // (see Wikipedia and RFC 4180) // Parameters: // input - the input CSV column String, may be null // Returns: // the input String, enclosed in double quotes // if the value contains a comma, newline or double quote, // '' if null string input. // Also, any double quote characters in the value are escaped with another double quote.

    var QUOTE = '"', ESCAPED_QUOTE = """", CHARS_THAT_MUST_BE_QUOTED = [ ',' , '"' , '\n' ];

     var escapeCSVCell = function(c) {
     if ( c.indexOf( QUOTE ) > -1 ) c = c.replace(/"/g, ESCAPED_QUOTE );
     $.each(CHARS_THAT_MUST_BE_QUOTED,function(i,char){
         if (c.indexOf(char) > -1) {
             c = QUOTE + c + QUOTE;
             return false;
         }
     });
     return c
    

    }

    // Call escapeCSVCell on each CSV field

To create CSV fields in JavaSCript, here's some a method that will work:

// Escapes a String such that it can be used in a CSV cell
// Based on Apache Commons StringEscapeUtils library
// (see Wikipedia and RFC 4180)
// Parameters:
//  input - the input CSV column String, may be null
//  Returns:
//  the input String, enclosed in double quotes 
//      if the value contains a comma, newline or double quote, 
//      '' if null string input.
//  Also, any double quote characters in the value are escaped with another double quote.

var QUOTE = '"',
    ESCAPED_QUOTE = "\"\"",
    CHARS_THAT_MUST_BE_QUOTED = [ ',' , '"' , '\n' ];

    var escapeCSVCell = function(c) {
    if ( c.indexOf( QUOTE ) > -1 ) c = c.replace(/"/g, ESCAPED_QUOTE );
    $.each(CHARS_THAT_MUST_BE_QUOTED,function(i,char){
        if (c.indexOf(char) > -1) {
            c = QUOTE + c + QUOTE;
            return false;
        }
    });
    return c
}

// Call escapeCSVCell on each CSV field

Here is some sample code for the JavaScript method:

  1. To create CSV fields in JavaSCript, here's some a method that will work:

    // Escapes a String such that it can be used in a CSV cell // Based on Apache Commons StringEscapeUtils library // (see Wikipedia and RFC 4180) // Parameters: // input - the input CSV column String, may be null // Returns: // the input String, enclosed in double quotes // if the value contains a comma, newline or double quote, // '' if null string input. // Also, any double quote characters in the value are escaped with another double quote.

    var QUOTE = '"', ESCAPED_QUOTE = """", CHARS_THAT_MUST_BE_QUOTED = [ ',' , '"' , '\n' ];

     var escapeCSVCell = function(c) {
     if ( c.indexOf( QUOTE ) > -1 ) c = c.replace(/"/g, ESCAPED_QUOTE );
     $.each(CHARS_THAT_MUST_BE_QUOTED,function(i,char){
         if (c.indexOf(char) > -1) {
             c = QUOTE + c + QUOTE;
             return false;
         }
     });
     return c
    

    }

    // Call escapeCSVCell on each CSV field

described how to parse a CSV blob in JavaScript
Source Link
zachelrath
  • 9.6k
  • 3
  • 41
  • 62
Loading
Source Link
zachelrath
  • 9.6k
  • 3
  • 41
  • 62
Loading