Currently , I am calling integration for each row separately,But I want optimize the code, Is there any way I can call integration just once and populate different row as per doc Id

Certified Associate Developer

a!localVariables(
  local!transactionCDM: index(
    index(
      rule!NW_Transaction(ri!transactionId),
      "result",
      {}
    ),
    "body",
    {}
  ),
  local!documentDetails: index(
    local!transactionCDM,
    "supporting_documents",
    {}
  ),
  local!selectedDocument,
  {
    if(
      a!isNotNullOrEmpty(local!selectedDocument),
      {
        /*{rule!NW_verticalSpacer(n: 4),*/
        rule!NW_UpdateDocumentDetails(
          selectedDocument: local!selectedDocument,
          transactionCDM: local!transactionCDM,
          showWhen: a!isNotNullOrEmpty(local!selectedDocument),
          transactionId: ri!transactionId
        )
      },
      a!cardLayout(
        contents: {
          a!recordActionField(
            actions: a!recordActionItem(
              action: 'recordType!{fff94020-d948-4267-8934-8f9a465308bd}NW Transaction.actions.{ba411847-1b0f-4a59-a70f-72a877d724f0}uploadDocument',
              identifier: ri!transactionId
            ),
            align: "END"
          ),
          a!gridField(
            data: local!documentDetails,
            columns: {
              a!gridColumn(
                label: "Document Form Name",
                value: a!richTextDisplayField(
                  value: a!richTextItem(
                    text: a!defaultValue(
                      value: index(fv!row, "form_name", {}),
                      default: cons!NW_NULL_PLACEHOLDER_VALUE
                    ),
                    link: a!safeLink(
                      uri: index(
                        index(
                          index(
                            rule!NW_GetDocumentDetails(docId: index(fv!row, "document_id", {})),
                            "result",
                            ""
                          ),
                          "body",
                          ""
                        ),
                        "document_url",
                        {}
                      ),
                      openLinkIn: "NEW_TAB"
                    ),
                    linkStyle: "STANDALONE"
                  )
                ),
                align: "START"
              ),
              a!gridColumn(
                label: "Origination",
                value: a!richTextDisplayField(
                  value: a!richTextItem(
                    text: a!defaultValue(
                      value: index(
                        index(
                          index(
                            rule!NW_GetDocumentDetails(docId: index(fv!row, "document_id", {})),
                            "result",
                            ""
                          ),
                          "body",
                          ""
                        ),
                        "origination",
                        {}
                      ),
                      default: cons!NW_NULL_PLACEHOLDER_VALUE
                    )
                  )
                ),
                align: "START"
              ),
              a!gridColumn(
                label: "Created On",
                sortField: "created_date",
                value: a!richTextDisplayField(
                  value: a!richTextItem(
                    text: a!defaultValue(
                      value: index(fv!row, "document_date", {}),
                      default: cons!NW_NULL_PLACEHOLDER_VALUE
                    )
                  )
                ),
                align: "START"
              ),
              a!gridColumn(
                label: "External Contract Number",
                value: a!richTextDisplayField(
                  value: a!richTextItem(
                    text: a!defaultValue(
                      value: index(fv!row, "external_contract_number", {}),
                      default: cons!NW_NULL_PLACEHOLDER_VALUE
                    )
                  )
                ),
                align: "START"
              ),
              a!gridColumn(
                label: "Status",
                value: a!richTextDisplayField(
                  value: a!richTextItem(
                    text: a!defaultValue(
                      value: index(fv!row, "document_state", {}),
                      default: cons!NW_NULL_PLACEHOLDER_VALUE
                    )
                  )
                ),
                align: "START"
              ),
              a!gridColumn(
                label: "Updated On",
                sortField: "modified_date",
                value: a!richTextDisplayField(
                  value: a!richTextItem(
                    text: a!defaultValue(
                      value: todate(
                        index(
                          index(
                            index(
                              rule!NW_GetDocumentDetails(docId: index(fv!row, "document_id", {})),
                              "result",
                              ""
                            ),
                            "body",
                            ""
                          ),
                          "modified_date",
                          {}
                        )
                      ),
                      default: cons!NW_NULL_PLACEHOLDER_VALUE
                    )
                  )
                ),
                align: "START"
              ),
              a!gridColumn(
                label: "Tracking Number",
                value: a!richTextDisplayField(
                  value: a!richTextItem(
                    text: a!defaultValue(
                      value: index(
                        index(
                          index(
                            rule!NW_GetDocumentDetails(docId: index(fv!row, "document_id", {})),
                            "result",
                            ""
                          ),
                          "body",
                          ""
                        ),
                        "tracking_number",
                        {}
                      ),
                      default: cons!NW_NULL_PLACEHOLDER_VALUE
                    )
                  )
                )
              ),
              a!gridColumn(
                value: a!richTextDisplayField(
                  value: {
                    a!richTextIcon(
                      icon: "dot-circle-o",
                      caption: "Update Document",
                      link: a!dynamicLink(
                        value: fv!row,
                        saveInto: local!selectedDocument
                      ),
                      linkStyle: "STANDALONE",
                      color: "POSITIVE"
                    )
                  },
                  showWhen: a!isNotNullOrEmpty(local!documentDetails)
                )
              )
            },
            pageSize: a!defaultValue(
              ri!pageSize,
              cons!CMGT_INT_BATCH_SIZE_SMALL
            )
          )
        },
        style: "NONE",
        shape: "ROUNDED",
        padding: "STANDARD",
        showBorder: false,
        showShadow: true,
        decorativeBarPosition: "TOP",
        decorativeBarColor: cons!NW_COLOR_PRIMARY
      )
    )
  }
)

