6 Replies · Latest reply on Dec 26, 2017 7:29 AM by chalan lokesh

    Need help to remove duplicates in the Background script

      Copying the watchlist from one table to other table. whereas my below code is working fine but only issue i am finding with the duplicates

      Note: ArrayUtil i tried but no luck or may be i am using it Wrongly. Can someone help in this code

       

       

      Code:

       

      ----~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~----------------------

       

      var gib = new GlideRecord('u_gib');

      var query = "sys_id=80c23880f168f400b3a17aa3202fa263"

      //var query = "u_gib_watchlistISNOTEMPTY";

      gib.addQuery(query);

      gib.query();

      while(gib.next()){

      var count = 0;

       

      var records = '';

      var watchlistvalue = gib.u_gib_watchlist;

      //gs.print('watclist>>'+watchlistvalue );

       

       

      var ibat = new GlideRecord('u_combined_pbues');

      ibat.addQuery('u_gib_equip_serial_number', gib.u_serial_number);

      ibat.query();

       

      if(ibat.next()){

      records += watchlistvalue;

      //gs.print('records ::'+records );

      }

       

      ibat.u_sites_watchlist += ','+ records;

      gs.print('IBAT Watchlist ::'+ibat.u_sites_watchlist );

       

      ibat.autoSysFields(false);

      ibat.setWorkflow(false);

      ibat.update();

       

       

      }

       

       

      gs.print('RowCount>>'+gib.getRowCount());

       

      ---------------------------------------------------------------------------------

       

      Regards,

      Chalan

        • Re: Need help to remove duplicates in the Background script
          Ankur Bawiskar

          Hi Lokesh,

           

          If you are getting duplicates then you can use ArrayUtil unique method.

          If you are in scoped app then call the script include as follows:

           

          var arrayutil = new global.ArrayUtil();

           

          Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.

          Thanks

          Ankur

          • Re: Need help to remove duplicates in the Background script
            gowrisankar sativada

            Add this lines of code before update:

             

            var answer = ibat.u_sites_watchlist;

            answer = answer.toString().split(',');

            var fin_answer = new ArrayUtil().unique(answer);

            ibat.u_sites_watchlist = fin_answer;

            GowriSankar, Community Citizen :)
            Award Correct Answer if my Solution Works, Mark helpful if it helps...

            • Re: Need help to remove duplicates in the Background script
              Himanshu Verma

              Hi Lokesh

               

              Please try below:

               

              var gib = new GlideRecord('u_gib');

               

               

              var query = "sys_id=80c23880f168f400b3a17aa3202fa263"

               

               

              gib.addQuery(query);

               

               

              gib.query();

               

               

              while(gib.next()){

               

               

              var ibat = new GlideRecord('u_combined_pbues');

               

               

              ibat.addQuery('u_gib_equip_serial_number', gib.u_serial_number);

               

               

              ibat.query();

               

               

              if(ibat.next()){

               

               

              var a = ibat.u_sites_watchlist+","+gib.u_gib_watchlist;

               

               

              var arr = new ArrayUtil().unique(a.split(','));

              ibat.u_sites_watchlist =arr;

               

               

              gs.print('IBAT Watchlist ::'+ibat.u_sites_watchlist );

               

               

              ibat.autoSysFields(false);

               

               

              ibat.setWorkflow(false);

               

               

              ibat.update();

               

               

               

               

              }

               

               

              }

               

              Please mark your question as answered if it resolves your issue.

               

              Please Hit ✅Correct, ⭐️Helpful, or ❣️Like depending on the impact of the response.

              • Re: Need help to remove duplicates in the Background script
                chalan lokesh

                **tried in this way as well, worked as expected. thank you **

                 

                 

                var gib = new GlideRecord('u_gib');

                var query = "sys_id=80c23880f168f400b3a17aa3202fa263"

                //var query = " u_gib_watchlistISNOTEMPTY";

                gib.addEncodedQuery(query);

                gib.query();

                if(gib.next()){

                var count = 0;

                var records = '';

                 

                var watchlistvalue = gib.u_gib_watchlist;

                gs.print('watclist>>'+watchlistvalue );

                 

                var ibat = new GlideRecord('u_combined_pbues');

                ibat.addQuery('u_gib_equip_serial_number', gib.u_serial_number);

                ibat.query();

                 

                if(ibat.next()){

                 

                 

                var test = ibat.u_sites_watchlist;

                 

                 

                records += watchlistvalue ;

                gs.print('records ::'+records );

                }

                 

                var dup=0;

                var gibvar = records.split(",");

                var ibatvar = test.split(",");

                //gs.print("Ibat watchlist::"+ibatvar.length);

                //gs.print("Gib watchlist::"+gibvar .length);

                 

                 

                for(var c=0; c<gibvar.length;c++){

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

                if(gibvar[c]==ibatvar[i])

                {

                dup=1;

                }

                }

                //gs.print('dupli>>'+dup);

                if(dup==0){

                 

                 

                ibat.u_sites_watchlist += ',' + gibvar[c];

                //gs.print('gibvar[c]::'+gibvar[c]);

                //gs.print('ibat.u_sites_watchlist'+ibat.u_sites_watchlist);

                }

                dup=0;

                }

                 

                 

                gs.print('ibat watchlist updated::'+ibat.u_sites_watchlist);

                 

                 

                        ibat.autoSysFields(false);

                ibat.setWorkflow(false);

                //ibat.update();

                 

                 

                }

                gs.print('RowCount>>'+gib.getRowCount());