Combined Saved search as a table with email body

In the script, I utilized a loaded saved search to generate the email body table, not as an attachment. Any modifications to the saved search will not impact the table contents within the email body.If any updates are necessary for the saved search, using this method eliminates the need to modify the script.

    function combined() {

      // Load the first search

      let firstSearch = search.load({

        id:

      });

      let thirdSearch = search.load({

        id:

      });

      // Get the column headings (field names) from the search

      let columnHeadings = thirdSearch.columns.map(function (column) {

        return column.name;

      });

      // Iterate through columnHeadings to get custom labels for all fields

      let headingArray = [];

      for (let i = 0; i < columnHeadings.length; i++) {

        // Get the custom label for the current field

        let fieldLabel = thirdSearch.columns[i].label;

        headingArray.push(fieldLabel)

      }

      // Convert headingArray to an HTML table

      let tableHTML = “<table style=’border: 1px solid black; border-collapse: collapse;’><tr>”;

      for (let i = 0; i < headingArray.length; i++) {

        tableHTML += “<th style=’border: 1px solid black;’>” + headingArray[i] + “</th>”;

      }

      tableHTML += “</tr>”;

      // Execute the first search and get the entire result

      firstSearch.run().each(function (result) {

        let firstResult = [];

        // Push the entire result object to the array

        for (let i = 0; i < result.columns.length; i++) {

          // Log each column’s value in order

          log.debug(result.getValue(result.columns[i]));

          firstResult.push(result.getValue(result.columns[i]));

        }

        firstResult.splice(1, 0, );

        tableHTML += “<tr style=’color:red; font-size: larger;’>”;

        for (let i = 0; i < firstResult.length; i++) {

          tableHTML += “<td style=’border: 1px solid black;’>” + firstResult[i] + “</td>”;

        }

        tableHTML += “</tr>”;

        return true;

      });

      let secondSearch = search.load({

        id:

      });

      secondSearch.run().each(function (result) {

        let secondResult = [];

        for (let i = 0; i < result.columns.length; i++) {

          secondResult.push(result.getValue(result.columns[i]));

        }

        secondResult.splice(1, 0, );

        tableHTML += “<tr style=’color:red; font-size: larger;’>”;

        for (let i = 0; i < secondResult.length; i++) {

          tableHTML += “<td style=’border: 1px solid black;’>” + secondResult[i] + “</td>”;

        }

        tableHTML += “</tr>”;

        return true;

      });

      thirdSearch.run().each(function (result) {

        let thirdResult = [];

        for (let i = 0; i < result.columns.length; i++) {

          log.debug(result.getValue(result.columns[i]));

          thirdResult.push(result.getValue(result.columns[i]));

        }

        tableHTML += “<tr style=’color:red; font-size: larger;’>”;

        for (let i = 0; i < thirdResult.length; i++) {

          tableHTML += “<td style=’border: 1px solid black;’>” + thirdResult[i] + “</td>”;

        }

        tableHTML += “</tr>”;

        return true;

      });

      tableHTML += “</table>”;

      return tableHTML;

    }

Leave a comment

Your email address will not be published. Required fields are marked *