OnRequest Function:
function onRequest(context) {
try {
if (context.request.method === ‘GET’) {
let form = serverWidget.createForm({
title: ‘Activity Page’,
hideNavBar: true
});
let projectData = search.lookupFields({
type: search.Type.JOB,
id: context.request.parameters.recordId,
columns: [‘companyname’]
}).companyname;
let projectId = context.request.parameters.recordId;
// Define body fields and sublist fields
let bodyFields = {
project: { id: ‘custpage_project’, type: serverWidget.FieldType.TEXT, label: ‘Project’, defaultValue: projectData },
projectId: { id: ‘custpage_projectid’, type: serverWidget.FieldType.TEXT, label: ‘Project’, defaultValue: projectId },
billingSchedule: { id: ‘custpage_billing_schedule’, type: serverWidget.FieldType.SELECT, label: ‘Billing Schedule’, defaultValue: ” },
paymentScheduleDescription: { id: ‘custpage_payment_schedule_description’, type: serverWidget.FieldType.MULTISELECT, label: ‘Payment Schedule Description’, defaultValue: ” },
TotalBillingAmount: { id: ‘custpage_total_billing_amount’, type: serverWidget.FieldType.TEXT, label: ‘Total Billing Amount’, defaultValue: ” }
};
let sublistFields = {
select: { id: ‘custpage_select’, type: serverWidget.FieldType.CHECKBOX, label: ‘Select’ },
paymentSchedule: { id: ‘custpage_payment_schedule’, type: serverWidget.FieldType.TEXT, label: ‘Payment Schedule’, defaultValue: ” },
paymentScheduleId: { id: ‘custpage_paymentschedule_id’, type: serverWidget.FieldType.TEXT, label: ‘Payment Schedule Id’, defaultValue: ” },
description: { id: ‘custpage_description’, type: serverWidget.FieldType.TEXT, label: ‘Description’, defaultValue: ” },
Details: { id: ‘custpage_details’, type: serverWidget.FieldType.TEXT, label: ‘Details’, defaultValue: ” },
paymentScheduleHidden: { id: ‘custpage_payment_scheduleid’, type: serverWidget.FieldType.TEXT, label: ‘Payment Schedule’, defaultValue: ” },
billingScheduleSublist: { id: ‘custpage_billing_schedule_sublist’, type: serverWidget.FieldType.TEXT, label: ‘Billing Schedule’, defaultValue: ” },
billingScheduleSublistHidden: { id: ‘custpage_billing_schedule_sublistid’, type: serverWidget.FieldType.TEXT, label: ‘Billing Schedule’, defaultValue: ” },
serviceItem: { id: ‘custpage_service_item’, type: serverWidget.FieldType.TEXT, label: ‘Service Item’, defaultValue: ” },
serviceItemid: { id: ‘custpage_service_itemid’, type: serverWidget.FieldType.TEXT, label: ‘Service Item’, defaultValue: ” },
projectTask: { id: ‘custpage_project_task’, type: serverWidget.FieldType.TEXT, label: ‘Project Task’, defaultValue: ” },
projectTaskid: { id: ‘custpage_project_taskid’, type: serverWidget.FieldType.TEXT, label: ‘Project Task’, defaultValue: ” },
paymentAmount: { id: ‘custpage_payment_amount’, type: serverWidget.FieldType.TEXT, label: ‘Payment Schedule Amount’, defaultValue: ” },
paymentScheduleAmount: { id: ‘custpage_payment_schedule_amount’, type: serverWidget.FieldType.TEXT, label: ‘Pending Amount’, defaultValue: ” },
billingPercentage: { id: ‘custpage_billing_percentage’, type: serverWidget.FieldType.TEXT, label: ‘Billing Percentage’, defaultValue: ” },
billingAmount: { id: ‘custpage_billing_amount’, type: serverWidget.FieldType.TEXT, label: ‘Billing Amount’, defaultValue: ” },
indianTaxHSNCode: { id: ‘custpage_indian_tax_hsn’, type: serverWidget.FieldType.TEXT, label: ‘INDIAN TAX HSN OR SAC CODE’, defaultValue: ” },
indianTaxNatureValue: { id: ‘custpage_indian_tax_nature’, type: serverWidget.FieldType.TEXT, label: ‘Indian Tax Nature’, defaultValue: ” }
};
// Add body fields to the form
for (let key in bodyFields) {
let field = bodyFields[key];
let bodyField = form.addField({ id: field.id, type: field.type, label: field.label });
bodyField.defaultValue = field.defaultValue;
if (key === ‘projectId’) {
bodyField.updateDisplayType({ displayType: serverWidget.FieldDisplayType.HIDDEN });
}
if (key === ‘project’ || key === ‘TotalBillingAmount’) {
bodyField.updateDisplayType({ displayType: serverWidget.FieldDisplayType.INLINE });
}
if (key === ‘paymentScheduleDescription’) {
bodyField.updateDisplayType({ displayType: serverWidget.FieldDisplayType.NORMAL });
}
}
// Add sublist to the form
let detailsSublist = form.addSublist({
id: ‘custpage_details_sublist’,
type: serverWidget.SublistType.LIST,
label: ‘Project Payment Details’
});
for (let key in sublistFields) {
let field = sublistFields[key];
let sublistField = detailsSublist.addField({ id: field.id, type: field.type, label: field.label });
sublistField.defaultValue = field.defaultValue;
// Set hidden fields to hidden display type
if (key === ‘paymentScheduleHidden’ || key === ‘billingScheduleSublistHidden’ || key === ‘serviceItemid’ || key === ‘projectTaskid’) {
sublistField.updateDisplayType({ displayType: serverWidget.FieldDisplayType.HIDDEN });
}
if (key === ‘billingPercentage’ || key === ‘billingAmount’) {
sublistField.updateDisplayType({ displayType: serverWidget.FieldDisplayType.ENTRY });
}
if (key === ‘paymentScheduleAmount’) {
sublistField.updateDisplayType({ displayType: serverWidget.FieldDisplayType.DISABLED });
}
}
form.clientScriptModulePath = ‘./jj_cs_activity_creation_rspdc_577.js’;
let billingScheduleField = form.getField({ id: ‘custpage_billing_schedule’ });
let descriptionField = form.getField({ id: ‘custpage_payment_schedule_description’ });
//toGetBillingSchedule(billingScheduleField, projectId);
toGetpaymentDescription(descriptionField, projectId);
let projecttasks = toGetProjectTaskDetails(context.request.parameters.recordId);
if (context.request.parameters.billingSchedule) {
billingScheduleField.defaultValue = context.request.parameters.billingSchedule;
}
if (context.request.parameters.descriptionfield) {
descriptionField.defaultValue = context.request.parameters.descriptionfield.split(‘,’);
}
// Fetch and populate payment schedules in the sublist by default
let paymentSchedules = toGetPaymentSchedule(billingScheduleField.defaultValue, descriptionField.defaultValue, projectId);
for (let i = 0; i < paymentSchedules.length; i++) {
let paymentSchedule = paymentSchedules[i];
// Ensure all values are non-null and convert to string
let paymentScheduleName = paymentSchedule.name || ‘-‘;
let paymentScheduleId = paymentSchedule.internalId || ‘-‘;
let description = paymentSchedule.description || ‘-‘;
let details = paymentSchedule.details || ‘-‘;
let paymentId = paymentSchedule.paymentScheduleId || ‘-‘
let indianTaxHSNCode = paymentSchedule.indianTaxHSNCode || ‘-‘;
let indianTaxNatureValue = paymentSchedule.indianTaxNatureValue || ‘-‘;
let billingScheduleSublist = paymentSchedule.billingScheduleText || ‘-‘;
let billingScheduleSublistId = paymentSchedule.billingSchedule || ‘-‘;
let serviceItem = paymentSchedule.serviceItemText || ‘-‘;
let serviceItemId = paymentSchedule.serviceItem || ‘NULL’;
let projectTask = paymentSchedule.ProjectTaskText || ‘-‘;
let projectTaskId = paymentSchedule.projectTask || ‘-‘;
let paymentScheduleAmount = paymentSchedule.paymentScheduleAmount ? paymentSchedule.paymentScheduleAmount.toString() : ‘0’;
let billingPercentage = paymentSchedule.billingPercentage ? paymentSchedule.billingPercentage.toString() : ‘0’;
let billingPercentageValue = parseFloat(paymentSchedule.billingPercentage);
let paymentScheduleAmountValue = parseFloat(paymentSchedule.paymentScheduleAmount);
// if (!isNaN(billingPercentageValue) && !isNaN(paymentScheduleAmountValue)) {
// paymentSchedule.billingAmount = (billingPercentageValue / 100) * paymentScheduleAmountValue;
// } else {
// paymentSchedule.billingAmount = paymentScheduleAmount;
// }
let billingAmount = paymentSchedule.billingAmount ? paymentSchedule.billingAmount.toString() : ‘0’;
detailsSublist.setSublistValue({ id: ‘custpage_payment_schedule’, line: i, value: paymentScheduleName });
detailsSublist.setSublistValue({ id: ‘custpage_payment_scheduleid’, line: i, value: paymentScheduleId });
detailsSublist.setSublistValue({ id: ‘custpage_paymentschedule_id’, line: i, value: paymentId });
detailsSublist.setSublistValue({ id: ‘custpage_description’, line: i, value: description });
detailsSublist.setSublistValue({ id: ‘custpage_details’, line: i, value: details })
detailsSublist.setSublistValue({ id: ‘custpage_billing_schedule_sublist’, line: i, value: billingScheduleSublist });
detailsSublist.setSublistValue({ id: ‘custpage_billing_schedule_sublistid’, line: i, value: billingScheduleSublistId });
detailsSublist.setSublistValue({ id: ‘custpage_service_item’, line: i, value: serviceItem });
detailsSublist.setSublistValue({ id: ‘custpage_service_itemid’, line: i, value: serviceItemId });
detailsSublist.setSublistValue({ id: ‘custpage_project_task’, line: i, value: projectTask });
detailsSublist.setSublistValue({ id: ‘custpage_project_taskid’, line: i, value: projectTaskId });
//Payment schedule amount in activity page
detailsSublist.setSublistValue({ id: ‘custpage_payment_amount’, line: i, value: billingAmount });
detailsSublist.setSublistValue({ id: ‘custpage_payment_schedule_amount’, line: i, value: paymentScheduleAmount });
detailsSublist.setSublistValue({ id: ‘custpage_billing_percentage’, line: i, value: billingPercentage });
//billingAMount in activity page
//detailsSublist.setSublistValue({ id: ‘custpage_billing_amount’, line: i, value: billingAmount });
detailsSublist.setSublistValue({ id: ‘custpage_indian_tax_hsn’, line: i, value: indianTaxHSNCode });
detailsSublist.setSublistValue({ id: ‘custpage_indian_tax_nature’, line: i, value: indianTaxNatureValue });
}
// Add submit button
form.addSubmitButton({ label: ‘Submit’ });
detailsSublist.addButton({
id: ‘custpage_mark_all’,
label: ‘Mark All’,
functionName: ‘markAll’
});
detailsSublist.addButton({
id: ‘custpage_unmark_all’,
label: ‘Unmark All’,
functionName: ‘unmarkAll’
});
// Display the form
context.response.writePage(form);
} else {
let sublistLineCount = context.request.getLineCount({ group: ‘custpage_details_sublist’ });
let itemArray = [];
for (let i = 0; i < sublistLineCount; i++) {
let selectLines = context.request.getSublistValue({
group: ‘custpage_details_sublist’,
name: ‘custpage_select’,
line: i
});
let itemId = context.request.getSublistValue({
group: ‘custpage_details_sublist’,
name: ‘custpage_service_itemid’,
line: i
});
if (selectLines == “T” && itemId !== “NULL”) {
let itemObj = {};
itemObj.itemId = context.request.getSublistValue({
group: ‘custpage_details_sublist’,
name: ‘custpage_service_itemid’,
line: i
});
itemObj.indianTaxHSNCode = context.request.getSublistValue({
group: ‘custpage_details_sublist’,
name: ‘custpage_indian_tax_hsn’,
line: i
});
itemObj.indianTaxNatureValue = context.request.getSublistValue({
group: ‘custpage_details_sublist’,
name: ‘custpage_indian_tax_nature’,
line: i
});
itemObj.paymentSchedule = context.request.getSublistValue({
group: ‘custpage_details_sublist’,
name: ‘custpage_payment_scheduleid’,
line: i
});
itemObj.description = replaceNewLineAndTabWithEmpty(context.request.getSublistValue({
group: ‘custpage_details_sublist’,
name: ‘custpage_description’,
line: i
}));
itemObj.billSchedule = context.request.getSublistValue({
group: ‘custpage_details_sublist’,
name: ‘custpage_billing_schedule_sublistid’,
line: i
});
itemObj.paymentScheduleAmount = context.request.getSublistValue({
group: ‘custpage_details_sublist’,
name: ‘custpage_payment_schedule_amount’,
line: i
});
itemObj.billPercentage = context.request.getSublistValue({
group: ‘custpage_details_sublist’,
name: ‘custpage_billing_percentage’,
line: i
});
itemObj.billingAmount = context.request.getSublistValue({
group: ‘custpage_details_sublist’,
name: ‘custpage_billing_amount’,
line: i
});
itemObj.projectTask = context.request.getSublistValue({
group: ‘custpage_details_sublist’,
name: ‘custpage_project_taskid’,
line: i
});
itemObj.details = context.request.getSublistValue({
group: ‘custpage_details_sublist’,
name: ‘custpage_details’,
line: i
});
itemArray.push(itemObj);
}
}
let csUrlPath = ‘./SuiteScripts/Jobin and Jismi/RSPDC-716 Proforma Invoice Customization/jj_cs_activity_creation_rspdc_577.js’;
let sanitizedItemArray = JSON.stringify(itemArray).replace(/’/g, “‘”);
let str = “<script>window.opener.require( [ ‘” + csUrlPath + “‘ ], function ( cs ) { cs.setSlLine( ‘” + sanitizedItemArray + “‘ ); } ); window.opener.focus(); window.close() </script>”;
log.debug(“String length”, str.length)
context.response.write(str);
}
} catch (e) {
log.error({
title: ‘Error in Suitelet onRequest’,
details: e
});
context.response.write({
output: ‘An error occurred while processing the request. Please contact your administrator.’
});
}
}
Code snippet to handle comma and special character:
let csUrlPath = ‘./SuiteScripts/Jobin and Jismi/RSPDC-716 Proforma Invoice Customization/jj_cs_activity_creation_rspdc_577.js’;
let sanitizedItemArray = JSON.stringify(itemArray).replace(/’/g, “‘”);
let str = “<script>window.opener.require( [ ‘” + csUrlPath + “‘ ], function ( cs ) { cs.setSlLine( ‘” + sanitizedItemArray + “‘ ); } ); window.opener.focus(); window.close() </script>”;
log.debug(“String length”, str.length)
context.response.write(str);