Comparing two arrays

Hi,

I have a requirement of displaying an Audit Trail comparing two arrays; Original and updated as follows:

Original Array:

[Id=15, PS_Username=VGAN, PS_Fullname=Veron Ganguly], [Id=18, PS_Username=CZUCKERT, PS_Fullname=Curt Zuckert], [Id=28, PS_Username=KDUENNHA, PS_Fullname=Karen Duennhaupt], [Id=31, PS_Username=LSWENSON, PS_Fullname=Liz Swenson], [Id=38, PS_Username=POTENKHI, PS_Fullname=Tatiana Potekhina], [Id=40, PS_Username=TCHAN, PS_Fullname=Tiffany Chan], [Id=41, PS_Username=TSTAPLES, PS_Fullname=Tina Staples], [Id=95, PS_Username=MONGEL, PS_Fullname=Geller], [Id=96, PS_Username=CBI, PS_Fullname=Chand]

Updated Array:

[Id=96, PS_Username=CBI, PS_Fullname=Chandler Bing]

So the audit trail should display:

PS_Fullname: Original value: Chand; Updated Value: Chandler Bing

TIA :)

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    The way I'd implement this would vary depending on particular requirement details that you haven't provided yet as far as I can see.  Just two remaining questions I have, for example:

    1) Will the "updated array" being compared really just be one item from the original array, was that just for illustration purposes and the real comparison would be against the full updated array of CDTs?

    2) Will the comparison always assume that one array member is updated or will it need to handle multiple updated members?

  • This may help..

    load(
      local!originalArray: {
        {
          Id: 15,
          PS_Username: "VGAN",
          PS_Fullname: "Veron Ganguly"
        },
        {
          Id: 18,
          PS_Username: "CZUCKERT",
          PS_Fullname: "Curt Zuckert"
        },
        {
          Id: 28,
          PS_Username: "KDUENNHA",
          PS_Fullname: "Karen Duennhaupt"
        },
        {
          Id: 31,
          PS_Username: "LSWENSON",
          PS_Fullname: "Liz Swenson"
        },
        {
          Id: 38,
          PS_Username: "POTENKHI",
          PS_Fullname: "Tatiana Potekhina"
        },
        {
          Id: 40,
          PS_Username: "TCHAN",
          PS_Fullname: "Tiffany Chan"
        },
        {
          Id: 41,
          PS_Username: "TSTAPLES",
          PS_Fullname: "Tina Staples"
        },
        {
          Id: 95,
          PS_Username: "MONGEL",
          PS_Fullname: "Geller"
        },
        {
          Id: 96,
          PS_Username: "CBI",
          PS_Fullname: "Chand"
        }
      },
      local!updatedArray: {
        {
          Id: 96,
          PS_Username: "CBI",
          PS_Fullname: "Chandler Bing"
        }
      },
      a!forEach(
        items: local!updatedArray,
        expression: if(
          displayvalue(
            fv!item.Id,
            local!originalArray.Id,
            local!originalArray.PS_Fullname,
            null
          ) = fv!item.PS_Fullname,
          {},
          {
            field: "PS_Fullname",
            oldValue: displayvalue(
              fv!item.Id,
              local!originalArray.Id,
              local!originalArray.PS_Fullname,
              null
            ),
            newValue: fv!item.PS_Fullname
          }
        )
      )
    )

  • You need to dynamically get the list of the cdt fields and compare the value of each field.

    I have written a sample code. This might help. You can improvise this to make it more efficient.

    load(
      local!originalCdt: 'type!{http://www.telstra.com.au/PDSA/}PD_PAYMENT_CLAIM'(  
      ),
      local!cdtFileds:split(cast(103,local!originalCdt),"="),
      local!cleanedFields:stripwith(stripwith(stripwith(local!cdtFileds,"]"),"["),", "), /*Clean the fields using regex or other efficient functions*/
      
      local!changedArray: symmetricdifference(ri!updatedCdt,ri!originalCdt),
      
      local!originalArrayChanged:index(ri!originalCdt, wherecontains(local!changedArray,ri!originalCdt),{}),
      local!updatedArrayChanged: difference(ri!updatedCdt,ri!originalCdt),
      
      
      
     a!forEach(
       local!cleanedFields,
       if(contains(index(local!updatedArrayChanged,fv!item,{}),index(local!originalArrayChanged,fv!item,{})),
         {},
         {
           field:fv!item,
           originalValue: index(local!originalArrayChanged,fv!item,{}),
           updatedValue: index(local!updatedArrayChanged,fv!item,{})
         }
       )
     )
      
    )