On a VB I am setting the value of a body field via...
# suitescript
n
On a VB I am setting the value of a body field via script. I added a checkbox to "override" the value (manually input a value instead). Problem is it seems like my afterSubmit is erasing the value of the field being put manually into the field. The script is not setting any value as verified with logs... is it because I have to put a return false/true?
b
share the code
n
Copy code
/*******************************************************************
 *
 *
 * Name: JAM_SUE_billRelatedPO.js
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * Version: 0.0.1
 *
 *
 * Author: Nicolas Bean
 * Purpose: Populates the "related purchase order" field
 * Script: The script record id
 * Deploy: The script deployment record id
 *
 *
 * ******************************************************************* */
define(['N/record', 'N/search', 'N/log'],
  function(record, search, log) {

    function afterSubmit(context) {
      var rec = context.newRecord;
      log.debug('afterSubmit rec', rec);
      var recid = rec.id;
      log.debug('afterSubmit recid', recid);
      var recordType = rec.type;
      log.debug('recordType', recordType);

      try {

        var relatedpo = getRelatedPO(recid);
        log.debug('relatedpo', relatedpo);

        if (relatedpo.relatedpo != null && relatedpo.relatedpo != '' && relatedpo.override == false) {
          record.submitFields({
            type: recordType,
            id: recid,
            values: {
              custbodyrel_po: relatedpo.relatedpo
            }
          });
          log.debug('setting PO value');
        } else {
          return false;
        }
      } catch (e) {
        log.debug('Error reads: ', e.name + e.message);
      }

      function getRelatedPO(internalid) {
        var vendorbillSearchObj = search.create({
          type: "vendorbill",
          filters: [
            ["type", "anyof", "VendBill"],
            "AND",
            ["internalid", "anyof", internalid],
            "AND",
            ["mainline", "is", "T"]
          ],
          columns: [
            search.createColumn({
              name: "internalid",
              label: "Internal ID"
            }),
            search.createColumn({
              name: "createdfrom",
              label: "Created From"
            }),
            search.createColumn({
              name: "internalid",
              join: "createdFrom",
              label: "Internal ID"
            }),
            search.createColumn({
              name: "custbodyrel_po_override",
              label: "Override Purchase Order"
            }),
            search.createColumn({
              name: "formulatext",
              formula: "{createdfrom}",
              label: "Formula (Text)"
            })
          ]
        });
        var tempres = vendorbillSearchObj.run().getRange(0, 999);
        if (tempres.length > 0) {
          var temprelatedpo = tempres[0].getValue({
            name: "internalid",
            join: "createdFrom"
          });
          var tempoverride = tempres[0].getValue({
            name: "custbodyrel_po_override"
          })
          log.debug('temprelatedpo + override', temprelatedpo + " + " + tempoverride);
        }
        return {
          relatedpo: temprelatedpo,
          override: tempoverride
        };
      }
    }

    return {
      afterSubmit: afterSubmit
    };
  });
b
it looks like your code would only try to set custbodyrel_po if it logs
setting PO value
so check to check for the presence of that log
and that logging in general is setup correctly on your script depoyment
honestly its weird that you have the script in general
n
haha I get it
b
a custom body field deployed to purchase orders is also automatically deployed to vendor bills
the vendor bill created from the purchase order will inherit the value from the purchase order
n
its working as intended though
in the sense that it sets the value only when it finds a PO
but it blanks out anything I put in the field
if not
bit werid
just FYI I had an interfering WF