User event script to avoid duplicate record creation

321 Views Asked by At

I want to create user event script to avoid duplicate record creation. I have created script but its not working. Still permitting to create duplicate record means same field values.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */
define(['N/record', 'N/email', 'N/url', 'N/runtime', 'N/search'],
  function (record, email, url, runtime, search) {

    function beforeSubmit(scriptContext) {
      if (scriptContext.type === 'create' || scriptContext.type === 'edit') {
        var rec = scriptContext.newRecord;
        log.debug("rec", rec);
        var recordId = record.id;
        log.debug("recordId", recordId);

        var category = rec.getValue({fieldId: 'custrecord_gbs_tren_cat'});
        log.debug("category", category);
        var exemptNexus = rec.getValue({fieldId: 'custrecord_gbs_tren_expt_nexus'});
        log.debug("exemptNexus", exemptNexus);
        //throw error.create("You cannot create duplicate record");
        
        var existing_record = false;
        var customrecord_gbs_tax_rule_expt_nexusSearchObj = search.create({
          type   : "customrecord_gbs_tax_rule_expt_nexus",
          filters:
            [
              ["custrecord_gbs_tren_cat", "anyof", category],
              "AND",
              ["custrecord_gbs_tren_expt_nexus", "anyof", exemptNexus]
            ],
          columns:
            [
              search.createColumn({name: "custrecord_gbs_tren_cat", label: "Category"}),
              search.createColumn({name: "custrecord_gbs_tren_expt_nexus", label: "Exempt Nexus"})
            ]
        });
        customrecord_gbs_tax_rule_expt_nexusSearchObj.runPaged().count;
        //log.debug("expensereportSearchObj result count",searchResultCount);
        // alert('Expenses Count: '+searchResultCount);

        customrecord_gbs_tax_rule_expt_nexusSearchObj.run().each(function () {
          // .run().each has a limit of 4,000 results
          existing_record = true;
          return false;
        });
      }
    }

    return {
      beforeSubmit: beforeSubmit
    }
  });
1

There are 1 best solutions below

2
SuiteStar On

If you want to user event script to show error to user then use N/error module and do like this in script:-

function beforeSubmit(scriptContext) {

         var currentRecord = scriptContext.newRecord;
         var recordId=currentRecord.id;
         log.debug("recordId",recordId);

         //getting field values
         var category=currentRecord.getValue({ fieldId:'custrecord_gbs_tren_cat'});
                log.debug("category",category);
         var exemptNexus=currentRecord.getValue({ 
            fieldId:'custrecord_gbs_tren_expt_nexus'});
                log.debug("exemptNexus",exemptNexus);
                              
         var customrecord_gbs_tax_rule_expt_nexusSearchObj = search.create({
                            type: "customrecord_gbs_tax_rule_expt_nexus",
                            filters:
                                    [
                                   ["custrecord_gbs_tren_cat","anyof",category], 
                                     "AND", 
                              ["custrecord_gbs_tren_expt_nexus","anyof",exemptNexus]
                               ],
                          columns:
                              [
                               search.createColumn({name: "custrecord_gbs_tren_cat", label: "Category"}),
                               search.createColumn({name: "custrecord_gbs_tren_expt_nexus", label: "Exempt Nexus"})
                               ]
                               });
            var searchResultCount = customrecord_gbs_tax_rule_expt_nexusSearchObj.runPaged().count; 
            alert("expensereportSearchObj result count: "+searchResultCount);  
                   
         if (searchResultCount>0) {
            
            var myCustomError = error.create({
                        name: 'Record Duplicate',
                        message: 'You have entered duplicate value',
                        notifyOff: true
              });
            
             throw myCustomError;  
     }

I hope it will work.

If it is relevant please mark as green for others help as well.

Thanks,