Suitelet script
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
*/
define(['N/search', 'N/log'], function (search, log) {
function onRequest(context) {
var response = context.response;
log.error('ramya context', context);
var dataParams = context.request.parameters;
var email = dataParams.email || '';
log.error('email', email);
if (email) {
var customerSearchObj = search.create({
type: "customer",
filters:
[
["stage", "anyof", "LEAD", "PROSPECT", "CUSTOMER"],
"AND",
["email", "is", email],
"AND",
["msesubsidiary.internalid", "anyof", "17"]
],
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: "altphone", label: "Office Phone" }),
search.createColumn({ name: "fax", label: "Fax" }),
search.createColumn({ name: "contact", label: "Primary Contact" }),
search.createColumn({ name: "altemail", label: "Alt. Email" })
]
});
var searchResultCount = customerSearchObj.runPaged().count;
log.debug("customerSearchObj result count", searchResultCount);
customerSearchObj.run().each(function (result) {
return true;
});
var message = JSON.stringify({ success: true, message: '', totalCustomers: 0 })
try {
if (parseInt(searchResultCount) >= 1) {
log.error('sucess')
message = JSON.stringify({ success: true, message: 'Duplicate customer with the same email already exists in this subsidiary.', totalCustomers: searchResultCount })
} else {
message = JSON.stringify({ success: true, message: 'No Duplicate found', totalCustomers: searchResultCount })
}
}
catch (error) {
log.error('message showing error', error)
}
} else {
message = JSON.stringify({ success: true, message: 'No email Found', totalCustomers: null })
}
response.write(message)
}
return {
onRequest: onRequest
};
});
js file
// jj.registerext.registerext.js
// Load all your starter dependencies in backend for your extension here
// ----------------
define('jj.registerext.registerext'
, [
'registerext.ServiceController', 'SC.Model',
'Application',
'Account.Model',
'underscore',
'SC.Models.Init',
'Profile.Model',
'LiveOrder.Model',
'Address.Model',
'CreditCard.Model',
'SiteSettings.Model',
'Configuration'
]
, function (
registerextServiceController, SCModel, Application,
AccountModel,
_, ModelsInit,
Profile,
LiveOrder,
Address,
CreditCard,
SiteSettings,
Configuration
) {
'use strict';
_.extend(AccountModel, {
register: function (user_data) {
var customfields = {};
for (var property in user_data) {
if (property.substring(0, 10) == 'custentity') {
customfields[property] = user_data[property];
}
}
try {
if (ModelsInit.customer.isGuest()) {
var guest_data = ModelsInit.customer.getFieldValues();
ModelsInit.customer.setLoginCredentials({
internalid: guest_data.internalid,
email: user_data.email,
password: user_data.password
});
console.error("user_data", user_data)
ModelsInit.session.login({
email: user_data.email,
password: user_data.password
});
if (Object.keys(customfields).length) {
ModelsInit.customer.updateProfile({
internalid: guest_data.internalid,
firstname: user_data.firstname,
lastname: user_data.lastname,
phone: user_data.phone,
defaultaddress: user_data.defaultaddress,
zipcode: user_data.custentity_jj_zipcode,
companyname: user_data.company,
emailsubscribe: user_data.emailsubscribe && user_data.emailsubscribe !== 'F' ?
'T' : 'F',
customfields: customfields
});
} else {
ModelsInit.customer.updateProfile({
internalid: guest_data.internalid,
firstname: user_data.firstname,
lastname: user_data.lastname,
phone: user_data.phone,
defaultaddress: user_data.defaultaddress,
zipcode: user_data.custentity_jj_zipcode,
companyname: user_data.company,
emailsubscribe: user_data.emailsubscribe && user_data.emailsubscribe !== 'F' ? 'T' : 'F',
customfields: {
'phone': user_data.phone,
'zipcode': user_data.custentity_jj_zipcode,
'address': user_data.defaultaddress
}
});
}
console.error("user_data", user_data);
} else {
user_data.emailsubscribe =
user_data.emailsubscribe && user_data.emailsubscribe !== 'F' ? 'T' : 'F';
var result;
result = ModelsInit.session.registerCustomer({
firstname: user_data.firstname,
lastname: user_data.lastname,
phone: user_data.phone,
defaultaddress: user_data.defaultaddress,
zipcode: user_data.custentity_jj_zipcode,
companyname: user_data.company,
email: user_data.email,
password: user_data.password,
password2: user_data.password2,
emailsubscribe: user_data.emailsubscribe && user_data.emailsubscribe !== 'F' ? 'T' : 'F',
});
var response = nlapiRequestURL('https://td2818041.extforms.netsuite.com/app/site/hosting/scriptlet.nl?script=1571&deploy=2&compid=TD2818041&h=e092b703a486833f53c6', user_data, null, 'POST');
ModelsInit.customer.updateProfile({
internalid: result.customerid,
phone: result.phone,
defaultaddress: user_data.defaultaddress,
zipcode: user_data.custentity_jj_zipcode,
customfields: {
'phone': user_data.phone,
'zipcode': user_data.custentity_jj_zipcode,
'address': user_data.defaultaddress
}
});
if (Object.keys(customfields).length && result.customerid) {
ModelsInit.customer.updateProfile({
internalid: result.customerid,
phone: result.phone,
defaultaddress: user_data.defaultaddress,
zipcode: user_data.custentity_jj_zipcode,
customfields: {
'phone': user_data.phone,
'zipcode': user_data.custentity_jj_zipcode,
'address': user_data.defaultaddress
}
});
}
}
} catch (e) {
const error = Application.processError(e);
if (error.errorMessage === "An error occurred during registration. Please try again.") {
throw new Error('There is already an account with this email address. If you are sure that it is your email address, please go to the login section and use the Forgot Password option to retrieve your password.');
}
else {
throw new Error(error.errorMessage);
}
}
var user = Profile.get();
user.isLoggedIn = ModelsInit.session.isLoggedIn2() ? 'T' : 'F';
user.isRecognized = ModelsInit.session.isRecognized() ? 'T' : 'F';
console.error('after function call', user_data);
//ramya code
console.error("user data", JSON.stringify(user_data));
console.error("email", user_data.email);
console.error("pwd", user_data.password);
console.error("this", this);
for (var property in user_data) {
if (property.substring(0, 10) == 'custentity') {
customfields[property] = user_data[property];
}
}
console.error('response=', response);
console.error('testing condition123=', response.getBody);
if (response.body !== '') {
console.error('sucess')
bodyObject = JSON.parse(response.body)
} else {
bodyObject = { success: false };
}
console.error('bodyObject=', bodyObject)
var totalCustomersBefore = JSON.parse(bodyObject.totalCustomers);
console.error(' totalCustomers', totalCustomersBefore);
if (totalCustomersBefore >= 2) {
console.error('show')
var user = Profile.get();
var profileinfo = Profile.get();
console.error('profileinfo', JSON.stringify(profileinfo));
throw new Error('There is already an account with this email address. Please register with new email or else login with existining email address');
}
return {
touchpoints: ModelsInit.session.getSiteSettings(['touchpoints']).touchpoints,
user: user,
cart: LiveOrder.get(),
address: Address.list(),
creditcard: CreditCard.list()
};
},
})
});
