RE: Script is not functioning to avoid duplicate record creation when record is imported via CSV

Hi,

I have created script to throw error message if user tries to create duplicate custom record. Script trows error when user tries to create duplicate record. But when I am trying to import records through CSV import, duplicate record gets created.

Anyone knows reason? I will appreciate your help. Thanks!

 

Here is my script

/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
 define([‘N/record’, ‘N/email’, ‘N/url’, ‘N/runtime’, ‘N/search’, ‘N/ui/serverWidget’,’N/error’],
  function(record, email, url, runtime, search, serverWidget,error) {
  function beforeSubmit(scriptContext){
    if(scriptContext.type === ‘create’ || scriptContext.type === ‘edit’ ){
      try{
      var rec=scriptContext.newRecord;
      log.debug(“rec”,rec);
      var recType=rec.type;
      log.debug(“recType”,recType);
      /*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 searchResultCount;
                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: “internalid”, label: “Internal ID”})
                ]
                });
                searchResultCount = customrecord_gbs_tax_rule_expt_nexusSearchObj.runPaged().count;
                log.debug(“customrecord_gbs_tax_rule_expt_nexusSearchObj result count”,searchResultCount);
                customrecord_gbs_tax_rule_expt_nexusSearchObj.run().each(function(result){
                // .run().each has a limit of 4,000 results
                return true;
                });
                log.debug(“searchResultCount”,searchResultCount);
  }catch(error)
  {
     log.error(‘error’,error)
  }
    /*}
    if(category==category2 && exemptNexus==nexus2)
    {
    throw ‘A record with the same name already exists.’;
     // return false;
    }
    else{
    var recId=rec.save();
    log.debug(“recId”,recId);
    }*/
    }
    if(searchResultCount>0)
    {
      throw ‘You cannot create a duplicate record.’;
    }
    }
//}//function dupcheck
  return {
    beforeSubmit:beforeSubmit
}
});
Maira S Beginner Asked on February 24, 2023 in SuiteScript.
Add Comment
6 Answers

Yes

 

Beginner Answered on February 24, 2023.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.