/* * Description: add more capability to a message when clicking * the Email button inside the message subtab */ /** * @NApiVersion 2.1 * @NScriptType UserEventScript * @NModuleScope Public */ define(['N/record', 'N/search', 'N/runtime', 'N/error'], function(record, search, runtime, error) { var scriptName = "article_message_ue_21."; /* --------------------------------------------------- */ function beforeLoad(context) { var funcName = scriptName + "beforeLoad " + context.type + " " + context.newRecord.type + " " + context.newRecord.getValue("id") + " via " + runtime.executionContext; try { var REC = context.newRecord; //run only if in create on the user interface if (context.type != context.UserEventType.CREATE) return; if (runtime.executionContext != runtime.ContextType.USER_INTERFACE) return; //get the values on the url parametes to help see the composer source var str =""; var params = context.request.parameters; log.debug(funcName,'params: '+JSON.stringify(params)); var rootRecType = ''; var recid = ''; //use the following source pattern the help drive the value lookups if(!isEmpty(params.transaction)) { recid = params.transaction; rootRecType= 'TRANSACTION'; } else if(!isEmpty(params.record)) { recid = params.record; rootRecType = 'CUSTOMRECORD'; } else { recid = params.entity; rootRecType = 'ENTITY'; } log.debug(funcName,'recid: '+recid); if(isEmpty(recid)){ log.debug(funcName, 'No record or transaction found'); return; } //now do the lookup work which will vary based on your solution var rectype = ''; var email = ''; if(rootRecType == 'TRANSACTION') { lookupResults = search.lookupFields({ type: search.Type.TRANSACTION, id: recid, columns: ['recordtype', 'email'] }) rectype = lookupResults.recordtype email = lookupResults.email } else if(rootRecType == 'CUSTOMRECORD'){ rectype = params.recordtype; } else if(rootRecType == 'ENTITY'){ rectype = params.entitytype; } log.debug(funcName,'rectype: '+ rectype); log.debug(funcName,'email: '+ email); if(isEmpty(rectype)){ log.debug(funcName, 'No record type found'); return; } //in this solution, we know the email list is semicolon separated var emaillist = email.split(';') log.debug(funcName,'emaillist: '+ JSON.stringify(emaillist)); //add to the email sublist the emails that were selected var line = 0 emaillist.forEach((element) => { log.debug(funcName,'emaillist: '+ line + ':' + element); if (element){ REC.insertLine({ sublistId: 'otherrecipientslist', line: line }); REC.setSublistValue({ sublistId: 'otherrecipientslist', fieldId: 'email', line: line, value: element }); // use 'cc' or 'bcc' for other fields REC.setSublistValue({ sublistId: 'otherrecipientslist', fieldId: 'toRecipients', line: line, value: 'T' }); } line++ }); } catch (e) { log.error(funcName, e); } } /* --------------------------------------------------- */ function isEmpty(val) { if(typeof(val) == 'object'){ for(var key in val) { if(val.hasOwnProperty(key)) return false; } return true; } else{ return (val == null || val == '' || val == undefined); } } return { beforeLoad: beforeLoad, } });