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
}
});
Beginner Asked on February 24, 2023 in SuiteScript.
Add Comment
6 Answer(s)

Is the CSV import configured to run scripts and workflows?

Intermediate Answered on February 24, 2023.
Add Comment

Yes

 

Beginner Answered on February 24, 2023.
Add Comment

I would suggest you add some logging to see if the script is running at all.

Also, could you share your script deployment settings.

Intermediate Answered on February 24, 2023.
Add Comment

I have checked all things.

Beginner Answered on February 24, 2023.
Add Comment

Have you identified whether the script is being run by adding some additional logging?

Intermediate Answered on February 24, 2023.
Add Comment

You should add logging at the very beginning of the entry point to verify it is running.  There are many settings that could impact this.  UE scripts can have script contexts filtered out at the deployment level.  Users and roles can also be filtered out for a UE script.   However, one final thing to consider.  If you have any SuiteCloud+ licenses, you could still have a race condition with your UE script if using multiple threads or queues for CSV imports.  This approach cannot prevent against that, no matter how it is written.  You should also remove the edit type from the script, or else it will prevent the editing of any existing records, since it does not filter itself out in the duplicate check.

Beginner Answered on February 24, 2023.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   Become a Sponsor   Become a Sponsor