For auto populating the task, event and phone call details to the custom Sales tab in Customer record. We need to create a custom CRM List/Record field Customer in the task, event and phone call records. Check the ‘Record is Parent’ check box in the field creation page. Also select Sales in the Parent Subtab field below the Display tab. Using script, set the Company field value to the custom Customer field. By setting this value, the details will be automatically populated in the Sales tab of the Customer record. To remove the details from the Communication subtab, we need to nullify the Company field. If the company name appears in the Participants sublist, we also need to nullify the sublist.
define([‘N/record’, ‘N/search’, ‘N/runtime’],
/**
* @param{record} record
* @param{search} search
* @param{runtime} runtime
*/
(record, search, runtime) => {
/**
* Defines the function definition that is executed before record is submitted.
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord – New record
* @param {Record} scriptContext.oldRecord – Old record
* @param {string} scriptContext.type – Trigger type; use values from the context.UserEventType enum
* @since 2015.2
*/
const beforeSubmit = (scriptContext) => {
try {
let executionContext = runtime.executionContext;
// Only run for WEBSERVICES or USERINTERFACE contexts, and for CREATE or EDIT actions
if ((executionContext === runtime.ContextType.WEBSERVICES || executionContext === runtime.ContextType.USER_INTERFACE) &&
(scriptContext.type === scriptContext.UserEventType.CREATE || scriptContext.type === scriptContext.UserEventType.EDIT)) {
let newRecord = scriptContext.newRecord;
let customerId = newRecord.getValue({
fieldId: ‘company’
});
if (customerId) {
// Use a helper function to check if the company is a customer
let isCustomer = checkIfCustomer(customerId);
if (isCustomer) {
newRecord.setValue({
fieldId: ‘custevent_jj_customer’,
value: customerId
});
let customField = newRecord.getValue({
fieldId: ‘custevent_jj_customer’
});
// Clear the company field to prevent it from being shown in the Communication tab
newRecord.setValue({
fieldId: ‘company’,
value: ”
});
} else {
log.debug(“Company is not a Customer, skipping setting the custom field.”);
}
}
// Remove any lines from the contact sublist
removeContactLines(newRecord);
}
} catch (e) {
log.error(“Error at before submit”, e)
}
}
/**
* Helper function to check if the given company ID is a customer.
* @param {number} companyId – The ID of the company to check.
* @returns {boolean} – True if the company is a customer, false otherwise.
*/
const checkIfCustomer = (customerId) => {
let customerSearch = search.create({
type: search.Type.CUSTOMER,
filters: [
[“internalid”, “is”, customerId],
“AND”,
[“subsidiary”, “anyof”, “5”, “4”, “1”]
],
columns: [‘internalid’]
});
let customerResult = customerSearch.run().getRange({ start: 0, end: 1 });
return customerResult && customerResult.length > 0;
};
/**
* Helper function to remove lines from the contact sublist if they have a company value.
* @param {Object} newRecord – The current newRecord instance.
*/
const removeContactLines = (newRecord) => {
let lineCount = newRecord.getLineCount({ sublistId: ‘contact’ });
for (let i = lineCount – 1; i >= 0; i–) {
let contactCompany = newRecord.getSublistValue({
sublistId: ‘contact’,
fieldId: ‘company’,
line: i
});
if (contactCompany) {
newRecord.removeLine({
sublistId: ‘contact’,
line: i
});
}
}
};
return {
beforeSubmit
}
});