Problem:
Send an when creating the Customer Payment through the customer portal and also the payment method is Credit Card.
Attach Payment receipt PDF along with the email. (preferred PDF template)
The sender is admin.
The recipient is the customer.
Solution:
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
/************************************************************************************************
CDU - 698 Send Email on customer Payment from Customer portal
*********************************************************************************************
*
* Author: Jobin & Jismi IT Services LLP
*
* Date Created : 19-November-2021
*
* Description : 698 Send Email on customer Payment from Customer porta
*
*
*
***********************************************************************************************/
define([ 'N/email', 'N/record', 'N/runtime', 'N/search','N/render'],
/**
* @param{email} email
* @param{file} file
* @param{record} record
* @param{runtime} runtime
* @param{search} search
*/
( email, record, runtime, search,render) => {
const afterSubmit = (scriptContext) => {
try {
var customeRole=runtime.getCurrentUser().role
log.debug('role',customeRole);
var currentRec = scriptContext.newRecord;
var paymentMethod = currentRec.getValue({
fieldId: 'paymentmethod'
});
log.debug('payment',paymentMethod)
// Checking User Role and Payment Method
if(customeRole!=1155 && scriptContext.type != scriptContext.UserEventType.CREATE &&(paymentMethod!==3||paymentMethod!==4 ||paymentMethod!==5 ||paymentMethod!==6) ) return false;
var transctionId = currentRec.id;
log.debug("id", transctionId)
var customer = currentRec.getValue({
fieldId: 'customer'
})
var customerSearch = search.lookupFields({
type: 'customer',
id: customer,
columns: ['email']
})
log.debug('customer', customer)
log.debug('email', customerSearch.email)
// Creation Payment Receipt
var paymentBill=render.transaction({
entityId: transctionId,
printMode: render.PrintMode.PDF
})
// Email to the Customer
if (customerSearch.email) {
email.send({
author: -5,
recipients: customerSearch.email,
subject: 'Payment',
body: 'Payment Receipt',
attachments: [paymentBill]
})
}
}catch (e){
log.debug('error',e)
}
}
return {afterSubmit}
});