Remove duplicates from Concatenated Child to Parent Field - Involves Javascript

  • 0
  • 1
  • Question
  • Updated 1 year ago
  • Answered
  • (Edited)
I have a field that is using Dan Diebolt's IOL technique that is Concatenating child fields to the parent record into one combined field.  I would like for this field to remove all duplicates and blanks. Can someone please help?  Field 173 that is being combined might say the same thing in each record so it is repeating in my combined field and I want it to only be listed once.  Field 173 may also be blank and I do not want the blank spaces in the combined field.  I appreciate any help!  Thank you!

Here is the code I am using: 

(function(){
  var XMLFlatToObj = function(xml, type) {
    var data = [];
    var record = {};
    $("record", xml).each(function() {
      record = {};
      $(this).children().each(function () {
        record[$(this).prop("tagName")] = $(this).text();
      });
      data.push(record);
    });
    return {records: data};
  }
  var dbid = "bjvehdwbq";
  var dbidParents = "bjvehdwcb";
  var dbidChilds = "bjvehdwcj";
  var apptoken = "d5ju6zccf9kf3bdbi87gsj8yz8m";
  $.ajaxSetup({data: {apptoken: apptoken}});
  var rids = $("span.QBU_Childs").map(function() {
    return this.dataset.rid;
  }).get();
  var n= 100;
  var ridsChunked = _.chain(rids).groupBy(function(element, index){
    return Math.floor(index/n);
  }).toArray().value();
  _.each(ridsChunked, function(rids, index) {
    var query = "{22.EX." + rids.join("}OR{22.EX.") + "}"
    $.ajax({
      url: dbidChilds,
      data: {
        act: "API_DoQuery",
        clist: "3.22.173",
        query: query
      },
      dataFilter: XMLFlatToObj
    }).then(function(resp) {
      var childsData = _.groupBy(resp.records, function(items) {
        return items["related_lot"];
      });
      _.each(childsData, function(item, rid) {
        $("span.QBU_Childs[data-rid=" + rid + "]").html( _.pluck(item, "combined_defects").join("<span style='color:red;'> | </span>") );
      });
       
    });
  }); 
})();
Photo of Krissy

Krissy

  • 100 Points 100 badge 2x thumb
  • frustrated

Posted 1 year ago

  • 0
  • 1
Photo of Krissy

Krissy

  • 100 Points 100 badge 2x thumb
Oh and I'd also like the result to be sorted A-Z
Photo of Chuck Grigsby

Chuck Grigsby

  • 1,978 Points 1k badge 2x thumb
I see underscore in there so you can use that by using this. Just pass your array of words through this before sending to the concat field. I haven't used underscore a lot so Dan may chime in with a way better answer as always :P
http://underscorejs.org/#uniq

_.uniq(data);


Another examle
_.uniq([1, 2, 1, 4, 1, 3]); 
=> [1, 2, 4, 3]
For sorting you can then pass it through something like this
_.iteratee(value, [context]) 

(Edited)
Photo of Krissy

Krissy

  • 100 Points 100 badge 2x thumb
I basically copied the formula i'm using exactly as is.  Is it possible you could tell me exactly where exactly this new addition might go within my formula?  Sorry for being such a novice. :) 
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 26,352 Points 20k badge 2x thumb
Replace this block of code:
 _.each(childsData, function(item, rid) {
   $("span.QBU_Childs[data-rid=" + rid + "]").html( _.pluck(item, "combined_defects").join("<span style='color:red;'> | </span>") );
 });
with this block of code:
 _.each(childsData, function(item, rid) {
   var markup = _.chain(item)
     .pluck("combined_defects")
     .uniq()
     .value()
     .join("<span style='color:red;'> | </span>")
   $("span.QBU_Childs[data-rid=" + rid + "]").html(markup);
 });
Photo of Krissy

Krissy

  • 100 Points 100 badge 2x thumb
Worked great!  Thanks so much!!  One other question, no big deal if no easy solution for this but is there a way to get the data from that field to show up on an e-mailed report? For some reason that field comes over blank when I try to e-mail.  I added a formula text field and converted it to text but that didn't do the trick. 
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 26,352 Points 20k badge 2x thumb
These types of fields contain JavaScript and are are evaluated and displayed in the browser. The displayed values is not the saved value.