List of data using SuiteQl

Use “N/query” Module

    /**
       * Retrieves a list of records based on the provided record ID.
       * It constructs and executes a SuiteQL query to fetch records from the specified record type.
       * @param {string} recordId – The ID of the record type from which records are to be fetched.
       * @returns {Object[]} – An array containing records retrieved from the specified record type.
       */
    getListQueryResult(recordId) {
        try {
          if (recordId) {
            let suiteQL = `
            SELECT
              id,
              name
            FROM
              ${recordId}
            ORDER BY
              id`;
            // Run the SuiteQL query
            return query.runSuiteQL(suiteQL);
          }
          return [];
        } catch (e) {
          log.error({ title: “Error @getListQueryResult”, details: e });
          return [];
        }
      },


  	/**
         * Retrieves list data based on the provided query result object.
       * @memberof dataSets
       * @param {Object} queryResultObj – The query result object.
       * @returns {Object[]} An array containing the retrieved list data.
       */
      getListData(queryResultObj) {
        try {
          return queryResultObj.results.map((item) => {
            return {
              id: item.values[0],
              name: item.values[1],
            };
          });
        } catch (e) {
          log.error({ title: “Error @grw_021_cm_functions @dataSets @getListData”, details: e });
          return [];
        }
      },


	/**
       * Retrieves a list of data sets.
       *
       * @returns {Object} An object containing a list of data sets.
       *           Each property of the returned object corresponds to a specific data set,
       *           with keys representing the name of the data set and values containing the retrieved data.
       */
	getList() {
        try {
          let queries = [
            “customrecord_grw007_wrkeffstatustype”,
            “customrecord_grw007_wrkeffapprstattype”,
            “customrecord_grw007_assetcategory”,
          ];
          let results = queries.map((queryName) => dataSets.getListData(dataModel.getResults.getListQueryResult(queryName)));
          let listObj = {
            list: [
              { workEffortStatusTypes: results[0] },
              { approvalStatusTypes: results[1] },
              { assetCategory: results[2] },
            ],
          };
          // log.debug(‘listObj’, listObj);
          return listObj;
        } catch (e) {
          log.error({ title: “Error @grw_021_cm_functions @dataSets @getList”, details: e });
          return [];
        }
      },
let list = getList();

Leave a comment

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