RE: How to write script to avoid duplicate record creation before record submit using suitescript 2.0

I want to create a user event script to avoid custom record creation if record is duplicate.

Also this custom record is being attached to another custom record. I have to stop from creating and attaching custom duplicate record.

Please Help!!

Maira S Beginner Asked on February 23, 2023 in SuiteScript.
Add Comment
10 Answers

Thanks,

I have written code in 2.0. But record saves even if record is duplicate. Could you understand what is wrong here?

 

/**

 

 *@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’ ){

 

      var rec=scriptContext.newRecord;

 

      var recordId=rec.id;

 

      log.debug(“recordId”,recordId);

 

          var dupList = CheckDuplicate();

 

      log.debug(“dupList”,dupList);

 

            var isDuplicate =false;

 

            for(var i=0; i<dupList.length; i++)

 

            {

 

 

 

                    var category=rec.getValue({ fieldId:’custrecord_gbs_tren_cat’});

 

                    var exemptNexus=rec.getValue({ fieldId:’custrecord_gbs_tren_expt_nexus’});

 

                    if(dupList[i].category == category && dupList[i].exemptNexus == exemptNexus){

 

                        isDuplicate = true;  // There is a duplicate line.

 

                         return false;

 

 

 

 

 

                }//if

 

                else{

 

                  return true;

 

                }

 

                //throw error.create(“You cannot create duplicate record”);

 

      }//for

 

    }//if close

 

  }//beforesubmit close

 

  function CheckDuplicate(recordId){

 

    var res=[];

 

    var customrecord_gbs_tax_rule_expt_nexusSearchObj = search.create({

 

      type: “customrecord_gbs_tax_rule_expt_nexus”,

 

      filters:

 

      [

 

      ],

 

      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;

 

        //log.debug(“expensereportSearchObj result count”,searchResultCount);

 

       // alert(‘Expenses Count: ‘+searchResultCount);

 

       customrecord_gbs_tax_rule_expt_nexusSearchObj.run().each(function(result){

 

            // .run().each has a limit of 4,000 results

 

 

 

         var category=result.getValue({name: “custrecord_gbs_tren_cat”, label: “Category”});

 

         var exemptNexus =result.getValue({name: “custrecord_gbs_tren_expt_nexus”, label: “Exempt Nexus”});

 

         if(result.id != recordId){// Exclude the current record

 

            res.push({

 

                ‘category’:category,

 

                ‘exemptNexus’:exemptNexus

 

            });

 

         }

 

            return true;

 

        });

 

         return res;

 

}//function dupcheck

 

  return {

 

    beforeSubmit:beforeSubmit

 

}

 

});

 

Beginner Answered on February 23, 2023.
Add Comment

Your Answer

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