How to use the suit let script for restricting duplicate customer based on the subsidiary and email in the lead, customer, prospect

Using this script, we can restrict the duplicate customer based on the subsider and email and display the error message in the register page.

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */
define(['N/search', 'N/log'], function (search, log) {
  function onRequest(context) {
    var response = context.response;
    try {
    log.error('cobn',context);
    var dataParams = context.request.parameters;
    log.error('dataParams', dataParams);
      var customerSearchObj = search.create({
        type: "customer",
        filters: [
          ["email", "is", dataParams.email],
          "AND",
          ["msesubsidiary.internalid", "anyof", dataParams.subsidiary],
          "AND",
          ["stage", "anyof", "LEAD", "PROSPECT", "CUSTOMER"]
        ],
        columns: [
          search.createColumn({
            name: "entityid",
            sort: search.Sort.ASC,
            label: "Name"
          }),
          search.createColumn({ name: "email", label: "Email" }),
          search.createColumn({ name: "phone", label: "Phone" }),
          search.createColumn({ name: "altphone", label: "Office Phone" }),
          search.createColumn({ name: "fax", label: "Fax" }),
          search.createColumn({ name: "contact", label: "Primary Contact" }),
          search.createColumn({ name: "altemail", label: "Alt. Email" })
        ]
      });
    log.error('dataParams er', customerSearchObj.runPaged().count);
  response.write(JSON.stringify({ success: true, totalCustomers: customerSearchObj.runPaged().count }));
  log.error(response)
  return response;
} catch (error) {
      response.write(JSON.stringify(error))
      return  response
    }
  }
  function searchExistingCustomer(request) {
    try {
      var dataParams = request.parameters;
      var customerSearchObj = search.create({
        type: "customer",
        filters: [
          ["email", "is", dataParams.email],
          "AND",
          ["msesubsidiary.internalid", "anyof", dataParams.subsidiary],
          "AND",
          ["stage", "anyof", "LEAD", "PROSPECT", "CUSTOMER"]
        ],
        columns: [
          search.createColumn({
            name: "entityid",
            sort: search.Sort.ASC,
            label: "Name"
          }),
          search.createColumn({ name: "email", label: "Email" }),
          search.createColumn({ name: "phone", label: "Phone" }),
          search.createColumn({ name: "altphone", label: "Office Phone" }),
          search.createColumn({ name: "fax", label: "Fax" }),
          search.createColumn({ name: "contact", label: "Primary Contact" }),
          search.createColumn({ name: "altemail", label: "Alt. Email" })
        ]
      });
if (customerSearchObj.runPaged().count > 0) {
  response.write(JSON.stringify({ success: true, message: 'Duplicate customer with the same email already exists in this subsidiary.' }));
  log.error(response)
  return response;
}
      var searchResult = customerSearchObj.run();
      var searchResultCount = searchResult.getRange({ start: 0, end: 1 }).length;
      var customer = [];
      searchResult.each(function (result) {
        var internalIdObj = {};
        internalIdObj.id = result.getValue({ name: 'internalid', label: 'Internal ID' });
        customer.push(internalIdObj);
        return true;
      });
      return customer;
    } catch (error) {
      log.error('ERROR', error);
      throw error;
    }
  }
  return {
    onRequest: onRequest
  };
});

Leave a comment

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