/** * @NApiVersion 2.1 * @NScriptType UserEventScript * @NModuleScope SameAccount */ /** * Purpose :Update Service Window Custom Record and related Service window custom records based on the customer * Date : 29 April 2020 * Author : DILIP P * * Version Date Author Requested By Remarks * * * * * * * * */ define(["N/record", "N/search"], (RECORD_MOD, SEARCH_MOD) => { const SW_CUSREC_ID = 'customrecord_service_window_cus'; const { CUSTOMER } = RECORD_MOD.Type; /** * Function definition to be triggered after record is commited to the server (database) * * @param {Object} scriptContext * @param {Record} scriptContext.newRecord - New record * @param {Record} scriptContext.oldRecord - Old record * @param {string} scriptContext.type - Trigger type * @Since 2015.2 */ var serviceWindow_AS = (scriptContext) => { try { var newRecord = scriptContext.newRecord; log.debug("Trigger condition", `Record Type : ${newRecord.type} , Context Type ${scriptContext.type}`); //Trigger only if its Service window record and only for EDIT if (newRecord.type === SW_CUSREC_ID) { if (scriptContext.type == 'edit') { var startTime = new Date().getTime(); log.debug("Service Window Script START", `*****Start*****`); //Get Service window record ID and Customer Value from service window record var sw_id = newRecord.id; var customerId = newRecord.getValue('custrecord_service_window_cus'); log.debug("sw_id & customerId", `SW_REC_ID: ${sw_id} Customer: ${customerId}`); //if Customer Value is present search the customer Record and get all related sublist Service window Records for this customer if (customerId) { var SW_searchObJ = get_SW_RecordSearchObj(customerId, SW_CUSREC_ID) //Update Service Window Custom Records field "SEND UPDATE" to TRUE; SW_searchObJ.run().each(function(result) { // .run().each has a limit of 4,000 results log.debug(`SW_ID from result`, `SW_ID from RESULT: ${result.id}`); RECORD_MOD.submitFields({ type: SW_CUSREC_ID, id: result.id, values: { custrecord_service_window_send_update: true }, options: { enablesourcing: true, ignoreMandatoryFields: true } }) return true; }); } var endTime = new Date().getTime(); log.debug("Service Window Script END", `***************END*** TIME*****${(endTime - startTime )/1000} seconds**********`); } } } catch (error) { log.error(`Error Occurred in serviceWindow_AS`, error.toString()) } } var serviceWindow_BS = (scriptContext) => { try { var newRecord = scriptContext.newRecord; log.debug("Trigger condition BS", `Record Type : ${newRecord.type} , Context Type ${scriptContext.type}`); var oldRecord = scriptContext.oldRecord; if (newRecord.type === CUSTOMER) { if (scriptContext.type == 'edit') { var startTime = new Date().getTime(); log.debug("Service Window Script START for Customer BS", `*****Start*****`); var customerId = newRecord.id; var newAddressLineCount = newRecord.getLineCount('addressbook'); var oldAddressLineCount = oldRecord.getLineCount('addressbook'); log.debug("addressLineCount", `addressbook newAddressLineCount: ${newAddressLineCount} isDYnamic : ${newRecord.isDynamic}, oldAddressLineCount : ${oldAddressLineCount}`); var newAddressObj = {}, oldAddressObj = {}; //Get NewRecord address Sublist Details for (var newInit = 0; newInit < newAddressLineCount; newInit++) { var objSubRecord = newRecord.getSublistSubrecord({ sublistId: 'addressbook', fieldId: 'addressbookaddress', line: newInit }); var addrtext = objSubRecord.getValue('addrtext'); var addressID = objSubRecord.getValue('id'); newAddressObj[addressID] = addrtext log.debug("NEW_objSubrecord addrtext", ` NEW_objSubrecord addrtext: ${addrtext} addressID: ${addressID} `) } //Get OldRecord address Sublist Details for (var oldInit = 0; oldInit < oldAddressLineCount; oldInit++) { var objSubRecord = oldRecord.getSublistSubrecord({ sublistId: 'addressbook', fieldId: 'addressbookaddress', line: oldInit }); var addrtext = objSubRecord.getValue('addrtext'); var addressID = objSubRecord.getValue('id'); oldAddressObj[addressID] = addrtext; log.debug("OLD_objSubrecord addrtext", ` OLD_objSubrecord addrtext: ${addrtext} addressID: ${addressID}`) } var flag = JSON.stringify(oldAddressObj) === JSON.stringify(newAddressObj) log.audit("JSON.stringify(oldAddressObj) === JSON.stringify(newAddressObj)", flag) //Compare old and new address values, if there is a change update Service window custom record. if (!flag) { var SW_searchObJ = get_SW_RecordSearchObj(customerId, SW_CUSREC_ID) //Update Service Window Custom Records field "SEND UPDATE" to TRUE; SW_searchObJ.run().each(function(result) { // .run().each has a limit of 4,000 results log.debug(`SW_ID from result`, `SW_ID from RESULT: ${result.id}`); RECORD_MOD.submitFields({ type: SW_CUSREC_ID, id: result.id, values: { custrecord_service_window_send_update: true }, options: { enablesourcing: true, ignoreMandatoryFields: true } }) return true; }); } var endTime = new Date().getTime(); log.debug("Service Window Script customer BS END", `***************END TIME*****${(endTime - startTime )/1000} seconds**********`); } } } catch (error) { log.error(`Error Occurred in serviceWindow_BS`, error.toString()) } } /** * [get_SW_RecordSearchObj description] * @param {[type]} customerId [customer Internal ID] * @param {[type]} SW_CUSREC_ID ["Service window custom record "Record ID"] * @return {[type]} [returns search Script Object] */ var get_SW_RecordSearchObj = (customerId, SW_CUSREC_ID) => { var customrecord_service_window_cusSearchObj = SEARCH_MOD.create({ type: SW_CUSREC_ID, filters: [ ["custrecord_service_window_cus", "anyof", customerId], "AND", ["isinactive", "is", "F"] ] }); return customrecord_service_window_cusSearchObj; } return { afterSubmit: serviceWindow_AS, beforeSubmit: serviceWindow_BS }; });