Hi all, Simple use case imo, but could not make it...
# suitescript
j
Hi all, Simple use case imo, but could not make it work. When I save a Customer record, I want to check if one specific field value exists at another customer. If so, the Customer should not be saved and an error message should be shown. When field value is empty or does not exists the Customer should be saved. I have created a client script that has the saveRecord function. Within the function I run a search to find Customers with the same value, I add the result in an array and check if the array has a result. If so, I give an error message and make sure the Customer should not be created. The problem is within making it work when there is no value entered or value is unique. The return = true within the saveRecord is not working correctly. Any help would be appreciated 🙂 Below my code
Copy code
/**
 *@NApiVersion 2.1
 *@NScriptType ClientScript
 */
define(["N/record", "N/ui/dialog", "N/url", "N/search"], function (record, dialog, url, nSearch) {
  //
  // Jurgen Rense
  // This script is executed on the Customer.
  // When the Customer is created or updated there will be a validation if the Hubspot key is unique, if not the duplicate customer will be shown in the error message
  //

  function saveRecord(context) {
    var rec = context.currentRecord;

    // Get Customer
    var customerId = rec.getValue({ fieldId: "id" });
    var customerHubspotKey = rec.getValue({
      fieldId: "custentity_inn_hubspot_key",
    });

    var duplicates = [];
   
 
    // if (validateSaveRecord(customerHubspotKey)) {
    //   log.debug({
    //     title: "True line 26"
    //   });
    //   return true;
    // } else {
    //   log.debug({
    //     title: "False line 31"
    //   });
    //   return false;
    // }

    validateSaveRecord(customerHubspotKey)
      
    async function validateSaveRecord(customerHSKey) {
      if (customerHSKey) {
        // return new Promise(function (resolve) {
          log.debug({ title: "Zoeken" });
          await findCustomerHubspotKey(customerId, customerHSKey).then (function(result) {
            log.debug({ title: "Result" + duplicates });
            //log.debug({ title: resultSet ? "ja" : "nee", details: resultSet });
            if (validationCheck(duplicates)) {
              log.debug({ title: "True" });
              return true;
            }
            log.debug({ title: "False" }); 
             return false;
          });
        // });
      } else {
      log.debug({ title: "no key" + customerHSKey });
      return true;
      }
    }

    async function findCustomerHubspotKey(customerId, customerHubspotKey) {
        var search = nSearch.create({
          type: "customer",
          filters: [
            ["internalid", "noneof", customerId],
            "AND",
            ["custentity_inn_hubspot_key", "contains", customerHubspotKey],
          ],
          columns: [
            nSearch.createColumn({ name: "entityid", label: "entityid" }),
            nSearch.createColumn({ name: "companyname", label: "companyname" }),
          ],
        });

        var resultSet = await search.run().each.promise(function (result) {
          //duplicateHubspotKeyCustomer = result.getValue({name: "entityid"});
          duplicates.push(result.getValue({ name: "entityid" }));
          log.debug({ title: "Debug in functie: ", details: duplicates });

          dialog.alert({
            title: "Error",
            message:
              "<p>Hubspot key already exists for customer: " +
              result.getValue({ name: "companyname" }) +
              "</p><p><br>Please make sure that you use the correct Hubspot key or merge the lead/prospect/customer</p>",
          });
        });
                
    }

    function validationCheck(duplicates) {
      log.debug({ title: "length: " + duplicates.length });
      if (duplicates.length === 0) {
        log.debug({
          title: "True - Debug ",
          details: duplicates,
        });
        return true;
      } else {
        log.debug({
          title: "False - Debug",
          details: duplicates,
        });
        return false;
      }
    }
  }

  return {
    saveRecord: saveRecord,
  };
});
s
You are calling
validateSaveRecord
instead of calling/returning it, your return function is not returning anything
j
Hi Sandii, thanks for replying. Should I change this: validateSaveRecord(customerHubspotKey) to this: return validateSaveRecord(customerHubspotKey)
?
s
Assuming all the other logic is working as intended, yes.
j
Still not working correctly. When I click Save, the customer is saved and after that the alert is popping up. attached is debug log
It's giving me a return = true, before the actual script runs. Something goes wrong with the order of execute
I think
s
It looks a little over-engineered to me personally, I don't really see the need for async/multiple functions with how simple the logic of this script is
Copy code
exports.saveRecord = function saveRecord(context) {
        // read id and hubspot key
        // if key is empty/null, return true

        // search.create() with key/id, 
        // if search.run().getRange(0,1).length return false

        // return true
    }
thats really all the logic you need
1
Or you could probably capture their response on dialog.alert(), and should be returning the dialog instead.
j
thanks Sandii, will try tomorrow
r
Why don't you try beforesubmit userevent?
j
Can I show a dialog alert with that?
r
Yes you can try ut
j
Thanks Sandii, your proposed solution did work out correctly.
s
FYI dialog is a client side module, it would not make sense and is not supported to use dialog in user event context. With that in mind, you might want to consider if customers are made in other contexts for a better/more complete solution. For a simple example, a csv upload that breaks this double hubspot key criteria would not get caught with your current solution.