Jira Code: PAL-5
To check the duplicate customer while submitting the online form. We call a suitelet script by a call back function.
<nlform>
<!DOCTYPE html>
<html>
<head>
<title>
Contact Us
</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.33.1/sweetalert2.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.33.1/sweetalert2.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>
<body>
<div style="display: none;">
<nlsubsidiary>
<nlfirstname>
<nllastname>
<nlphone>
<nlemail>
<nlcomments>
<nlcustomform>
</div>
<div class="container">
<form class="needs-validation" novalidate>
<div class="form-row mt-5">
<div class="col-sm-12 col-12 col-md-12 col-lg-12 mb-2">
<label class="font-weight-bold text-dark">Your Name<sup class="font-weight-bold text-danger">*</sup></label>
</div>
<div class="col-sm-6 col-md-6 col-lg-6 col-6 mb-3">
<input type="text" class="form-control" id="firstname1" placeholder="First name" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-sm-6 col-md-6 col-lg-6 col-6 mb-3">
<input type="text" class="form-control" id="lastname1" placeholder="Last name" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-sm-12 col-12 col-md-12 col-lg-12 mb-3 form-group">
<label class="font-weight-bold text-dark">Email<sup class="font-weight-bold text-danger">*</sup></label>
<input type="email" class="form-control" id="email1" aria-describedby="emailHelp" placeholder="Enter email" pattern="^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-sm-12 col-12 col-md-12 col-lg-12 mb-3 form-group">
<label class="font-weight-bold text-dark">Phone<sup class="font-weight-bold text-danger">*</sup></label>
<input type="tel" class="form-control" id="phone1" aria-describedby="telephoneHelp" placeholder="###-###-####" pattern="^\+{0,2}([\-\. ])?(\(?\d{0,3}\))?([\-\. ])?\(?\d{0,3}\)?([\-\. ])?\d{3}([\-\. ])?\d{4}" required>
<div class="valid-feedback">
Looks good!
</div>
<div class="invalid-feedback">
Invalid Number! (ex:000-000-0000)
</div>
</div>
<div class="col-sm-12 col-12 col-md-12 col-lg-12 mb-2 mt-3">
<div class="form-group">
<label class="font-weight-bold text-dark" for="exampleFormControlTextarea1">Tell us a little about your project.</label>
<textarea class="form-control" id="comments1" rows="3"></textarea>
</div>
</div>
</div>
<div class="mb-5 col-sm-12 col-12 col-md-12 col-lg-12 text-center">
<button class="btn btn btn-dark btn-lg" id="sumbitBtn" type="submit" style="display: none;">Send</button>
<button class="btn btn btn-dark btn-lg" id="duplicateBtn" type="button">Send</button>
</div>
</form>
<script>
// Example starter JavaScript for disabling form submissions if there are invalid fields
(function() {
'use strict';
window.addEventListener('load', function() {
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.getElementsByClassName('needs-validation');
// Loop over them and prevent submission
var validation = Array.prototype.filter.call(forms, function(form) {
form.addEventListener('submit', function(event) {
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
}, false);
})();
$(function() {
$('#duplicateBtn').click(function() {
var allfilled = true;
var mandatoryFields = ['firstname1', 'lastname1', 'email1', 'phone1'];
mandatoryFields.forEach(function(keyID) {
if ($('#' + keyID).val().trim() == '') {
var value2=$('#' + keyID).val().trim();
$('#' + keyID).val(value2);
allfilled = false;
} else {
var fieldid = keyID.replace(/1/g, '');
var value = $('#' + keyID).val().trim();
$('#' + fieldid).val(value);
}
});
var emailid=$('#email1').val().trim();
var comment=$('#comments1').val().trim();
if(comment!='')
$('#comments').val(comment);
var u = "https://forms.na3.netsuite.com/app/site/hosting/scriptlet.nl?script=291&deploy=1&compid=5102952&h=92a561884d3fe2221b9a&email=" + emailid+"&comment="+comment;
cb = 'myCallBackSaveContactForm';
getJSONP(u + '&callback=' + cb)
// if (allfilled == false)
// alert('Please Fill Mandatory Fields');
});
});
function myCallBackSaveContactForm(data) {
console.log("data",data)
var myObj = JSON.parse(data);
var emailid = $('#email').val().trim();
if (myObj == 'ALREADY_EXISTS') {
Swal.fire({
type: 'success',
title: 'Request is being submitted',
text: 'Thank you for your inquiry. We will be in contact soon',
allowOutsideClick: false,
timer: 2000
}).then(function() {
window.location.reload();
});
} else {
Swal.fire({
type: 'success',
title: 'Request is being submitted',
text: 'Thank you for your inquiry. We will be in contact soon',
allowOutsideClick: false,
timer: 2000
}).then(function() {
$('#sumbitBtn').click();
});
}
}
function getJSONP(url) {
var script = document.createElement('script');
var head = document.getElementsByTagName('head')[0] || document.documentElement;
script.src = url
head.appendChild(script);
}
</script>
</div>
</body>
</html>
</nlform>
Suitelet
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope SameAccount
*/
/**
* Script Description
* This suitelet to search customer.
*/
/*******************************************************************************
*
*
* NetSuite Name :PAL-5 JJ SL Duplicate Check
* Script ID: customscript_jj_customer_search
*
* *****************************************************************************
*
*
* $Author: Jobin & Jismi IT Services LLP $
*
* DESCRIPTION
* Suitelet to search customer to find the duplicate.
*
* Date Created :11-05-2019
*
* REVISION HISTORY Update:
*
*
*
******************************************************************************/
define(['N/file', 'N/search', 'N/runtime', 'N/record', 'N/https', 'N/encode', 'N/ui/serverWidget', 'N/email'],
function (file, search, runtime, record, https, encode, serverWidget, email) {
var main = {
onRequest: function (context) {
log.debug("context", context)
var email = context.request.parameters.email;
var comment = context.request.parameters.comment;
var callBackFuncStr = context.request.parameters.callback;
var customerSearchObj = search.create({
type: "customer",
filters: [
["email", "is", email]
],
columns: [
search.createColumn({
name: "entityid",
sort: search.Sort.ASC,
label: "Name"
}),
search.createColumn({
name: "email",
label: "Email"
}),
search.createColumn({
name: "phone",
label: "Phone"
}),
search.createColumn({
name: "internalid",
label: "Internal ID"
})
]
});
var found = customerSearchObj.runPaged().count > 0 ? true : false;
log.debug(found)
if (found) {
var resultsearch = customerSearchObj.run().getRange(0, 1);
var idcustomer = resultsearch[0].getValue({
name: "internalid",
label: "Internal ID"
});
var rec = record.load({
type: record.Type.CUSTOMER,
id: idcustomer,
isDynamic: false
});
rec.setValue({
fieldId: 'comments',
value: comment
});
rec.save();
}
if (found) {
var strJson = callBackFuncStr + '(\'' + JSON.stringify('ALREADY_EXISTS') + '\')';
context.response.write(strJson);
} else {
var strJson = callBackFuncStr + '(\'' + JSON.stringify('ALREADY_NOT_EXISTS') + '\')';
context.response.write(strJson);
}
}
}
for (var key in main) {
if (typeof main[key] === 'function') {
main[key] = trycatch(main[key], key);
}
}
function trycatch(myfunction, key) {
return function () {
try {
return myfunction.apply(this, arguments);
} catch (e) {
log.debug("e in " + key, e);
}
}
};
return main;
});