I would like to create a custom field called Email on the customer form and have it auto fill with the customer email address with the "Primary contact" role. I am new to writing scripts so I tried applying the below but it did not work:
My primary role in NetSuite has Internal ID of -14. So I did the below:
function getContactEmail() {
var numItem = nlapiGetLineItemCount('contactroles');
for (var i=1; i<=numItem; i++)
{
var contact_rec = nlapiGetLineItemValue('contactroles', 'contact', i); // get all the contact name of the contacts of the customer
var contactEmailAddress = nlapiGetLineItemValue('contactroles','email', i); // get the e-mail address of the contact
var contactRole = nlapiLookupField('contact',contact_rec,'contactrol e'); // get the internal ID of the role of that particular contact from the customer
if (contactRole == '-14') //check is the role is 'Primary Contact' or not.
{
nlapiSetFieldValue('custentity_email',contactEmail Address);//set the value of custom field with email address of the Contact which has 'Primary Contact' role
}
}
}
I applied the below steps and added the above script instead of the one mentioned below but it is not working at all. I would appreciate if anyone can help. Thanks
Step 1: Create a Custom field of type 'Email Address' by navigating to Customization>Lists, Records, & Fields>Entity Fields>New
-Label: any name
-Type: Email Address
-Store Value: Checked
-Check the checkbox 'Customer' under 'Applies To' field.
-Under 'Display' tab;
--Subtab: Main
--Display type: Normal
-Click Save.
Step 2: Create a java script file with following sample code;
function getContactEmail() {
var numItem = nlapiGetLineItemCount('contactroles');
for (var i=1; i<=numItem; i++)
{
var contact_rec = nlapiGetLineItemValue('contactroles', 'contact', i); // get all the contact name of the contacts of the customer
var contactEmailAddress = nlapiGetLineItemValue('contactroles','email', i); // get the e-mail address of the contact
var contactRole = nlapiLookupField('contact',contact_rec,'contactrol e'); // get the internal ID of the role of that particular contact from the customer
if (contactRole == '1') //check is the role is 'Accounts Payable' or not.
{
nlapiSetFieldValue('custentity_email',contactEmail Address);//set the value of custom field with email address of the Contact which has 'Accounts Payable' role
}
}
}
Step 3: Create a workflow action script record in NetSuite by going to Customization>Scripting>Scripts>New and select Workflow Action from List;
-Name: any name
-Under Scripts tab;
--Select the JavaScript file created in Step1 under 'Script File' field.
--Put 'getContactEmail' under 'Function' field.
-Click Save and Deploy.
-On Deployment page,
--Select Customer under 'Applies To' field.
--Put 'released' under status.
--Put 'Administrator' under 'Execute As Role'
--Select 'Select All' under Audience tab.
-Click Save.
Step 4: Create a workflow by going to Customization>Scripting>Workflow>New
-Name: any name
-Select 'Customer' under Record Type.
-Select 'Customer' under Sub Types as well.
-Check 'Execute as Admin' checkbox.
-Select 'Released' under Release Status.
-Check 'Enable Logging' checkbox.
- Select 'Event Based' under Initiation tab.
-Check 'On Create' and 'On Update' check boxes.
-Select 'Before Record Submit' under Trigger Type.
-Click Save. -Select the custom workflow action created in Step2 from New Action List and set the 'Before Record Submit' for Trigger Type on that action and click Save.