Here , this integration  "rule!NW_GetDocumentDetails(docId: index(fv!row, "document_id", {}))", is called separately for different row , but I want to optimize it, please help regarding this scenario

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    Old Answer: Fetch all document details once using a!forEach and store in local!allDocumentDetails.
    Access data using local!allDocumentDetails[fv!index] instead of calling the integration.

    New Answer: Only way to truly improve performance is to modify the integration to accept array of Ids.

    /* Instead of N calls, make ONE call */
    local!allDocumentDetails: rule!NW_GetDocumentDetailsBatch(
      docIds: index(local!documentDetails, "document_id", {})
    )

  • 0
    Certified Lead Developer
    in reply to Shubham Aware

    Calling an integration in a foreach does not improve the code performance in any way. You will have to find a way to fetch the data for all documents in a single call.

  • 0
    Certified Lead Developer
    in reply to Stefan Helzle

    I agree with you. I validated the approach and it showed slight improvement, but nothing major

  • 0
    Certified Lead Developer

    The high-level answer: Query once with all id's (as Shubham suggested) and then use type-safe index/wherecontains to reference that data. This is a common enough pattern it feels like there should be an Appian function for it. I've seen this rule in multiple forms dating back to my first ever project, so it's pretty common.

    Name: COM_findData
    ri!data: Any (DSE query result, dictionary, record query result, etc)
    ri!whereField: Any (Text or Record Field - single input value)
    ri!equalsValue: Any (can be an array, but if it is an array every element should be the same type)
    ri!select: Any (Text or Record Field - single input value)
    index(
      index(
        ri!data,
        wherecontains(
          ri!equalsValue,
          cast(
            runtimetypeof({ ri!equalsValue }),
            index(ri!data, ri!whereField, {})
          )
        ),
        {}
      ),
      a!defaultValue(ri!select, {}),
      {}
    )

    If you have `local!allDocumentDetails`, and it only contains a list of document details (i.e. not 'result' or 'body') you can use the rule as follows:

    uri: rule!COM_findData(
        data: local!allDocumentDetails,
        whereField: "docId",
        equalsValue: toninteger(index(fv!row, "document_id", {})) /* tointeger() isn't needed when using record notation */,
        select: "document_url"
    ),