Hello gurus -
I have a Map/Reduce script to send cancellation emails based off of a saved search. The email is sending just fine, however the
relatedRecords
object doesn't seem to work, when using the key transactionId
, however when using entityId
, it works fine and logs the email under the customer's Communication -> Messages subtab. Has anybody experienced this before? Any idea if I'm doing something wrong? Here is the entirety of my code, any help or insight is appreciated! /** * @NApiVersion 2.x * @NScriptType MapReduceScript * @NModuleScope SameAccount */ define(['N/email', 'N/search', 'N/render', 'N/record'], function(email, search, render, record) { var department_map = { 15: { templates: { 1: 301 // RISKIFIED DECLINED (KW) }, author: 113746, // Customer Care template_vars: { '%SALESCHANNEL%': 'KegWorks.com' } }, 35: { templates: { 1: 302 // RISKIFIED DECLINED (BTB) }, author: 21100362, // Customer Care BTB template_vars: { '%SALESCHANNEL%': 'BehindtheBar.com' } } }; function getInputData() { return search.load({ id: 'customsearch_kw_cancelledorderstoemail' }); } function map(context) { var searchResult = JSON.parse(context.value); context.write(searchResult); } function summarize(summary) { summary.output.iterator().each(function(res,v) { res = JSON.parse(res); var so_id = parseInt(res['id']); var values = res['values']; var email_address = values['email']; var customer = values['entity']['value']; var reason = values['custbody_kw_cancelemailreason']['value']; var dept = values['department']['value']; var storefront_order = values['custbody_custbody_storefront_order']; var document_number = values['tranid']; var first_name = values['firstname.customerMain']; var company_name = values['companyname.customerMain']; var template; if (email_address) { try { template = department_map[dept]['templates'][reason]; } catch (e) { log.error('Error getting template', 'Reason or Template doesn't exist. '+ 'Reason: '+reason+', dept: '+dept+', template: '+template); log.error(e); } if (template) { var merge = render.mergeEmail({ templateId: template, transactionId: so_id }); var subject = merge.subject; var body = merge.body; var date = new Date(); var template_vars = department_map[dept]['template_vars']; template_vars['%NLORDERNUM%'] = storefront_order ? storefront_order : document_number; template_vars['%NLCUSTNAME%'] = first_name ? first_name : company_name; template_vars['%NLEMAILADDRESS%'] = '<a style="color:#FFFFFF;" href="mailto:'+email_address+'">'+email_address+'</a>'; template_vars['%NLCURRYEAR%'] = date.getFullYear(); for (var v in template_vars) { var regexp = new RegExp(v, 'g'); subject = subject.replace(regexp, template_vars[v]); body = body.replace(regexp, template_vars[v]); } try { email.send({ author: department_map[dept]['author'], recipients: email_address, subject: subject, body: body, relatedRecords: { //entityId: customer, transactionId: so_id } }); } catch (e) { log.error('Error sending email', e); } try { record.submitFields({ type: record.Type.SALES_ORDER, id: so_id, values: { custbody_kw_cancelemailsent: true } }); } catch (e) { log.error('Error updating order '+so_id, e); } } } return true; }); } return { getInputData: getInputData, map: map, summarize: summarize }; });