Requirement
In our NetSuitecustomer record, we also use 3 main contact fields under “Email / Phone / Address” section on the main body:Main ContactMain Contact EmailMain Contact PhoneIf there’s conflicting information for these 3 fields, can you create a new contact record using these 3 fields of the duplicate record and then link this new contact record under the newly created master record?
Solution
- We will create a user event script and deploy in customer record; this script will be achieving this functionality.
2.When anew customer is created in the NetSuite. We will check the main contact field (field id: ‘custentity14’) has value.
3.If main contact field has value, then we will fetch values from the fields main contact email (field id: ‘email’) and the main contact phone (field id: ‘phone’).
4.Using these values, we will create a new contact record and attached this contact with respective customer record. This customization work on the create/edit context of customer record.
5.We’ll also establish a custom field to track the creation of the contact record. We will mark the check box as true after creating the contact record from the customer record.
6.Existing NetSuite customers are not affected by this automatic contact creation. New customers created after the script’s deployment date are eligible to have their contacts created automatically.
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
/*************************************************************
* NTI-77
* Date : 20-03-2022
* Author: Jobin & Jismi IT Services LLP
* Description: create contact and attach automatically when customer is created
*************************************************************/
define(['N/record', 'N/search'],
/**
* @param{record} record
* @param{search} search
*/
(record, search) => {
function createAndAttchContact(customer, contact, email, phone) {
try {
//create contact record
var contactRec = record.create({
type: record.Type.CONTACT,
isDynamic: true,
});
contactRec.setValue('firstname', contact);
contactRec.setValue('entityid', contact);
contactRec.setValue('email', email);
contactRec.setValue('phone', phone);
contactRec.setValue('externalid', customer.id);
contactRec.setValue('company', customer.id);
var contactID = contactRec.save({ignoreMandatoryFields:true});
return contactID;
} catch (e) {
log.debug("Error@createAndAttchContact", e)
customer.setValue('custentity_jj_errormessage_nti76', e.message)
return false;
}
}
function contactExistSearch(contact, customer) {
try {
var contactSearchObj = search.create({
type: "contact",
filters:
[
["externalid", "is", customer]
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"})
]
});
var searchResultCount = contactSearchObj.runPaged().count;
log.debug("contactSearchObj result count", searchResultCount);
var internalid = null;
if (searchResultCount > 0) {
contactSearchObj.run().each(function (result) {
internalid = result.getValue({name: "internalid", label: "Internal ID"});
return false;
});
return internalid;
} else
return false;
} catch (e) {
log.debug("Error@contactsearch", e)
return false;
}
}
function updateEmailAndPhone(contact, email, phone, contactID, custID) {
try {
var fieldLookUp = search.lookupFields({
type: search.Type.CONTACT,
id: contactID,
columns: ['entityid', 'email', 'phone']
});
if (fieldLookUp.entityid != contact || fieldLookUp.email != email || fieldLookUp.phone != phone) {
var id = record.submitFields({
type: record.Type.CONTACT,
id: contactID,
values: {
//firstname: contact,
email: email,
phone: phone
},
options: {
enableSourcing: false,
ignoreMandatoryFields: true
}
});
if (contact)
record.submitFields({
type: record.Type.CONTACT,
id: contactID,
values: {
firstname: contact,
entityid: contact
},
options: {
enableSourcing: false,
ignoreMandatoryFields: true
}
});
}
} catch (e) {
log.debug("Error@updateEmailAndPhone", e)
custID.setValue('custentity_jj_errormessage_nti76', e.message)
}
}
const afterSubmit = (scriptContext) => {
try {
log.debug("***UE Started***")
//execute if context is Create/Edit
var recID = scriptContext.newRecord;
var customerID = record.load({
type: record.Type.CUSTOMER,
id: recID.id,
isDynamic: true,
});
var mainContact = customerID.getValue({fieldId: 'custentity14'});
var mainEmail = customerID.getValue({fieldId: 'email'});
var mainPhone = customerID.getValue({fieldId: 'phone'});
var isPerson = customerID.getValue({fieldId: 'isperson'});
log.debug("isperson", isPerson)
if (scriptContext.type == scriptContext.UserEventType.CREATE) {
try {
//check if the Main contact field has value
if (mainContact && (isPerson ==='F'))
var contactREc = createAndAttchContact(customerID, mainContact, mainEmail, mainPhone);
if (contactREc)
customer.setValue('custentity_jj_contactcreated_nti76', true);
// customer.setValue('custentity_jj_errormessage_nti76', null);
//customerID.setValue('custentity_jj_errormessage_nti76', null)
} catch (e) {
log.debug("Error@CreateContext", e)
customerID.setValue('custentity_jj_errormessage_nti76', "CREATE CONTEXT: " + e.message)
}
}
if (scriptContext.type == scriptContext.UserEventType.EDIT) {
try {
//script should execute only if the record is created after the script deployment
let dateCreated = new Date(customerID.getValue('datecreated'));
let deployedDate = new Date('3/28/2022 6:40 am')
log.debug("datacreatd>datedeployed1", dateCreated > deployedDate)
if (dateCreated > deployedDate) {
//check if a customer with same contact name exists
let contactExist = contactExistSearch(mainContact, customerID.id);
// if contact exists, fn returned internal id else false
if (contactExist) {
//update the phone and email in contact record if they're updated in customer record.
updateEmailAndPhone(mainContact, mainEmail, mainPhone, contactExist, customerID)
} else {
if (mainContact && (isPerson ==='F'))
createAndAttchContact(customerID, mainContact, mainEmail, mainPhone);
}
}
customerID.setValue('custentity_jj_errormessage_nti76', null)
} catch (e) {
log.debug("Error@EditContext", e)
customerID.setValue('custentity_jj_errormessage_nti76', "EDIT CONTEXT: " + e.message )
}
}
customerID.save({ignoreMandatoryFields:true});
log.debug("***UE ENDED***")
}
catch (e) {
log.debug("Error@afterSubmit", e)
}
}
return {afterSubmit}
});