2

I trying to modify a Purchase Order when a user opens it. This seems like this is a really simple example but doesn't seem to work. In the GUI I don't see the "test" memo. And in the script debug the memo field is empty.

I know the script is running because of the debug.

/**
 * Update Drop Ship PO with route Information
 *
 * @author Patrick 
 * @NApiVersion 2.0
 * @NScriptType UserEventScript
 */


define(['N/search', 'N/record'],

    function(search, record) {
        function beforeLoad(context) {
            var newRecord = context.newRecord;
            newRecord.setValue({fieldId: 'memo', value: 'this is a test'});
            log.debug({title: 'memo', details: newRecord.getValue({fieldId: 'memo'})});

            return true;
        }
      return {
        beforeLoad: beforeLoad
    };
});

I am assuming it has something to do with the record I can modify but I can't for the life of me find a working example in the docs. Any help would be greatly appreciated.

Patrick_Finucane
  • 725
  • 1
  • 7
  • 24

2 Answers2

5

You cannot modify fields on existing records in beforeLoad; see the Help page for beforeLoad for details. Here's a snippet of beforeLoad limitations:

  • beforeLoad user events cannot be triggered when you load/access an online form.
  • Data cannot be manipulated for records that are loaded in beforeLoad scripts. If you attempt to update a record loaded in beforeLoad, the logic is ignored.
  • Data can be manipulated for records created in beforeLoad user events.
  • Attaching a child custom record to its parent or detaching a child custom record from its parent triggers an edit event.

The second bullet point is the relevant one for your issue.

erictgrubaugh
  • 8,519
  • 1
  • 20
  • 28
5

newRecord.setValue wont work in beforeLoad. Its better if you use newRecord.submitFields like below

record.submitFields({
    id: newRecord.id,
    type: newRecord.type,
    values: {'memo': 'this is a test'}
});

Hope it helps!!

Skromak
  • 490
  • 6
  • 12
Viks
  • 227
  • 1
  • 6