Function difference() but for specific fields?

A Score Level 2

I have a requirement that needs me to find the difference between two arrays of CDTs on a field level rather than CDT level. In other words something similar to the function difference() but it compares a particular field rather than all the fields to return the difference.

Example:

array1: { (id: 1, name: bob), (id: 2, name: bill), (id: 3, name: mary) }

array2: { (id: 1, name: joe), (id: 2, name: john), (id: 4, name: mary) }

Difference only in field id returns { (id: 3, name: mary) } because array2 does not contain a CDT with field id: 3

I tried difference(local!array1.id, local!array2.id) but obviously that didn't quite work. Any help is greatly appreciated!

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    You could try creating a couple of new arrays out of the field you want, e.g. local!names1: property(local!array1, "name") *index() does the same thing*

    local!names2: property(local!array2, "name")

    Check to make sure those are arrays of what you want, e.g. {"bob","bill","mary"} and {"joe", "john", "mary")

    Then another local variable which just stores the difference of only the field, local!nameDiff: difference(local!names1, local!names2)

    Hopefully, by stripping the field out of the CDT and diffing it by itself, you can avoid confusing the difference function.  But then you have to index both of the CDT lists from your diff list to get all the associated data back, unless you were only going to use the diffed property.  If that was the only data you're going to use you're done, otherwise the last step is needed.

    Yet another local variable, local!finalArray: union( index(local!array1, whereContains(local!nameDiff, local!array1.name), index(local!array2, whereContains(local!nameDiff, local!array2.name) )

    *in short all the stuff from array 1 that matches union all the stuff from array 2 that matches

  • 0
    A Score Level 2
    in reply to Dave Lewis

    I was able to implement this approach and got it working as expected. Thanks David!

Reply Children
No Data