REQUIREMENT
Develop a script to populate the email of the Contact flagged for receiving the Invoice to the Message Wideget opened while sending email from the Invoice record.
SOLUTION
Following userevent script can be used to populate the email address in the email widget other than the email address of the entity of the invoice.
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/record', 'N/search'],
/**
* @param{record} record
*/
(record, search) => {
/**
* Defines the function definition that is executed before record is loaded.
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
* @param {Form} scriptContext.form - Current form
* @param {ServletRequest} scriptContext.request - HTTP request information sent from the browser for a client action only.
* @since 2015.2
*/
const beforeLoad = (scriptContext) => {
try {
let messageRec = scriptContext.newRecord
let transactionId = scriptContext.request.parameters.transactio
if (checkIfTransaction(transactionId)) {
if (scriptContext.type == 'create') {
let entityid = scriptContext.request.parameters.entity
let receptLineCount = messageRec.getLineCount({
sublistId: 'otherrecipientslist'
});
let contactSearchResults = fetchContactEmails(entityid)
if (contactSearchResults.length > 0) {
messageRec.setValue({
fieldId: 'recipient',
value: '',
ignoreFieldChange: false
});
messageRec.setValue({
fieldId: 'recipientemail',
value: contactSearchResults[0].emailId,
ignoreFieldChange: false
});
if (contactSearchResults.length > 1) {
let initLine = 0
for (let itr = 1; itr < contactSearchResults.length; itr++) {
messageRec.setSublistValue({
sublistId: 'otherrecipientslist',
fieldId: 'otherrecipient',
line: initLine,
value: contactSearchResults[itr].contactId
});
messageRec.setSublistValue({
sublistId: 'otherrecipientslist',
fieldId: 'email',
line: initLine,
value: contactSearchResults[itr].emailId
});
messageRec.setSublistValue({
sublistId: 'otherrecipientslist',
fieldId: 'toRecipients',
line: initLine,
value: true
});
initLine++
}
}
else if (contactSearchResults.length == 1) {
for (let i = 0; i < receptLineCount; i++) {
messageRec.removeLine({
sublistId: 'otherrecipientslist',
line: i,
ignoreRecalc: true
});
}
}
}
}
}
}
catch (err) {
log.error("error@beforeLoad", err)
}
}
/**
* Defines the function definition that is executed before record is submitted.
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
* @since 2015.2
*/
const beforeSubmit = (scriptContext) => {
try {
let messageRec = scriptContext.newRecord
let entity=messageRec.getValue({
fieldId:'entity'
})
let contacts = fetchContactEmails(entity)
messageRec.setValue({
fieldId:'recipient',
value:contacts[0].contactId
})
}
catch (err) {
log.error("error@beforeSubmit", err)
}
}
/**
* The function is to execute a saved search to fetch the contact of customer with the receive invocie checkbox checked.
* @param {*} custId Intenal Id of the customer
* @returns array of contacts
*/
function fetchContactEmails(custId) {
try {
let customerSearchObj = search.create({
type: "customer",
filters:
[
["contact.custentity_invoicenotification", "is", "T"],
"AND",
["internalid", "anyof", custId],
"AND",
["contact.isinactive","is","F"]
],
columns:
[
search.createColumn({
name: "internalid",
sort: search.Sort.ASC,
join: "contact",
label: "Internal ID"
}),
search.createColumn({
name: "email",
join: "contact",
label: "Email"
}),
]
});
let searchResultCount = customerSearchObj.runPaged().count;
let searchResults = customerSearchObj.run().getRange({
start: 0,
end: 1000
})
let contactArray = []
if (searchResultCount > 0) {
for (let count = 0; count < searchResultCount; count++) {
let contactDet = {}
contactDet.contactId = searchResults[count].getValue({
name: "internalid",
join: "contact",
label: "Internal ID"
})
contactDet.emailId = searchResults[count].getValue({
name: "email",
join: "contact",
label: "Email"
})
contactArray.push(contactDet)
}
}
return contactArray
}
catch (err) {
log.error("error@fetchContactEmails", err)
}
}
/**
* Funciton is to check if the transaction is an invoice
* @param {*} tranId Internal Id of the transaciton
* @returns true if it is an invoice record
*/
function checkIfTransaction(tranId) {
try {
let invoiceSearchObj = search.create({
type: "invoice",
filters:
[
["internalid", "anyof", tranId],
"AND",
["type", "anyof", "CustInvc"],
"AND",
["mainline", "is", "T"]
],
columns:
[
search.createColumn({ name: "internalid", label: "Internal ID" })
]
});
let searchResultCount = invoiceSearchObj.runPaged().count;
log.debug("invoiceSearchObj result count", searchResultCount);
if (searchResultCount > 0) {
return true
}
else {
return false
}
}
catch (err) {
log.error("error@checkIfTransaction", err)
return false
}
}
return { beforeLoad, beforeSubmit }
});