Sunday, January 8, 2017

Productivity: Case email bounce back analysis by querying, parsing result and printing to CSV from Salesforce.com Developer console

Use-case: Case email bounce back analysis by querying, parsing the result and printing to CSV using a single code snippet from Salesforce.com Developer console #Productivity

What the following sample script does?: 1) Queries for the undelivered emails resulted in cases created via "Email to case".
 2) Parses the bounced emails from the result
 3) Prints the modified results on CSV which will be stored in Salesforce.com Files.
 4) The code can be run from salesforce.com developer console in a short time and does not need to be maintained on the salesforce.com platform.

Sample script:
String csv = 'Id,Description,EmailAddress\n';
          String regex = '>>> ([a-zA-Z0-9_\\-\\.]+)@(((\\[a-z]{1,3}\\.[a-z]{1,3}\\.[a-z]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3}))';
         Pattern myPattern = Pattern.compile(regex );

for ( Case cas : [ Select Id,Description
from Case
where <<filter conditions>>
order by createddate asc limit 10]
) {
Matcher myMatcher = myPattern.matcher(acct.description.escapeCsv());
if (myMatcher.find())
       csv += cas.id + ',' + cas.description.escapeCsv() + ',' + myMatcher.group().split('>>> ')[1].trim() + '\n';
else
      csv += cas.id + ',' + cas.description.escapeCsv() + ',' + '' + '\n';

}

ContentVersion file = new ContentVersion(
title = 'cases.csv',
versionData = Blob.valueOf( csv ),
pathOnClient = '/cases.csv'
);

insert file;
System.debug('this is file:' + file );


3 comments:

  1. This code did not work for me. Note, the first for loop is not closed, and that you're for some reason looping through the cases twice. First one attempts to get back a List of cases, when a soql query only returns a List not a List>

    I modified the code to simply get back a list of Account names:
    String csv = 'Id,name\n';
    for ( Account acc : [ Select Id,Name from Account
    order by createddate asc limit 10] ) {
    String regex = '>>> ([a-zA-Z0-9_\\-\\.]+)@(((\\[a-z]{1,3}\\.[a-z]{1,3}\\.[a-z]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3}))';
    Pattern myPattern = Pattern.compile(regex );
    Matcher myMatcher = myPattern.matcher(acc.name.escapeCsv());
    if (myMatcher.find())
    csv += acc.id + ',' + acc.name.escapeCsv() + ',' + myMatcher.group().split('>>> ')[1].trim() + '\n';
    else
    csv += acc.id + ',' + acc.name.escapeCsv() + '' + '\n';

    }

    ContentVersion file = new ContentVersion(
    title = 'acc.csv',
    versionData = Blob.valueOf( csv ),
    pathOnClient = '/acc.csv'
    );

    insert file;
    System.debug('this is file:' + file );

    ReplyDelete
    Replies
    1. Your pattern compiler does not need to be in the loop:

      String csv = 'Id,name\n';
      String regex = '>>> ([a-zA-Z0-9 ])';
      Pattern myPattern = Pattern.compile(regex );
      for ( Account acc : [ Select Id,Name from Account
      order by createddate asc limit 10] ) {
      Matcher myMatcher = myPattern.matcher(acc.name.escapeCsv());
      if (myMatcher.find())
      csv += acc.id + ',' + acc.name.escapeCsv() + ',' + myMatcher.group().split('>>> ')[1].trim() + '\n';
      else
      csv += acc.id + ',' + acc.name.escapeCsv() + '' + '\n';

      }

      ContentVersion file = new ContentVersion(
      title = 'acc.csv',
      versionData = Blob.valueOf( csv ),
      pathOnClient = '/acc.csv'
      );

      insert file;
      System.debug('this is file:' + file );

      Delete
  2. Thanks Nick for the update! it must be a typo.

    ReplyDelete