Promotion Details in to the item record

To get the promotion details upon creation and edit of the promotion and save the details of the promotion on the corresponding item record.

A user event script that runs after a record is submitted. It handles both the creation/editing and deletion of promotion records, updating related item records accordingly.

Key Points:

  1. Script Definition:
  • The script is defined with @NApiVersion 2.x and @NScriptType UserEventScript.
  • It uses the define function to import record, search, and log modules.
  1. afterSubmit Function:
  • This function is executed after a record is submitted.
  1. Logging Context Type:
  • Logs the type of context (CREATE, EDIT, DELETE).
  1. Handling Deletion:
  • If the context type is DELETE, it retrieves the promotion record and its details.
  • It collects all items associated with the promotion.
  • For each item, it loads the item record, checks if the promotion is applied, and clears the promotion records id matches current oromotion added to each item.
  1. Handling Creation/Editing:
  • If the context type is CREATE or EDIT, it retrieves the promotion record and its details.
  • It collects all items associated with the promotion.
  • For each item, it loads the item record, checks if the promotion should be applied based on various conditions, and updates the item record with the promotion details if necessary.

 

created a new tab in the item record named ‘promotion’ and added the custom ite fields for promotion start date , end date, promotion rate, name and type, and checkbox field specifying whether the item has the promotion or not

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */
define(['N/record', 'N/search', 'N/log'], function(record, search, log) {
    function afterSubmit(context) {
    log.error('context.type', context.type);

    if (context.type !== context.UserEventType.CREATE && context.type !== context.UserEventType.EDIT) {
      if (context.type === context.UserEventType.DELETE) {
        var promoRecord = context.newRecord;
        var promoId = promoRecord.getValue('id');
        var lineItemCount = promoRecord.getLineCount('discounteditems');
        var itemsHavingPromotion = [];
        for (var i = 0; i < lineItemCount; i++) {
          var itemId = promoRecord.getSublistValue({
            sublistId: 'discounteditems',
            fieldId: 'discounteditem',
            line: i
          });
          itemsHavingPromotion.push(itemId);
        }

        itemsHavingPromotion.forEach(function (itemId) {
          try {
            var itemType = search.lookupFields({
              type: 'item',
              id: itemId,
              columns: ['type']
            });
            var type = itemType.type[0].value;
            var typeOfItem = getTypeOfItem(type);

            if (!!typeOfItem) {
              var itemRecord = record.load({
                type: typeOfItem,
                id: itemId,
                isDynamic: true
              });

              var isPromotionApplied = itemRecord.getValue('custitem_promotionapplied');
              var promoinItem = itemRecord.getValue('custitem_promotionid');

              if (isPromotionApplied && (promoId === promoinItem)) {
                clearPromotionDetails(itemRecord);
                itemRecord.save({
                  enableSourcing: false,
                  ignoreMandatoryFields: true
                });
              }
            }
          } catch (e) {
            log.error('Error in afterSubmit', e.message);
          }
        });
      }
      return;
    }

    try {
      var promoRecord = context.newRecord;
      var applyDiscountTo = promoRecord.getValue('applydiscountto');
      var isInactive = promoRecord.isinactive ? promoRecord.getValue('isinactive') : false;
      var canBeAutoApplied = promoRecord.getValue('canbeautoapplied');
      var audience = promoRecord.getValue('audience');
      var minimumOrderAmountCheck = promoRecord.getValue('minimumorderamountcheck');
      var startDate = promoRecord.getValue('startdate');
      var endDate = promoRecord.getValue('enddate');
      var discountType = promoRecord.getValue('discounttype');
      var rate = promoRecord.getValue('rate');
      var lineItemCount = promoRecord.getLineCount('discounteditems');
      var promoId = promoRecord.getValue('id');
      var itemsHavingPromotion = [];

      for (var i = 0; i < lineItemCount; i++) {
        var itemId = promoRecord.getSublistValue({
          sublistId: 'discounteditems',
          fieldId: 'discounteditem',
          line: i
        });
        itemsHavingPromotion.push(itemId);
      }

      itemsHavingPromotion.forEach(function (itemId) {
        try {
          var itemType = search.lookupFields({
            type: 'item',
            id: itemId,
            columns: ['type']
          });
          var type = itemType.type[0].value;
          var typeOfItem = getTypeOfItem(type);

          if (!!typeOfItem) {
            var itemRecord = record.load({
              type: typeOfItem,
              id: itemId,
              isDynamic: true
            });

            var isPromotionApplied = itemRecord.getValue('custitem_promotionapplied');
            var promoinItem = itemRecord.getValue('custitem_promotionid');
            if (
                applyDiscountTo === 'ALLSALES' &&
                isInactive === false &&
                canBeAutoApplied === true &&
                audience === 'EVERYONE' &&
                minimumOrderAmountCheck === false
            ) {
            if (!isPromotionApplied || (promoId === promoinItem)) {
              applyPromotionDetails(itemRecord, promoId, startDate, endDate, discountType, rate);
            } 
            }else if (promoId === promoinItem) {
              clearPromotionDetails(itemRecord);
            }
            itemRecord.save({
              enableSourcing: false,
              ignoreMandatoryFields: true
            });
            log.error('Item Updated', 'Item ID: ' + itemId + ' updated with promotion details.');
          }
        } catch (itemError) {
          log.error('Error Updating Item', 'Item ID: ' + itemId + ', Error: ' + itemError.message);
        }
      });
    } catch (e) {
      log.error('Error in afterSubmit', e.message);
    }
  }
  function getTypeOfItem(type) {
    if (type === "InvtPart") {
      return "inventoryitem";
    } else if (type === "Assembly") {
      return "assemblyitem";
    } else if (type === "Kit") {
      return "kititem";
    } else {
      return null;
    }
  }
  function applyPromotionDetails(itemRecord, promoId, startDate, endDate, discountType, rate) {
    itemRecord.setValue({
      fieldId: 'custitem_promotionapplied',
      value: true
    });
    itemRecord.setValue({
      fieldId: 'custitem_promotionid',
      value: promoId
    });
    itemRecord.setValue({
      fieldId: 'custitem_promostartdate',
      value: startDate
    });
    itemRecord.setValue({
      fieldId: 'custitem_promoenddate',
      value: endDate
    });
    itemRecord.setValue({
      fieldId: 'custitem_promotiontype',
      value: discountType
    });
    itemRecord.setValue({
      fieldId: 'custitem_promotionrate',
      value: rate
    });
  }
  function clearPromotionDetails(itemRecord) {
    itemRecord.setValue({
      fieldId: 'custitem_promotionapplied',
      value: false
    });
    itemRecord.setValue('custitem_promotionid', '');
    itemRecord.setValue('custitem_promostartdate', '');
    itemRecord.setValue('custitem_promoenddate', '');
    itemRecord.setValue('custitem_promotiontype', '');
    itemRecord.setValue('custitem_promotionrate', '');
  }
  return {
    afterSubmit: afterSubmit
  };
});

Leave a comment

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