Requirement
- The form must contain a data entry form for the company employees to track down the blood donor details. They should be able to store Name (First Name, Last Name), Gender, Phone Number, Blood Group, Last Donation Date.
- Another form should be available to search for the eligible blood donors, based on the filters (blood group) and last donation date (should be three months before). Name should also be available as an extra filter (this will not be used often, we would love to add that feature to narrow down the results using the name filter).
Create a custom form with the help of Suitelet, Client Script.
Design a custom record to track and store the details.
Store the values in a custom record.
Render the data in the form using the custom records.
A custom form used for the data entry of donor details into NetSuite database.
Solution
First create a custom record in Netsuite to store the blood donor’s details entered through the suitelet form.


Suitelet Script to create the blood donor’s form and storing data to the custom record
/**
* @NApiVersion 2.1
* @NScriptType Suitelet
*/
define(['N/ui/serverWidget','N/record'], function (sw,record)
{
const onRequest = (context) =>
{
try
{
var getForm = sw.createForm({ title: 'Blood Donation Form'});
if(context.request.method === 'GET')
{
//client script internal id---------------
getForm.clientScriptFileId = 9181;
//To create a container for the form
getForm.addFieldGroup({ id:'custpage_bloodform',
label:'Primary Details'});
//To create fields in the form
var fname = getForm.addField({ id:'firstname',
type: sw.FieldType.TEXT,
label: 'First Name',
container:'custpage_bloodform'});
fname.isMandatory = true;
var lname = getForm.addField({ id:'lastname',
type: sw.FieldType.TEXT,
label: 'Last Name',
container:'custpage_bloodform'});
lname.isMandatory=true;
var Gender = getForm.addField({id:'genderlabel',
type:sw.FieldType.LABEL,
label:'Gender',
container:'custpage_bloodform'});
Gender.isMandatory = true;
//Radio Button for Gender Selection
var radiomale = getForm.addField({ id:'gender',
type: sw.FieldType.RADIO,
label:'MALE',
source:'male',
container:'custpage_bloodform'});
var radiofemale = getForm.addField({ id:'gender',
type:sw.FieldType.RADIO,
label:'FEMALE',
source:'female',
container:'custpage_bloodform'});
//instead of setMandatory
radiomale.defaultValue = 'male';
var mobile = getForm.addField({ id:'mobile',
type: sw.FieldType.INTEGER,
label: 'Mobile Number',
container:'custpage_bloodform',
});
mobile.isMandatory = true;
getForm.addField({ id:'ddate',
type: sw.FieldType.DATE,
label: 'Last Donation Date',
container:'custpage_bloodform'});
//Dropdown for selecting blood group
var bloodgroup = getForm.addField({ id:'bgroup',
type:sw.FieldType.SELECT,
label:'Blood Group',
source:'customlist_jj_blood_group',
container:'custpage_bloodform'});
bloodgroup.isMandatory = true;
getForm.addSubmitButton('SUBMIT');
}
else if( context.request.method === 'POST' )
{
var fn = context.request.parameters.firstname;
var ln = context.request.parameters.lastname;
var gen = context.request.parameters.gender;
var mob = context.request.parameters.mobile;
var grp = context.request.parameters.bgroup;
var date1 = context.request.parameters.ddate;
// Storing Data to Netsuite db
var customRecord = record.create({ type: 'customrecord_jj_blood_donation_details', isDynamic:true });
customRecord.setValue({ fieldId:'custrecord_jj_firstname',
value:fn });
customRecord.setValue({ fieldId:'custrecord_jj_lastname',
value:ln });
customRecord.setValue({ fieldId:'custrecord_jj_gender',
value:gen });
customRecord.setValue({ fieldId:'custrecord_jj_phone',
value:mob });
customRecord.setValue({ fieldId:'custrecord_jj_blood_group',
value:grp });
if(date1)
{
customRecord.setValue({ fieldId:'custrecord_jj_donation_date',
value:new Date(date1) });
}
else
{
customRecord.setValue({ fieldId:'custrecord_jj_donation_date',
value:'' });
}
var RecordId = customRecord.save({ ignoreMandatoryFields : false, enableSourcing : true });
log.debug( "record id", RecordId );
if( RecordId )
context.response.sendRedirect("SUITELET", "customscript_jj_blood_filter", "customdeploy_jj_blood_filter");
else
context.response.writePage("Sorry!! Your data is not submitted. Please try again...");
}
context.response.writePage(getForm);
}
catch(e)
{
log.debug("Error", e);
}
}
return {onRequest}
});
The above script uses Client Script in it for field validation and attached the Client script ID from the file cabinet and no need of deploying the Client Script.
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/currentRecord'], function(currentRecord)
{
function validateField(scriptContext)
{
var currentRecord = scriptContext.currentRecord;
if (scriptContext.fieldId === 'mobile')
{
var phone = currentRecord.getValue({
fieldId: 'mobile'
});
if (!(/^[0-9]{10}$/.test(phone)))
{
alert("Please enter a valid phone number....");
return false;
}
else
return true;
}
// Always return true at this level, to continue validating other fields
return true;
}
return { validateField: validateField };
});
Another Suitelet Script is created for displaying the custom record from Netsuite and to to search for the eligible blood donors, based on the filters
/**
* @NApiVersion 2.1
* @NScriptType Suitelet
*/
define(['N/ui/serverWidget', 'N/record', 'N/search'], function(serverWidget, record, search)
{
const onRequest = (context) =>
{
try
{
//creating form for displaying records---------
var form = serverWidget.createForm({title: 'Blood Donation Record'});
if (context.request.method === 'GET')
{
//client script internal id---------------
form.clientScriptFileId = 8776;
//Defining field for filtering data---------
var bg_filter = form.addField({
id: 'bgroup',
type: serverWidget.FieldType.SELECT,
label: 'Blood Group',
source: 'customlist_jj_blood_group'
});
var name_filter = form.addField({
id: 'name',
type: serverWidget.FieldType.TEXT,
label: 'Name'
});
//Getting filtering parameter from client script--------
var getFilter = context.request.parameters.filter;
var getName = context.request.parameters.name;
// if getFilter has a value then set defaultValue
if (getFilter)
{
bg_filter.defaultValue = getFilter;
}
// if getName has a value then set defaultValue
if (getName)
{
name_filter.defaultValue = getName;
}
//------creating sublist in the form----------
var sublist = form.addSublist({
id: 'sublist',
type: serverWidget.SublistType.LIST,
label: 'View Details'
});
//-------Adding fields to the sublist
sublist.addField({
id: 'internalid',
type: serverWidget.FieldType.TEXT,
label: 'Internal ID'
});
sublist.addField({
id: 'custrecord_jj_firstname',
type: serverWidget.FieldType.TEXT,
label: 'First Name',
align: serverWidget.LayoutJustification.RIGHT
});
sublist.addField({
id: 'custrecord_jj_lastname',
type: serverWidget.FieldType.TEXT,
label: 'Last Name',
align: serverWidget.LayoutJustification.RIGHT
});
sublist.addField({
id: 'custrecord_jj_gender',
type: serverWidget.FieldType.TEXT,
label: 'Gender',
align: serverWidget.LayoutJustification.RIGHT
});
sublist.addField({
id: 'custrecord_jj_phone',
type: serverWidget.FieldType.PHONE,
label: 'Phone Number',
align: serverWidget.LayoutJustification.RIGHT
});
sublist.addField({
id: 'custrecord_jj_blood_group',
type: serverWidget.FieldType.TEXT,
label: 'Blood Group',
align: serverWidget.LayoutJustification.RIGHT
});
sublist.addField({
id: 'custrecord_jj_donation_date',
type: serverWidget.FieldType.DATE,
label: 'Last Donation Date',
align: serverWidget.LayoutJustification.RIGHT
});
filter=[];
//creating filter if getFilter has a value
if(getFilter)
{
filter.push( ["custrecord_jj_blood_group", "anyof", getFilter]);
filter.push( "AND", ["custrecord_jj_donation_date", "before", "ninetydaysago"]);
}
if(getName)
{
filter.push( ["custrecord_jj_firstname", "is", getName]);
}
//-----------Search Creation---------
var searchobj = search.create({
type: "customrecord_jj_blood_donation_details",
columns:
[
search.createColumn({name: "internalid", label: "ID"}),
search.createColumn({name: "custrecord_jj_firstname", label: "First Name"}),
search.createColumn({name: "custrecord_jj_lastname", label: "Last Name"}),
search.createColumn({name: "custrecord_jj_gender", label: "Gender"}),
search.createColumn({name: "custrecord_jj_phone", label: "Phone"}),
search.createColumn({name: "custrecord_jj_blood_group", label: "Blood Group"}),
search.createColumn({name: "custrecord_jj_donation_date", label: "Last Donation Date"})
],
filters:filter
});
//----------Running search------------
var resultSet = searchobj.run();
var results = resultSet.getRange({start: 0, end: 500});
var arr = [];
var obj;
for (var i in results)
{
obj = {};
obj.internalid = results[i].getValue(resultSet.columns[0]);
obj.custrecord_jj_firstname = results[i].getValue(resultSet.columns[1]);
obj.custrecord_jj_lastname = results[i].getValue(resultSet.columns[2]);
obj.custrecord_jj_gender = results[i].getValue(resultSet.columns[3]);
obj.custrecord_jj_phone = results[i].getValue(resultSet.columns[4]);
obj.custrecord_jj_blood_group = results[i].getText(resultSet.columns[5]);
obj.custrecord_jj_donation_date = results[i].getValue(resultSet.columns[6]);
arr.push(obj);
}
//Rendering the array to sublist
for (var j = 0; j < arr.length; j++)
{
sublist.setSublistValue({
id: 'internalid',
line: j,
value: arr[j].internalid
});
sublist.setSublistValue({
id: 'custrecord_jj_firstname',
line: j,
value: arr[j].custrecord_jj_firstname
});
sublist.setSublistValue({
id: 'custrecord_jj_lastname',
line: j,
value: arr[j].custrecord_jj_lastname
});
sublist.setSublistValue({
id: 'custrecord_jj_gender',
line: j,
value: arr[j].custrecord_jj_gender
});
sublist.setSublistValue({
id: 'custrecord_jj_phone',
line: j,
value: arr[j].custrecord_jj_phone
});
sublist.setSublistValue({
id: 'custrecord_jj_blood_group',
line: j,
value: arr[j].custrecord_jj_blood_group
});
var ldate = arr[j].custrecord_jj_donation_date;
if( !ldate )
{
ldate = null;
}
sublist.setSublistValue({
id: 'custrecord_jj_donation_date',
line: j,
value: ldate
});
}
}
context.response.writePage(form);
}
catch (e)
{
log.debug("Error", e);
}
}
return { onRequest: onRequest};
});
Client script is attached to the above Suitelet for filtering data from the records.
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/currentRecord'], function(currentRecord) {
function fieldChanged(scriptContext) {
if (scriptContext.fieldId === 'bgroup') {
var currentRecord = scriptContext.currentRecord;
//Getting the current URL
var currentUrl = window.location.href;
//gets value from the Suitelet filter field
var getFilter = currentRecord.getValue({
fieldId: "bgroup"
});
//To remove the alert box
window.onbeforeunload = null;
//spliting the currentUrl as two parts
var newURL = currentUrl.split('&deploy=1');
//Replacing the URL
window.location.replace(newURL[0] + '&deploy=1' + '&filter=' + getFilter);
}
if (scriptContext.fieldId === 'name') {
var currentRecord = scriptContext.currentRecord;
//Getting the current URL
var currentUrl = window.location.href;
//gets value from the Suitelet filter field
var getName = currentRecord.getValue({
fieldId: "name"
});
//To remove the alert box
window.onbeforeunload = null;
//spliting the currentUrl as two parts
var newURL = currentUrl.split('&deploy=1');
//Replacing the URL
window.location.replace(newURL[0] + '&deploy=1' + '&name=' + getName);
}
}
return{
fieldChanged: fieldChanged
};
});
Output
Blood Donor Form

Blood Donor Record

After applying filter
