Need to split CDT elements

Certified Associate Developer

Hello All,

I'm creating an application where there is a requirement to update one of the field of a cdt with the same field of another cdt and store the output into third variable. My scenario for the said work is as below

Suppose my two cdt's are as below

cdt1{

amount,

claimID,

claimType

},

cdt2{

amount,

date,

claimID,

claimType,

owner

}

Now all i need is to search both the cdt on the basis of claimID and claimType and if any both the matching values found then replace the amount of cdt1 with the amount in cdt2 or else keep it as it is. To accomplish this function I wrote the below logic

 

in Parent Interface

local!var1:a!foreach(

items:local!cdt1,

expression:rule!secondrule(

cdt1:local!cdt1[fv!index],

cdt2:local!cdt2

)

)

 

and in the secondrule file, I wrote the below logic

a!foreach(

items:local!cdt2,

expression{

if(

and(

ri!cdt2[fv!index].claimType=ri!cdt1.claimType,

ri!cdt2[fv!index].claimID=ri!cdt1.claimID

),

type!cdt1(

amount:ri!cdt2[fv!index].amount,

claimID:ri!cdt1.claimID,

claimType:ri!cdt1.claimType

),

type!cdt2(

amount:ri!cdt1.amount,

claimID:ri!cdt1.claimID,

claimType:ri!cdt1.claimType

)

)

),

 

Now the problem that is happening here is related to the final cdt i'm getting. Suppose both cdt have 2 records. Out of which cdt1 has both the records different i.e combination of claimID and claimType is different but in cdt2 both the records are similar i.e claimID and claimType combination is same in both the records. do due to that i'm unfortunately getting the final cdt which has data like

 

[amount:500,claimID:10,ClaimType:2],[amount:600,claimID:20,ClaimType:3],

[amount:500,claimID:10,ClaimType:2]

Here the first two element is considered as a single item which is not acceptable. So is there a way where I can convert this elements into different array item and my totalcount would be 3 instead of 2? By that I can use union clause to remove the duplicate item. I hope my requirement is clear. Any suggestion here would be appreciated

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer
    Hello vivek,

    I haven't reviewed your whole logic(I might have some comments), but from your question, Have you tried flatten function?
    docs.appian.com/.../fnc_array_a_flatten.html

    Jose
  • Hopefully this is what you want, or at least will help get you to where you need to be. Note that the extra index(..., 1...) for local!matchesOnClaimIdAndClaimType isn't required if you have an "is null or empty" rule available to use in the if() towards the end of the rule.

    load(
      local!arrayOfCdt1: {
        {amount:100, claimId: 10, claimType: 1},
        {amount:100, claimId: 10, claimType: 1},
        {amount:200, claimId: 20, claimType: 2},
        {amount:700, claimId: 10, claimType: 2},
      },
      local!arrayOfCdt2: {
        {amount:110, claimId: 11, claimType: 1},
        {amount:110, claimId: 11, claimType: 1},
        {amount:210, claimId: 20, claimType: 1},
        {amount:710, claimId: 20, claimType: 2},
      },
      a!forEach(
        items: local!arrayOfCdt1,
        expression: with(
          local!matchesOnClaimId: index(
            local!arrayOfCdt2,
            wherecontains(
              fv!item.claimId,
              local!arrayOfCdt2.claimId
            ),
            {}
          ),
          local!matchesOnClaimIdAndClaimType: index(
            index(
              local!matchesOnClaimId,
              wherecontains(
                fv!item.claimType,
                local!matchesOnClaimId.claimType
              ),
              null
            ),
            1,
            null
          ),
          if(
            isnull(
              local!matchesOnClaimIdAndClaimType
            ),
            fv!item,
            local!matchesOnClaimIdAndClaimType
          )
        )
      )
    )

  • Why are you trying to store the exact same amount value in two different CDTs? There definitely can be use cases for this type of deonormalization, but it seems to me that if two rows have the same value that this should be stored via a foreign key relationship instead of saving the data twice. This can lead to data corruption in your application if they ever fall out of sync. Once this issue pops up, it can be very painful to resolve.
  • 0
    Certified Lead Developer

    Hi  as per your explanation, what I understood is, finally you are getting an array as the response which has 3 items in it where one of the item is duplicate. Which can be resolved by union() as you already have mention,

    But still I don't understand the meaning of "first two element is considered as a single item" why first 2 items will be considered as single item?

    Also as I can see, you have a duplicate item so why are you considering this duplicate item? I believe the totalCount should be 2 instead of 3 unless you want to retain the duplicate values as well.

  • 0
    Certified Associate Developer
    in reply to josep
    Thanks Josep,

    This helped me, This was my exact requirement. Thanks a ton Jose. Also thanks to everyone for putting your efforts and giving your precious times for the resolution of this issue. I think my requirement was not that cleared and that is why we got multiple resolutions but in any case all your posts are helpful may be not in this case but in some other one.