Nested for each logic for base record and it's relationships

Certified Associate Developer

Hi ,

I have one base record in that one i'm forming three relationship two one-to-many and one many-to-one.

now i want to typecast data fields from base records and related records in one CDT.

can anyone please explain how i can achieve this using for each as i want to cast data for each id of base record.

  Discussion posts and replies are publicly visible

Parents
  • Hi ,

    That can be done, I'll add a sample code for your reference.
    Let me know if that helps you.

    a!forEach(
      items: local!baseRecords,
      expression: a!localVariables(
        /* Fetch related records for the current base record */
        local!relatedOnes: a!queryEntity(
          entity: cons!RelatedOne_ENTITY,
          query: a!query(
            condition: a!queryCondition(
              field: "baseId", // Foreign key field in related entity
              operator: "=",
              value: fv!item.id
            )
          )
        ).data,
    
        local!relatedTwos: a!queryEntity(...), // Similar to above
        local!relatedManies: a!queryEntity(...), // Adjust for many-to-many
    
        /* Map data to the combined CDT */
        type!BaseWithRelatedCDT(
          baseField1: fv!item.field1,
          baseField2: fv!item.field2,
          relatedOnes: a!forEach(
            items: local!relatedOnes,
            expression: type!RelatedOneCDT(
              relatedFieldA: fv!item.fieldA,
              ...
            )
          ),
          relatedTwos: a!forEach(
            items: local!relatedTwos,
            expression: type!RelatedTwoCDT(...)
          ),
          relatedManies: a!forEach(
            items: local!relatedManies,
            expression: type!RelatedManyCDT(...)
          )
        )
      )
    )

Reply
  • Hi ,

    That can be done, I'll add a sample code for your reference.
    Let me know if that helps you.

    a!forEach(
      items: local!baseRecords,
      expression: a!localVariables(
        /* Fetch related records for the current base record */
        local!relatedOnes: a!queryEntity(
          entity: cons!RelatedOne_ENTITY,
          query: a!query(
            condition: a!queryCondition(
              field: "baseId", // Foreign key field in related entity
              operator: "=",
              value: fv!item.id
            )
          )
        ).data,
    
        local!relatedTwos: a!queryEntity(...), // Similar to above
        local!relatedManies: a!queryEntity(...), // Adjust for many-to-many
    
        /* Map data to the combined CDT */
        type!BaseWithRelatedCDT(
          baseField1: fv!item.field1,
          baseField2: fv!item.field2,
          relatedOnes: a!forEach(
            items: local!relatedOnes,
            expression: type!RelatedOneCDT(
              relatedFieldA: fv!item.fieldA,
              ...
            )
          ),
          relatedTwos: a!forEach(
            items: local!relatedTwos,
            expression: type!RelatedTwoCDT(...)
          ),
          relatedManies: a!forEach(
            items: local!relatedManies,
            expression: type!RelatedManyCDT(...)
          )
        )
      )
    )

Children
No Data