Created Date of corresponding sales order

Here’s how you can use search.lookupFields in SuiteScript 2.0 to retrieve the created date of a Sales Order associated with a given Invoice.

define([‘N/search’], function(search) {

  function getSalesOrderCreatedDate(invoiceId) {

    try {

      // Lookup the ‘createdfrom’ field of the Invoice

      var invoiceData = search.lookupFields({

        type: search.Type.INVOICE,

        id: invoiceId,

        columns: [‘createdfrom’]

      });

      var salesOrderId = invoiceData.createdfrom[0] ? invoiceData.createdfrom[0].value : null;

      if (!salesOrderId) {

        return ‘No Sales Order linked to this Invoice.’;

      }

      // Lookup the created date of the Sales Order

      var salesOrderData = search.lookupFields({

        type: search.Type.SALES_ORDER,

        id: salesOrderId,

        columns: [‘datecreated’]

      });

      return salesOrderData.datecreated || ‘Created Date not found.’;

    } catch (error) {

      return ‘Error retrieving data: ‘ + error.message;

    }

  }

  return {

    getSalesOrderCreatedDate: getSalesOrderCreatedDate

  };

});

Explanation:

  1. Step 1: Retrieves the createdfrom field from the Invoice, which contains the associated Sales Order ID.
  2. Step 2: Uses the retrieved Sales Order ID to look up the datecreated field from the Sales Order.
  3. Step 3: Returns the Sales Order’s created date.

Leave a comment

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