Autopopulate Current User as Sales Rep in Sales Orders (Sales Team feature)

  • This customization automatically adds the current user as the Sales Representative in the Sales Team subtab of a Sales Order whenever a new customer is selected. The feature is implemented as a client script in NetSuite and aims to streamline the process of populating sales representatives for newly created Sales Orders.

Objective

This customization is designed to:

  • Automatically add the current user as a Sales Representative in the “Sales Team” subtab when creating a new Sales Order.
  • Allow the standard behavior if a Sales Representative is already assigned to the customer.

Features

  1. Autopopulate Sales Rep for New Sales Orders:
  • When creating a Sales Order and selecting a customer, this script will add the current user to the “Sales Team” subtab if the customer doesn’t already have a designated Sales Representative.
  1. Existing Sales Representative Handling:
  • If the selected customer already has an assigned Sales Representative, the standard functionality remains unaffected. The script will not override any existing assignments.

Implementation Details

Script Entry Point: fieldChanged

The fieldChanged function is used to execute the autopopulation process whenever the “entity” field is modified.

Parameters for fieldChanged

  • scriptContext: Object containing the following properties:
  • currentRecord: The current record on which the script is executing.
  • sublistId: The sublist name where the field is changed.
  • fieldId: The field name that has been changed.
  • lineNum: The line number in the sublist (if applicable).
  • columnNum: The column number in a matrix field (if applicable).

Logic Workflow

  1. Trigger Point:
  • The script is triggered when the “entity” field (i.e., the customer) is changed in the Sales Order form.
  1. Sales Team Population:
  • Current User Check: The script first gets the current user ID using runtime.getCurrentUser().
  • Customer Sales Rep Lookup: It then uses search.lookupFields to determine if the customer has an existing Sales Representative.
  • Condition:
  • If the current user is already assigned as the Sales Representative, no further action is taken.
  • Otherwise, the script adds the current user to the “Sales Team” sublist.
  1. Adding Sales Representative:
  • The script inserts a new line in the “Sales Team” subtab and sets the following values:
  • Employee: Set to the current user.
  • Primary: Set to true (indicating that the user is the primary representative).
  • Sales Role: Set to -2 (indicating a generic or standard sales role).
  • Contribution: Set to 0 (contribution percentage).

Error Handling

If an error occurs during the execution of the script, an error message is logged for debugging purposes.

Code Snippet

Below is a relevant excerpt of the code:

function fieldChanged(scriptContext) {
    try {
        if (scriptContext.fieldId == 'entity') {
            let salesOrderRecord = currentRecord.get();
            let currentUser = runtime.getCurrentUser().id;


            let customerRecordId = salesOrderRecord.getValue({
                fieldId: 'entity'
            });


            let salesRep = search.lookupFields({
                type: search.Type.CUSTOMER,
                id: customerRecordId,
                columns: 'salesteammember'
            });


            let isSalesRepPresent = null;
            if (salesRep && salesRep.salesteammember && salesRep.salesteammember.length > 0) {
                isSalesRepPresent = salesRep.salesteammember[0].value;
            }


            if (currentUser == isSalesRepPresent) {
                return;
            }


            salesOrderRecord.selectNewLine({ sublistId: 'salesteam' });
            salesOrderRecord.setCurrentSublistValue({ sublistId: 'salesteam', fieldId: 'employee', value: currentUser, ignoreFieldChange: false, forceSyncSourcing: true });
            salesOrderRecord.setCurrentSublistValue({ sublistId: 'salesteam', fieldId: 'isprimary', value: true, ignoreFieldChange: false, forceSyncSourcing: true });
            salesOrderRecord.setCurrentSublistValue({ sublistId: 'salesteam', fieldId: 'salesrole', value: -2, ignoreFieldChange: false, forceSyncSourcing: true });
            salesOrderRecord.setCurrentSublistValue({ sublistId: 'salesteam', fieldId: 'contribution', value: '0', ignoreFieldChange: false, forceSyncSourcing: true });
            salesOrderRecord.commitLine({ sublistId: 'salesteam' });
        }
    } catch (e) {
        log.error("Error@fieldChanged", e);
    }
}

This customization enhances the efficiency of the sales process by ensuring that the current user is added to the Sales Order without requiring manual entry. The automation reduces errors and ensures consistency, particularly when handling customer-specific Sales Representatives.

Leave a comment

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