Skip to main content
chrisg0006
June 18, 2024
Solved

a!foreach() does not evaluate a local variable

  • June 18, 2024
  • 3 replies
  • 0 views

My goal is to produce an excel file from a set of Clergy Pension records.  My plan to do this is:

1.  Create an CDT to hold the data for the excel spreadsheet

2. Create an Expression Rule to create the data for the CDT

3. Use the Expression Rule in a Process Model to populate the CDT

4. Use the Export Data Store Entity to Excel smart service in the Process Model

My problem is with the Expression Rule,  In the Expression Rule, I create two local variables; data and report.  The local variable holds the results of the query and the local variable report is formatted data for storage into the CDT.

To create the local!data, I use a!queryRecordType() to pull the records I need.  The data comes from several tables and I use the Relationships from the main table of the query, CPMA Clergy Pension.  I filter and sort the data.

Here is the code for local!data.

local!data: a!queryRecordType(
    recordType: 'recordType!CPMA Clergy Pension',
    fields: {
      'recordType!CPMA Clergy Pension.fields.clergyId',
      'recordType!CPMA Clergy Pension.relationships.clergyMember.fields.clergyssnlast4',
      'recordType!CPMA Clergy Pension.relationships.clergyMember.fields.clergylastname',
      'recordType!CPMA Clergy Pension.relationships.clergyMember.fields.clergyfirstname',
      'recordType!CPMA Clergy Pension.relationships.clergyMember.fields.clergymiddlename',
      'recordType!CPMA Clergy Pension.relationships.clergyMember.relationships.clergyStatus.relationships.refStatustype.fields.status',
      'recordType!CPMA Clergy Pension.relationships.clergyMember.fields.clergydob',
      'recordType!CPMA Clergy Pension.relationships.clergyMember.fields.clergyhire',
      'recordType!CPMA Clergy Pension.fields.participationDate',
      'recordType!CPMA Clergy Pension.relationships.clergyMember.relationships.refGender.fields.gender',
      'recordType!CPMA Clergy Pension.relationships.refPensionStatus.fields.pensionStatus',
      'recordType!CPMA Clergy Pension.fields.pensionStatusEffectiveDate',
      'recordType!CPMA Clergy Pension.fields.comment'
    },
    filters: {
      a!queryFilter(
        field: 'recordType!CPMA Clergy Pension.fields.pensionStatusId',
        operator: "in",
        value: { 1, 3, 4 }
      )
    },
    pagingInfo: a!pagingInfo(
      startIndex: 1,
      batchSize: 5000,
      sort: a!sortInfo(
        field: 'recordType!CPMA Clergy Pension.relationships.clergyMember.fields.clergylastname',
        ascending: true
      )
    )
  ),
  local!data

The output from this query looks like this.

In the local!report code, my objective is to take the Map output from the first local variable and cast it to the CDT I need for the excel export.  I use a!foreach() to cast the data.  The code for this is:

local!Report: a!forEach(
    items: local!data,
    expression: 
    cast(
      'type!{urn:com:appian:types:CPMA}CPMA_ClergyPensionContributor',
      {
        rowNumber: fv!index,
        cleryId: fv!item.clergyId,
        clergySSN:concat(
          "***-**-",
          fv!item.clergyssnlast4),
        clergyLastName: fv!item.clergylastname,
        clergyFirstName: fv!item.firstname,
        clergyFullName: concat(
          fv!item.clergylastname,
          ", ",
          fv!item.clergyfirstname,
          " ",
          if(
            a!isNotNullOrEmpty(fv!item.clergymiddlename),
            charat(fv!item.clergymiddlename, 1),
            null
          )
        ),
        clergyMiddleName: fv!item.clergymiddlename,
        cleryStatus: fv!item.status,
        clergyGender: fv!item.gender,
        clergyDoBirth: fv!item.clergydob,
        clergyAge: if(
          a!isNotNullOrEmpty(fv!item.clergydob),
          rounddown((fv!item.clergydob - today()) / 365.24, 0),
          null
        ),
        clergyDoHire: fv!item.clergyhire,
        clergyYearsofService: if(
          a!isNotNullOrEmpty(fv!item.clergyhire),
          rounddown((fv!item.clergyhire - today()) / 365.24, 0),
          null
        ),
        clergyDoParticipation: fv!item.participationDate,
        clergyDoRetire: if(
          a!isNullOrEmpty(fv!item.clergydob),
          null,
          if(
            month(fv!item.clergydob) = 12,
            date(year(fv!item.clergydob) + 72, 1, 1),
            date(
              year(fv!item.clergydob) + 71,
              month(fv!item.clergydob) + 1,
              1
            )
          )
        ),
        clergyPensionStatus: fv!item.pensionStatus,
        pensionStatusDate: fv!item.pensionStatusEffectiveDate,
        comment: fv!item.comment,
        
      }
    )
  ),
  local!Report

However, when I put the two together, I get one record with all null values cast as the CDT.  The a!foreach() function does not seem to process the initial local variable.

What am I doing wrong?

    Best answer by yogip3177

    You need to use local!data.data to get query data. See below screenshot:

    local!data is a map of the result (totalCount, batchSize, data, etc.). To get data from this map, you need to use local!data.data

    3 replies

    yogip3177Answer
    June 18, 2024

    You need to use local!data.data to get query data. See below screenshot:

    local!data is a map of the result (totalCount, batchSize, data, etc.). To get data from this map, you need to use local!data.data

    konduruc733182
    June 18, 2024

    Your local!data is a map which contains multiple keys, you would need to index the "data" before you run it in the a!forEach loop. As Yogi mentioned, use local!data.data in the a!forEach items parameter.

    chrisg0006
    June 18, 2024

    Thanks to Yogi and Konduru!!  After I fixed this I had to do one more thing to get this to work.  I had to change my references in the a!foreach expression to reference the record type for each of fields.  I had to change fv!item.clergyName to fv!item(RecordType!CPMA Clergy Pension.relationship.clergyMember,fields.clergyName.

    Once I had all of that corrected, the expression rule worked.  Thank you agin to Yogi and Konduru!!