Rule for creating list of CDTs based on input list of CDTs

I'm trying to populate one list of CDTs base on another input list of CDTs. My process has a step in which it stores records in WORKLOG table and retrieves primary keys of those records using process variable which is array of worklog CDTs.

I would like to use this pv!worklog as input into script task which will prepare another array of CDTs called pv!relation which then will be saved to database.

It looks like I need expression rule which will user pv!worklog and pv!employeeId as inputs and create array of relation CDTs by the rule "for each worklog entry create relation with input employee Id which is always single integer".

That array of relation CDTs should be saved into process variable pv!relation.

My attempt looks like this:

1) rule inputs:

2) rule code:

a!localVariables(
  local!worklog: ri!worklog
),
a!forEach(
  items: local!worklog,
  expression: append(
    ri!relation,
    'type!{urn:com:mcom:eedm}EEDM_Relation'(
      "EMPLOYEE",
      ri!employeeId,
      "WORKLOG ENTRY",
      fv!item.id,
      "WORK DONE"
    )
  )
)

My test input would be:

worklog entries:

{{1,"2020-01-01","Testing",3},{2,"2020-02-01","Testing",5}}

employeeId: 

3

Also, please give me advice how to map this rule input ri!relation (array of CDTs) to process variable outside of script task which will use this rule I'm trying to make work.

  Discussion posts and replies are publicly visible

  • +1
    Certified Lead Developer

    Just real quick, your code sample is wrong if you are expecting to use local!worklog.  The code subsequent to the local variable definition (in this case, your a!forEach) call should be inside the a!localVariables() call, not after it like it's currently shown.

  • Firstly, if the rule you've expressed above simply needs to return a list of type 'relation' you do not need to create a rule input for it. The rule's purpose should be to take the list of type' worklog' and the 'employeeId' and return a list of type 'relation'.

    Assuming the above to be true, then your code should be:

    a!forEach(
      items: ri!worklog,
      expression: 
        'type!{urn:com:mcom:eedm}EEDM_Relation'(
          "EMPLOYEE",
          ri!employeeId,
          "WORKLOG ENTRY",
          fv!item.id,
          "WORK DONE"
        )
    )

    Note: because I can't see the attribute names that make up the 'type!{urn:com:mcom:eedm}EEDM_Relation' I can't write the code as it should be, but best practice is to use name/value pairs e.g. if the first attribute of the 'relation' type is named 'role' then the first line of your type construction should be:

    role: "EMPLOYEE"

    (the method you're using will work, but is fragile to changes in the order of the CDT attributes, so always explicitly name the attributes in the way described above!!!