Check An email is existing in netsuite at the time of registration

We have to check whether an email is existing in netsuite , when a user register in website at the time of registration.For that a suitelet is using.The return value from suitelet is using to check whether the email is existing or not.

suitelet:

define([
 	'SuiteScripts/Enlighten Smiles/Library.FHL.2.0.js'
,	'SuiteScripts/Enlighten Smiles/Library.ES.2.0.js'
,	'N'
],
function(libraryFHL, libraryES, N)
{
	'use strict';
	
	function onRequest(context)
	{
		try
		{
			log.debug("start");
			switch(context.request.method)
			{
				case 'POST':
					handlePOSTRequest(context);
				break;
				default:
					handleBadRequest(context);
				break;
			};
		}
		catch (e)
		{
			libraryFHL.errorHandler('script', e);
		};
	};
	
	function handlePOSTRequest(context)
	{
		var body 	= null;
		var address	= null;
		var verdict	= {};
		try
		{
			body 	= JSON.parse(context.request.body);
			address = body.email;
			verdict = checkEmail(address, context);
			context.response.addHeader({
			    name 	: 'Content-Type'
			,   value 	: 'application/json'
			});
			context.response.write({output : JSON.stringify(verdict)});
		}
		catch (e)
		{
			libraryFHL.errorHandler('handlePOSTRequest', e);
			handleBadRequest(context);
		};
	};
	
	function handleBadRequest(context)
	{
		var BAD_REQUEST_HTML = '<html><body><h1>Error: 400 - Bad Request</h1><h2>Request method {name} is not supported!</h2></body></html>';
		try
		{
			context.response.addHeader({
			    name 	: 'Content-Type'
			,   value 	: 'text/html'
			});
			context.response.write(BAD_REQUEST_HTML.replace('{name}', context.request.method || 'undefined'));
		}
		catch (e)
		{
			libraryFHL.errorHandler('handleBadRequest', e);
		};
	};
	
	function checkEmail(address, context)
	{
		var RECORD_TYPES 	= ['customer', 'employee', 'vendor']; //1.1.2
		var matchingRecords = 0;
		var retVal 			= {
			status 	: false
		,	message : 'Invalid email address'
		};
		try
		{
			retVal.status = 'Invalid Email Address';
			if(libraryES.validateEmail(address))
			{
				
	RECORD_TYPES.forEach(function(TYPE, index){
					matchingRecords += libraryES.genericSearchCount(TYPE, 'email', address);
				});
				retVal.message 	= (matchingRecords > 0) ? 'An account with this email already exists' : 'Email address is clear';
				retVal.status 	= (matchingRecords > 0) ? false : true;
			};
		}
		catch(e)
		{
			libraryFHL.errorHandler('checkEmail', e);
			handleBadRequest(context);
		};
		return retVal;
	};
	return{
		onRequest : onRequest
	};
});

suitescript model file:

function validateEmail(emailAddress)
				{
					var suiteletURL = null;
					var suiteletResponse = null;
					var dataResponse = null;
					var postdata = null;
					var HEADERS = {
						"content-type": "application/json"
					,   "cache-control": "no-cache"
					};
					try
					{
	suiteletURL = nlapiResolveURL('SUITELET', 'customscript_duplicateemaildetect', 'customdeploy_duplicateemaildetect', 'external');
		postdata = JSON.stringify({email : emailAddress});
	suiteletResponse = nlapiRequestURL(suiteletURL, postdata, HEADERS, 'POST');//10 gov units
		dataResponse = JSON.parse(suiteletResponse.getBody());
						
					}
					catch(e)
					{
		nlapiLogExecution('ERROR', 'Account.Model: validateEmail', e);
					}
					return dataResponse;
				};

Leave a comment

Your email address will not be published. Required fields are marked *