Version: eXtendPS-SE 1.3.8 and above
Audience: Administrator, Developer

Usually, when creating a Sales Order using the Purchase Order service, we create the Sales Order under the customer which is linked to eXtendPS-SE Users record. See eXtendPS-SE - Add New User for more information on the eXtendPS-SE Users record.

Image Placeholder

If you want to set custom sub-customer references on the Sales Orders which created through the eXtendPS-SE Purchase Order service, then you can do so by adding the function below (setOrderCustomer) in the suite_promoapi_transformers.js file within NetSuite.

This function will check the email address of the contact details received from the Purchase Order request, and then it will match it with the against the list of sub-customers underneath the customer which is associated with the eXtendPS-SE Users record.

Image Placeholder

Once the match is found, the matching sub-customer will be set as the customer on the created Sales Order record.

/**
 * @param {HookContext} hookContext
 * @param {Object} hookArgs
 * @param {String} hookArgs.customer
 */
setOrderCustomer: function(hookContext, hookArgs) {
  var soRecord = hookContext.soRecord;
  var contactArray = hookContext.PO.OrderContactArray || [];
  log.debug({ title: 'setOrderContactArrayOnOrder contactArray', details: contactArray });

  //First find contact object for Order
  var contactObject = _.find(contactArray, function (contact) { return contact.contactType === 'Order'; });
  var orderContactEmail = contactObject ? _.get(contactObject, 'ContactDetails.email') : '';

  if (orderContactEmail && hookArgs.customer) {
    var subCustomerSearchResults = search.create({
      type: 'customer',
      filters: [
        ['parent', 'anyof', hookArgs.customer],
        'AND',
        ['isinactive', 'is', false],
        'AND',
        ['email', 'is', orderContactEmail],
        'AND',
        ['parent', 'noneof', '@NONE@']
      ]
    }).run().getRange({ start: 0, end: 1 });

    if (subCustomerSearchResults.length > 0) {
      //Setting order contact array customer to custom sub-customer field to set it to customer field via workflow.
      soRecord.setValue({ fieldId: 'custbody_extend_ps_se_sub_customer', value: subCustomerSearchResults[0].id });
      soRecord.setValue({ fieldId: 'entity', value: subCustomerSearchResults[0].id });
    }
  }

  if (!soRecord.getValue({ fieldId: 'entity'}) ) {
    soRecord.setValue({ fieldId: 'entity', value: hookArgs.customer });
  }
},