Skip to main content
1-Visitor
March 13, 2013
Question

Consolidate information into one email.

  • March 13, 2013
  • 1 reply
  • 3079 views

I'm looking for the best way to consolidate information into one email for a scheduled (weekly or monthly) report. I have found using emailAdvanced.js will get me close to what I want, but it sends a user an email for each item. Instead, if the query returns multiple items for the same user, I would like the user to receive only one email. I have also looked at ReportLinkEmail.js, but this script is only allowed to be used as a rule based trigger, and not a scheduled trigger. Does anyone have any recommendations for modifying either of these scripts to do what I want? Are there alternatives that I'm not aware of?

    1 reply

    1-Visitor
    March 13, 2013

    Hi Christina,

    This is definitely something that customizing a trigger script can deliver.

    You can start with emailAdvanced.js and change how the bulk processing works (so that it clumps items for the same user instead of 1 email per item) or you can start from ReportLinkEmail.js and change it so that it allows bulk processing.

    You could also start from scratch and write a new trigger script to do exactly what you want. At the end of that exercise you will probably have a greater understanding of how the trigger scripts work as well as a trigger script that exactly meets your needs, but it will take a while to do.

    In any case, the key to understanding how to create or modify these triggers will start with how the various kinds of triggers differ.

    The main difference between a rule based trigger and a scheduled trigger is how you get to the "issueDeltaBean". The issueDeltaBean is the mechanism for reading the values of fields from an item or writing new field values to an item, so it is used in pretty much every workflow and documents trigger.

    In a rule based trigger, the issueDeltaBean represents the single item that the trigger is running against. If you set up a trigger to fire when the state changes from Active to Complete, and the state of item 1234 changes from Active to Complete, then your trigger will get an issueDeltaBean representing item 1234.

    In most triggers, the way to get this issueDeltaBean is to use 'bsf.lookupBean("imIssueDeltaBean")'

    In a scheduled trigger, the trigger is passed an array of issue IDs representing every item in the query run when the trigger executes. Most triggers handle this array by looping over each ID, loading the issueDeltaBean from that ID and processing it individually. Some example code which does this in our triggers is:

    var beanServer = bsf.lookupBean("imServerBean");

    var args = bsf.lookupBean("imScheduleTriggerArgumentsBean");

    var issues = args.getIssues();

    for ( var i = 0; i < issues.length; i++ ) {

    beanDelta = beanServer.getIssueDeltaBean( issues[i] );

    if (beanDelta != null) {

    common(beanDelta);

    }

    }

    So, for changing the ReportLinkEmail.js to handle scheduled triggers, you would need to build something like the above.

    At the same time, all this does is process the individual issueDeltaBeans separately (like emailAdvanced,js already does), but you want to process them together so that you can group the items and send out a single email per user.

    To do this grouping, you will probably want to add some variables (maybe from Java collections) which keep track of the list of issueDeltaBeans applicable to each user as each issueDeltaBean is processed. After all the issueDeltaBeans have been handled, you will want to loop over those variables and use the data to construct the emails to send to each individual user.

    A final word of caution is that storing all that data during a scheduled trigger to bulk process later on will cause the trigger to run slower and use more memory. The performance hit will scale up with the number of items returned in the scheduled trigger's query. I would not imagine that this would take down a well performing server, but it is something to keep in mind while designing, testing, and using the new trigger.

    Hope that helps,

    Matt

    crunge1-VisitorAuthor
    1-Visitor
    March 14, 2013

    Thanks, this information was very helpful. Is there an easy way to sort the results of the query by the user I want to email the results to? That may reduce the amount of information I need to store.

    1-Visitor
    March 18, 2013

    Unfortunately I don't think its possible to sort the results in the query itself and have the trigger use that. I just tried with a test trigger and no matter what the sorting was on the query, the trigger still processed them in the order of their IDs.

    The data structure you use to handle the data before your second processing loop could hold it in a sorted order, such as a java treemap or treeset, but that seems to be it.