/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
* @NModuleScope SameAccount
*/
define(['N/search', 'N/record', 'N/email', 'N/log'],
function (search, record, email, log) {
/**
* Definition of the Scheduled script trigger point.
*
* @param {Object}
* scriptContext
* @param {string}
* scriptContext.type - The context in which the script is
* executed. It is one of the values from the
* scriptContext.InvocationType enum.
* @Since 2015.2
*/
function execute(scriptContext) {
/* Create a SAVED SEARCH using Record Type "Amortization - Payment
Schedule" */
/* Value Set: Posting Date = System Date */
var mySearch = search.create({
"type": "customrecordpfg_advances_pymt_sched",
"filters": [{
"name": "custrecordpfg_adv_pymt_post_date",
"operator": "within",
"values": ["today"],
"isor": false,
"isnot": false,
"leftparens": 0,
"rightparens": 0
}
],
/* Item Fields Fetched */
"columns": [{
"name": "custrecordpfg_adv_pymt_advance_id",
"label": "Advance Id",
"type": "select",
"sortdir": "NONE"
}, {
"name": "custrecordpfg_adv_pymt_post_date",
"label": "Posting Date",
"type": "date",
"sortdir": "ASC"
}, {
"name": "custrecordpfg_adv_pymt_pymt_date",
"label": "Payment Date",
"type": "date",
"sortdir": "NONE"
}, {
"name":
"custrecordpfg_adv_pymt_prncpl_adj_amt",
"label": "Principal Adjustment Amount",
"type": "currency",
"sortdir": "NONE"
}, {
"name": "custrecordpfg_adv_pymt_bgn_bal",
"label": "Beginning Balance",
"type": "currency",
"sortdir": "NONE"
}, {
"name": "custrecordpfg_adv_pymt_schd_pymt",
"label": "Scheduled Payment",
"type": "currency",
"sortdir": "NONE"
}, {
"name": "custrecordpfg_adv_pymt_principal",
"label": "Principal",
"type": "currency",
"sortdir": "NONE"
}, {
"name": "custrecordpfg_adv_pymt_interest",
"label": "Interest",
"type": "currency",
"sortdir": "NONE"
}, {
"name": "custrecordpfg_adv_pymt_end_bal",
"label": "Ending Balance",
"type": "currency",
"sortdir": "NONE"
}, {
"name": "custrecordpfg_adv_pymt_status",
"label": "Status",
"type": "text",
"sortdir": "NONE"
}, {
"name": "custrecordpfg_adv_pymt_je_id",
"label": "Journal Entry",
"type": "select",
"sortdir": "NONE"
}
]
});
/* Define and Assign Variables */
mySearch.run().each(function (result) {
var adv_pymt_advance_id = result.getValue({
name: 'custrecordpfg_adv_pymt_advance_id'
});
var adv_pymt_post_date = result.getValue({
name: 'custrecordpfg_adv_pymt_post_date'
});
var adv_pymt_pymt_date = result.getValue({
name: 'custrecordpfg_adv_pymt_pymt_date'
});
var adv_pymt_prncpl_adj_amt = result.getValue({
name: 'custrecordpfg_adv_pymt_prncpl_adj_amt'
});
var adv_pymt_bgn_bal = result.getValue({
name: 'custrecordpfg_adv_pymt_bgn_bal'
});
var adv_pymt_schd_pymt = result.getValue({
name: 'custrecordpfg_adv_pymt_schd_pymt'
});
var adv_pymt_principal = result.getValue({
name: 'custrecordpfg_adv_pymt_principal'
});
var adv_pymt_interest = result.getValue({
name: 'custrecordpfg_adv_pymt_interest'
});
var adv_pymt_end_bal = result.getValue({
name: 'custrecordpfg_adv_pymt_end_bal'
});
/* Header Fields Fetched */
var objRecord = record.load({
type: 'customrecordpfg_amortization',
id: adv_pymt_advance_id,
isDynamic: true,
});
/* Define and Assign Variables */
var adv_franchise_name = objRecord.getValue({
fieldId: 'custrecordpfg_adv_franchise_name'
});
var recordid = objRecord.getValue({
fieldId: 'recordid'
});
var adv_loan_amount = objRecord.getValue({
fieldId: 'custrecordpfg_adv_loan_amount'
});
var adv_memo = objRecord.getValue({
fieldId: 'custrecordpfg_adv_memo'
});
var partner_currency = objRecord.getValue({
fieldId: 'custrecordpfg_partner_currency'
});
/* Create: Invoice */
var objRecord = record.create({
type: 'invoice',
});
/* Assign Variables */
objRecord.setText({
fieldId: 'customform',
value: 'Amortization Invoice Form',
ignoreFieldChange: true
});
objRecord.setValue({
fieldId: 'entity',
value: adv_franchise_name,
ignoreFieldChange: true
});
objRecord.setValue({
fieldId: 'custbody_amortization_partner_currency',
value: partner_currency,
ignoreFieldChange: true
});
objRecord.setValue({
fieldId: 'custbody_amortization_loan_amount',
value: adv_loan_amount,
ignoreFieldChange: true
});
objRecord.setValue({
fieldId: 'memo',
value: adv_memo,
ignoreFieldChange: true
});
objRecord.setValue({
fieldId: 'custbody_amortization_advance_id',
value: adv_pymt_advance_id,
ignoreFieldChange: true
});
objRecord.setValue({
fieldId: 'trandate',
value: adv_pymt_post_date,
ignoreFieldChange: true
});
objRecord.setValue({
fieldId: 'duedate',
value: adv_pymt_pymt_date,
ignoreFieldChange: true
});
objRecord.setValue({
fieldId: 'custbody_amortization_prn_adj_amt',
value: adv_pymt_prncpl_adj_amt,
ignoreFieldChange: true
});
objRecord.setValue({
fieldId: 'custbody_amortization_bgn_balance',
value: adv_pymt_bgn_bal,
ignoreFieldChange: true
});
objRecord.setValue({
fieldId: 'custbody_amortization_ending_balance',
value: adv_pymt_end_bal,
ignoreFieldChange: true
});
objRecord.setValue({
fieldId: 'custbody_amortization_schd_payment',
value: adv_pymt_schd_pymt,
ignoreFieldChange: true
});
objRecord.setValue({
fieldId: 'custbody_amortization_principal_amt',
value: adv_pymt_principal,
ignoreFieldChange: true
});
objRecord.setValue({
fieldId: 'custbody_amortization_interest_amt',
value: adv_pymt_interest,
ignoreFieldChange: true
});
/* Save: Invoice */
var recordId = objRecord.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
/* Define: Log */
log.debug({
title: 'id',
details: recordId
});
});
};
return {
execute: execute
};
});